Saturday, October 13, 2012

Dynamic Loading vs Static Class Loading in java

Class in java can be loaded in two ways

Static means at compile time means using new operator.

Dynamic means using class.forname() a class is loaded dynamically during run time

so a decision to load a class dynamically at run time can be taken based on certain logic using property file etc.
getClass(); getName(); getDeclaredFields();
Instance can also be created using forName() method. It loads the class into the current class memory.  
it is similar like Reflection.

Difference between path and classPath in java

We always set a path and classpath while coding using java.

Path is for the OS to know where the java exe's are. by setting path you can execute java program from anywhere if not set then u need to go to bin directory of java and only then u can execute a java program.
Classpath is for the java program to know where the .class file are.

Classpath location points to where the .class file of your java programs are and /or the jar files (they contain the .class file used to code a specific functionality/usage)

Monday, July 30, 2012

public static void main (String[] args)

public : as it is accessible from anywhere for jvm
static: no need to create objects to call method.
void: no returning of value
main: syntax- main method - name to be followed
String[] args - syntax- to be followed

== vs equals in java

public class EqualsTest {
public static void main(String[] args) {

String s1 = "abc";

String s2 = s1;

String s5 = "abc";

String s3 = new String("abc");

String s4 = new String("abc");

System.out.println("== comparison : " + (s2 == s4));

System.out.println("== comparison : " + (s1 == s2));

System.out.println("Using equals method : " + s1.equals(s2));

System.out.println("== comparison : " + s3 == s5);

System.out.println("Using equals method : " + s3.equals(s4));

}

}
 
Output
 
== comparison : false


== comparison : true

Using equals method : true

false

Using equals method : true

Equals comapares the content, == compares the references(memory location), hence when new keyword is used to create string, == shows false.