Tuesday, March 17, 2015

What happens when creating instance inside constructor of the same class - frequently asked questions on core java

Q: What happens when creating instance inside constructor of the same class - frequently asked questions on core java?

package com.sat.test.java;
public class TestConstructor {
public static void main(String[] args) {
A a = new A();
}
}
class A{
A(){
System.out.println("class A()");
new A();
}

}



OUTPUT

class A()
class A()
class A()
.
.
.

class A()
class A()
class A()
class A()
class A()
class A()
class A()
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByte.withResult(Unknown Source)
at sun.nio.cs.SingleByte.access$000(Unknown Source) 


Explanation: When we call new A() from A() constructor it will try to create as many instances as it can but when it got exhausted it will throw StackOverflowError.

Monday, May 12, 2014

Difference between load() and get() methods in Hibernate

Difference between load() and get() methods in Hibernate


Dont use load() when you are not sure about the object exists/record exists in database.
When you call load() method if the object is not exist/unique id is not found in DB, then it will throw an exception.

When you call get() method if the object is not exist then it will return NULL reference.


Hibernate persisting transient object to database - Equality and Identity of persistent object

Hibernate persisting transient object to database How it behaves?.

It is not appropriate to save an object that has already been persisted. Equally, it is not
appropriate to update a transient object. If it is impossible or inconvenient to determine the
state of the object from your application code, you may use the saveOrUpdate() method.

When we have a persistent object in Hibernate,
that object represents both an instance of a class in a particular Java virtual machine (JVM)
and a row (or rows) in a database table (or tables).
Requesting a persistent object again from the same Hibernate session returns the same
Java instance of a class, which means that you can compare the objects using the standard Java
== equality syntax. If, however, you request a persistent object from more than one Hibernate
session, Hibernate will provide distinct instances from each session, and the == operator will
return false if you compare these object instances.

Saturday, May 10, 2014

About Hashmap in Java Collections

Hashmap: Hashmap is collection class where you can store Objects and playing with it.
In Hashmap objects are stored in Key, Value pair.
Keys are also objects, Hashmap stores the keys based on Object hashing.
The better your hashCode implementation the better you can store Hashmap.

Hashmap accepts only one NULL object as key.

NOTE: When you try to add two null keys to hashmap it takes only the last added entry.


package com.sat.java.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Satya
 */
public class TestJava {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        Map map = new HashMap();
       
        map.put(null, "X");
        map.put(null, "Y");
        //Iterating over entrySet
        for(Entry ent: map.entrySet()){
            System.out.println(ent.getKey() + " , " + ent.getValue());
        }

    }

}


Output of this program:
null , Y


Monday, November 11, 2013

Difference between Aggregation and Composition in java.

Difference between Aggregation and Composition in java.

Aggregation means weak relationship. A container object can have the contained object, but its not mandatory with out contained object , container object will not exist.

Best Example is, A student and library

Composition means strong relationship, A container object will not exist without the contained object.
Best Example is, A student and Book

In composition (Person, Heart, Hand), "sub objects" (Heart, Hand) will be destroyed as soon as Person is destroyed.

In aggregation (City, Tree, Car) "sub objects" (Tree, Car) will NOT be destroyed when City is destroyed.

The bottom line is, composition stresses on mutual existence, and in aggregation, this property is NOT required.

Thursday, May 13, 2010

Example Build File Using the Custom Ant Task

 

1 <?xml version="1.0"?>
2 <project name="CodegenExample" default="main" basedir=".">
3
4 <path id="example.classpath">
5 <fileset dir="classes">
6 <include name="**/*.jar" />
7 </fileset>
8 </path>
9
10 <target name="declare" >
11 <taskdef name="codegen"
12 classname="org.apache.axis2.tool.ant.AntCodegenTask"
13 classpathref="example.classpath"/>
15 </target>
16
17 <target name="main" depends="declare">
18 <codegen
19 wsdlfilename="C:\test\wsdl\CombinedService.wsdl"
20 output="C:\output"
21 serverside="true"
22 generateservicexml="true"/>
23 </target>
24
25 </project>

 

 

 

http://ws.apache.org/axis2/tools/1_4/CodegenToolReference.html


You might also like ..


SHARE & SAVE
Delicious Digg Furl Stumbleupon Technorati Squidoo Reddit live Yahoo MySpaceGoogle Yahoo Buzz Facebook Twitter Orkut Google Buzz Email this postby bbP

Monday, April 26, 2010

what will happen if I serialize and de-serialize a singleton instance in same JVM?

what will happen if I serialize and de-serialize a singleton instance in same JVM?

We will get two instances of Singleton object. To avoid that we should use a readResolve() method which returns the singleton instance.


Implementing a Serializable Singleton:

public class MySingleton implements Serializable
{
static MySingleton singleton = new MySingleton();
private MySingleton() {
}
// This method is called immediately after an object of this class is deserialized.
// This method returns the singleton instance.
protected Object readResolve()
{
return singleton;
}
}


You might also like ..


SHARE & SAVE
Delicious Digg Furl Stumbleupon Technorati Squidoo Reddit live Yahoo MySpaceGoogle Yahoo Buzz Facebook Twitter Orkut Google Buzz Email this postby bbP

what is JIT - Just In Time Compiler?

what is JIT - Just In Time Compiler?

JIT is sometimes called as Dynamic translation is a technique to improve the run time performance of a computer system.
JIT builds upon two earlier ideas in run-time environments: bytecode compilation and dynamic compilation.
It converts code at runtime prior to executing it natively, for example bytecode  into native machine code.

In a bytecode-compiled system, source code is translated to an intermediate representation known as bytecode. Bytecode is not the machine code for any particular computer, and may be portable among computer architectures. The bytecode may then be interpreted by, or run on, a virtual machine. A just-in-time compiler can be used as a way to speed up execution of bytecode. At the time the bytecode is run, the just-in-time compiler will compile some or all of it to native machine code for better performance. This can be done per-file, per-function or even on any arbitrary code fragment; the code can be compiled when it is about to be executed (hence the name "just-in-time").

Resource: http://en.wikipedia.org/wiki/Just-in-time_compilation


You might also like ..


SHARE & SAVE
Delicious Digg Furl Stumbleupon Technorati Squidoo Reddit live Yahoo MySpaceGoogle Yahoo Buzz Facebook Twitter Orkut Google Buzz Email this postby bbP