Sunday, August 1, 2010

Some tits-bits

>>> String.split("delimeter") - This will split the string on delimeter passed and return a String array.

>>> Synchronizing a static method or static block of a class is obtaining the lock on the class instead of instance. The lock on class can also be obtained by
synchronize(ABC.class){

}


>>> String.indexOf("abc") - This will return the index in int of the first occurrence of the string 'abc' in the string.

>>> Convert String to Number - Use wrapper class like Integer, Long, Float, Double.
int i = Integer.parseInt(String);
or,
int i = Integer.valueOf(String).intValue();


>>> Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized.

>>> Swap two variables without using a third variable
int a=5,b=10;a=a+b; b=a-b; a=a-b;

>>> Java object are stored in Heap.

>>>To get system name using java code print -
System.out.println(InetAddress.getLocalHost().getHostName());

No comments:

Post a Comment