what does String.class returns

I have a small doubt that,suppose String s="sudha"return s; //this is okbut what about return String.class;
[142 byte] By [sudha_1234a] at [2007-10-3 1:57:54]
# 1
That returns the Class instance that is mapped to the String class. Each class created in Java has a matching Class object. In your case, you have absolutely nothing to do with this object.
gimbal2a at 2007-7-14 18:56:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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.

sudha_1234a at 2007-7-14 18:56:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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.>

Madathil_Prasada at 2007-7-14 18:56:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
hisudha, it will show the Runtime Error ......saM
saM@spia at 2007-7-14 18:56:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
sorry for my mistake it will showCompile time Error.saM
saM@spia at 2007-7-14 18:56:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...