• Post Reply Bookmark Topic Watch Topic
  • New Topic

"The literal value for String is null" - what does this mean?  RSS feed

 
Iarla O'Riada
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The literal value for String is null". The quote is from Mala Gupta's OCA I exam prep book.

According to Wikipedia, a literal is "any notation for representing a value within source code".

But that definition covers anything. Does she mean null is the default value?

Many thanks,
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means you literally get a string back saying "null" like "Iarla"
Check out the source code for String.valueOf(Object obj)
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is indeed a misleading statement. I think the author may have meant default value as null is default value for all reference types.

See the Default Values section here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
 
Iarla O'Riada
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Maneesh & James, very interesting.

Maneesh, that sounds like a sensible interpretation. I find it hard to believe that a String reference that I assign as null will actually reference a String object with the value null but I'll check it in my IDE.

James, that also makes sense that it defaults to null. Thank you for the link, I see from reading that the default values only apply to class members and not to local variables.

I'll update the post on the results of my String tests as Maneesh suggested.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iarla O'Riada wrote:
Maneesh, that sounds like a sensible interpretation. I find it hard to believe that a String reference that I assign as null will actually reference a String object with the value null but I'll check it in my IDE.

Umm. Not really. You might want to remember the valueOf() is a static method
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iarla O'Riada wrote:I find it hard to believe that a String reference that I assign as null will actually reference a String object with the value null

So do I as this is not true. Strings are treated no differently to any other reference type here. From the link provided above:

There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!