u says that class instance ,
when class instance will be created ?
String s="sudha"
or
String s=new String("sudha");
In these two situations class instance i.e object will be created.
but u says that class instance will be return.
what is the difference between these two instances i.e
String.class instance and String s=new String("sudha")
please give me clearly.
Cheers
Sudha.
String s=new String("sudha");
will create an object of class 'java.lang.String'. The String will contain the content "sudha" (internally as a char array). That said, it would be better if the String is created as
String s="sudha";
This would help the jvm do some optimization for you.
String.class
refers to an instance of java.lang.Class which will be created for every object (implicitly). Its a statis method that returns an object of java.lang.Class associated with the String class. From this object you can get information about the String class itself. (for example, its methods, variables etc).
example
class Test{
public static void main(String args[]) {
Class cls = String.class;
System.out.println("Name of class is "+cls.getName());
System.out.println("Methods are");
java.lang.reflect.Method meth[] = cls.getMethods();
for(int i = 0; i<meth.length; i++){
System.out.println(meth[i].getName());
}
}
}
ram.>