Race conditions occurs when two thread operate on same object without proper synchronization and there operation interleaves on each other. Classical example of Race condition is incrementing a counter since increment is not an atomic operation and can be further divided into three steps like read, update and write. if two threads tries to increment count at same time and if they read same value because of interleaving of read operation of one thread to update operation of another thread, one count will be lost when one thread overwrite increment done by other thread. atomic operations are not subject to race conditions because those operation cannot be interleaved
Wednesday, 19 August 2015
Friday, 12 June 2015
Tuesday, 2 June 2015
Find the missing elements in the array - java program
public class MissingElement {
public static void main(String args[]){
int a[] = { 1,3,4,5,6,7,10 };
int j = a[0];
for (int i=0;i<a.length;i++)
{
if (j==a[i])
{
j++;
continue;
}
else
{
System.out.println(j);
i--;
j++;
}
}
}
}
Out put :
2
8
9
public static void main(String args[]){
int a[] = { 1,3,4,5,6,7,10 };
int j = a[0];
for (int i=0;i<a.length;i++)
{
if (j==a[i])
{
j++;
continue;
}
else
{
System.out.println(j);
i--;
j++;
}
}
}
}
Out put :
2
8
9
Monday, 18 May 2015
How to reverse the ArrayList elements without using reverse method in JAVA
public class ArrayList1{
public static void main(String[] args){
ArrayList al= new ArrayList();
al.add(10);
al.add(20);
al.add(30);
System.out.println(al); //[10 20 30]
int length=al.size();
for(int i=length-1; i>=0;i--){
System.out.println(al.get(i)); //30 20 10
}
}
}
public static void main(String[] args){
ArrayList al= new ArrayList();
al.add(10);
al.add(20);
al.add(30);
System.out.println(al); //[10 20 30]
int length=al.size();
for(int i=length-1; i>=0;i--){
System.out.println(al.get(i)); //30 20 10
}
}
}
Java Program for given Integer value in to reverse
public class ReverseNo{
public static void main(String [] args){
int n=894, sum=0, m;
while(n>0){
m=n%10; //Here Reminder - 4
sum=sum*10+4; //Here sum - 4
n=n/10; //quotient - 89
//System.out.println(sum); //after failing loop we will get the values 4 ,49 , 498
}
System.out.println(sum);
}
}
public static void main(String [] args){
int n=894, sum=0, m;
while(n>0){
m=n%10; //Here Reminder - 4
sum=sum*10+4; //Here sum - 4
n=n/10; //quotient - 89
//System.out.println(sum); //after failing loop we will get the values 4 ,49 , 498
}
System.out.println(sum);
}
}
Friday, 8 May 2015
Thursday, 7 May 2015
what is use of Cascade attribute in hibernate
Main concept of hibernate relations is to getting the relation between parent and child class objects
Cascade attribute is mandatory, when ever we apply relationship between objects, cascade attribute transfers operations done on one object onto its related child objects
If we write cascade = “all” then changes at parent class object will be effected to child class object too, if we write cascade = “all” then all operations like insert, delete,update at parent object will be effected to child object also
Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will also be stored into the database.
default value of cascade =”none” means no operations will be transfers to the child class
Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will not be effected, if cascade = “none”
Cascade having the values…….
- none (default)
- save
- update
- save-update
- delete
- all
- all-delete-orphan
In hibernate relations, if we load one parent object from the database then child objects related to that parent object will be loaded into one collection right.
Now if we delete one child object from that collection, then the relationship between the parent object and that child object will be removed, but the record (object) in the database will remains at it is, so if we load the same parent object again then this deleted child will not be loaded [ but it will be available on the database ]
so all-delete-orphan means, breaking relation between objects not deleting the objects from the database.
Saturday, 25 April 2015
Thursday, 16 April 2015
Monday, 6 April 2015
Sunday, 5 April 2015
How to Compare the two Arrays Content in JAVA?
A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).
import java.util.Arrays;class Test{ public static void main (String[] args) { int arr1[] = {1, 2, 3}; int arr2[] = {1, 2, 3}; if (Arrays.equals(arr1, arr2)) System.out.println("Same"); else System.out.println("Not same"); }} |
Output: Same
Thursday, 12 March 2015
How to Read the XML file in JAVA
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("D://test.xml"); //path of the xml file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
==================================
test.xml file
==================================
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>xxxx</firstname>
<lastname>vvvv</lastname>
<nickname>ssss</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>yyyy</firstname>
<lastname>gggg</lastname>
<nickname>kkkkkk</nickname>
<salary>200000</salary>
</staff>
</company>
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("D://test.xml"); //path of the xml file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
==================================
test.xml file
==================================
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>xxxx</firstname>
<lastname>vvvv</lastname>
<nickname>ssss</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>yyyy</firstname>
<lastname>gggg</lastname>
<nickname>kkkkkk</nickname>
<salary>200000</salary>
</staff>
</company>
Tuesday, 10 March 2015
Object Restriction for the class in java?(How to restrict object creation not more than 3 in Java class?)
We can restrict the creation of Object for a particular class by little modification in Singleton design pattern. Following code is to create the three objects for the class;
public class LimitClass {
private static LimitClass limInstance;
public static int objCount = 0;
private LimitClass(){
objCount++;
}
public static synchronized LimitClass getLimInstance(){
if(objCount < 3 ){
limInstance = new LimitClass();
}
return limInstance;
}
public static void main(String[] args) {
LimitClass obj1 = LimitClass.getLimInstance();
LimitClass obj2 = LimitClass.getLimInstance();
LimitClass obj3 = LimitClass.getLimInstance();
LimitClass obj4 = LimitClass.getLimInstance();
LimitClass obj5 = LimitClass.getLimInstance();
LimitClass obj6 = LimitClass.getLimInstance();
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
System.out.println(obj6);
}
}
Monday, 9 March 2015
SQL Query to Display the department Number with more than ten employees in each department?
select deptno, count(*) from emp group by deptno having count (*) >10
sql query to find the recent joining employees in each department
select * from emp order by deptno, hiredate desc;
Saturday, 7 March 2015
inverse attribute in hibernate what is use of inverse attribute in hibernate
Inverse attribute informs the hibernate that the relation ship is Bi Directional
If we write inverse = “false” then hibernate understands that relationship as unidirectional and generates additional update operations on the database, so in order to reduce the internal operations, we need to include inverse=”true“
remember, default value of inverse =”false”
If we make inverse =”true” the performance will be increased
Thursday, 5 March 2015
What is orphan record in hibernate
an orphan record means it is a record in child table but it doesn’t have association with its parent in the application.
In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”.
In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”.
Tuesday, 3 March 2015
what is difference between ClassNotFoundException and NoClassDefFoundError in JAVA
ClassNotFoundException:
For dynamically provided class names at runtime if the corresponding .class file name not available then we will get the runtime exception saying CallsNotFoundException. It is Checked Exception.
NoClassDefFoundError:
For hard coded class names at runtime the corresponding .class file not availbale then we will get exception called NoClassDefFoundError. It is UnChecked Exception
For better understanding see the link:
https://www.youtube.com/watch?v=pZw6T-hNhjM
For dynamically provided class names at runtime if the corresponding .class file name not available then we will get the runtime exception saying CallsNotFoundException. It is Checked Exception.
NoClassDefFoundError:
For hard coded class names at runtime the corresponding .class file not availbale then we will get exception called NoClassDefFoundError. It is UnChecked Exception
For better understanding see the link:
https://www.youtube.com/watch?v=pZw6T-hNhjM
Friday, 27 February 2015
Differences Between Forwarding and Redirection
|
Forwarding
|
Redirection
|
|
While forwarding,
there is no intimation given back to the client.
|
While redirecting,
there will be an intimation given back to the client.
|
|
Only one pair of
request and response objects are created.
|
Minimum two pairs of
request and response objects are created by the container.
|
|
Automatically the data
is also forwarded.
|
Only control is
redirected, but data is not transferred.
|
|
Forwarding is only
possible within the server only.
|
Redirecting is
possible within the server and across the servers.
|
|
The destination
resource must be java enabled resource.
|
The destination
resource may be java enabled resource (or) it can be non-java enabled
resource.
|
Is it possible to read the data of a web application from another web application ?
Yes.
It possible by using the getContext() method. It gives ServletContext object of
that web application into our current web application.
ServletContext ctx =
getServletContext();
ServletContext ctx2 =
ctx.getContext(“webapp_name”);
Monday, 16 February 2015
Can we create a userdefined immutable class?
Yes.
i)
Make the
class as final and
ii)
make the data
members as private and final.
Program:
final class Test{
private int i;
Test(int i){
this.i=i;
}
public Test modify(int i){
if(this.i==i){
return this;
return(new Test(i))
}
}
public static void main(String args[] ){
Test t1= new Test(10);
Test t2= t1.modify(10)
Test t3= new Test(10);
Test t4=t1.modify(100);
System.out.println(t1==t2); //true
System.out.println(t1==t4);//false
System.out.println(t1==t3)//true
}
}
Program:
final class Test{
private int i;
Test(int i){
this.i=i;
}
public Test modify(int i){
if(this.i==i){
return this;
return(new Test(i))
}
}
public static void main(String args[] ){
Test t1= new Test(10);
Test t2= t1.modify(10)
Test t3= new Test(10);
Test t4=t1.modify(100);
System.out.println(t1==t2); //true
System.out.println(t1==t4);//false
System.out.println(t1==t3)//true
}
}
Wednesday, 4 February 2015
Xpath Expressions
XPath uses path expressions to select nodes or node-sets in an XML document.
In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.
XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
/ -->Selects from the root node
// -->Selects nodes in the document from the current node that match the selection no matter where they are
. -->Selects the current node
.. -->Selects the parent of the current node
@ -->Selects attributes
Friday, 30 January 2015
Java Program To Convert String To Integer and Integer to String
public class StringToInteger
{
public static void main(String[] args)
{
String s = "2015";
int i = Integer.parseInt(s);
System.out.println(i); //Output : 2015
}
}
public class IntegerToString
{
public static void main(String[] args)
{
int i = 2015;
String s = Integer.toString(i);
System.out.println(s); //Output : 2015
}
}
Thursday, 15 January 2015
Java program for without duplicate elements from the two arrayList Objects
We can retrieve elements without duplicate elements from the two arrayList Objects by using Set interface.we can add the two arrayList Objects to set by using addAll() method.
Program :
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resources;
public class ArrayList1 {
public static void main(String[] args) {
List l1=new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);
List l2=new ArrayList();
l2.add(3);
l2.add(2);
l2.add(4);
l2.add(1);
l2.add(5);
List l3=new ArrayList();
Set<Integer> s=new HashSet<Integer>();
s.addAll(l1);
s.addAll(l2);
System.out.println(s);
Program :
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resources;
public class ArrayList1 {
public static void main(String[] args) {
List l1=new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);
List l2=new ArrayList();
l2.add(3);
l2.add(2);
l2.add(4);
l2.add(1);
l2.add(5);
List l3=new ArrayList();
Set<Integer> s=new HashSet<Integer>();
s.addAll(l1);
s.addAll(l2);
System.out.println(s);
}
}
Out put:
[1, 2, 3, 4, 5]
What is sublist in java and how to get the sublist from the existing Arrayist?
Sublist is used to get the sublist from the existing ArrayList.
List subList(int fromIndex, int toIndex)
Here fromIndex is inclusive and toIndex is exclusive.In this we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.
List subList(int fromIndex, int toIndex)
Here fromIndex is inclusive and toIndex is exclusive. There are few important points regarding this method which I have shared at the end of this post.
Example of getting sub-list from an ArrayList
Points to Note in the below example:
The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).
package beginnersbook.com;
import java.util.ArrayList;
import java.util.List;
public class SublistExample {
public static void main(String a[]){
ArrayList<String> al = new ArrayList<String>();
al.add("Steve");
al.add("Justin");
al.add("Ajeet");
al.add("John");
al.add("Arnold");
al.add("Chaitanya");
System.out.println("Original ArrayList Content: "+al);
//Sublist to ArrayList
ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4));
System.out.println("SubList stored in ArrayList: "+al2);
//Sublist to List
List<String> list = al.subList(1, 4);
System.out.println("SubList stored in List: "+list);
}
}
Output:
Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya]
SubList stored in ArrayList: [Justin, Ajeet, John]
SubList stored in List: [Justin, Ajeet, John]
List subList(int fromIndex, int toIndex)
Here fromIndex is inclusive and toIndex is exclusive.In this we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.
List subList(int fromIndex, int toIndex)
Here fromIndex is inclusive and toIndex is exclusive. There are few important points regarding this method which I have shared at the end of this post.
Example of getting sub-list from an ArrayList
Points to Note in the below example:
The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).
package beginnersbook.com;
import java.util.ArrayList;
import java.util.List;
public class SublistExample {
public static void main(String a[]){
ArrayList<String> al = new ArrayList<String>();
al.add("Steve");
al.add("Justin");
al.add("Ajeet");
al.add("John");
al.add("Arnold");
al.add("Chaitanya");
System.out.println("Original ArrayList Content: "+al);
//Sublist to ArrayList
ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4));
System.out.println("SubList stored in ArrayList: "+al2);
//Sublist to List
List<String> list = al.subList(1, 4);
System.out.println("SubList stored in List: "+list);
}
}
Output:
Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya]
SubList stored in ArrayList: [Justin, Ajeet, John]
SubList stored in List: [Justin, Ajeet, John]
Subscribe to:
Comments (Atom)

