Originally posted by Damanjit Kaur:
String s;
is declaration of variables s which is of type String class and hold some garbage value as reference i.e. can refer to some arbitrary value in memory.
No, you're maybe thinking of "C" language. In
Java, such arbitrary values do not happen.
If "String s;" occurs within the definition of a method (i.e. it is a "local variable"), then that declares a variable "s" that is a reference to a String. No value is assigned. However, Java will not allow the value of local variable "s" to be examined until it has been assigned; the compiler will refuse to compile the code if you try to examine it before it has a value.
If "String s;" occurs as the definition of a field of a class, then "s" has the value null, which is not an arbitrary value, but instead means "I am a reference currently pointing to no object".