• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Literal value of object reference variable

 
James Qu
Greenhorn
Posts: 6
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mala Gupta's OCA java se 7 certification guide. On page 83, "The literal value of all types of object reference variables is null". I wonder why the literal value is not the heap memory address of the actual object it is pointing? My guess is the reference variable can be created without pointing to any object. So it can only be null. I'm not sure about my guess though, please help!
Thanks!

Regards,
James
 
Liutauras Vilda
Bartender
Pie
Posts: 2806
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say simply, literal value is the one, which you can hard code.
For the object reference only one exist which you can hard code and assign is "null".
 
Liutauras Vilda
Bartender
Pie
Posts: 2806
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took my hardcopy of your mentioned book, and found for you probably more clear and accurate answer:
"A literal is a fixed value that doesn't need further calculations to be assigned to any variable."

So, actually it clearly explains, why objects can have only "null" literal value.
 
James Qu
Greenhorn
Posts: 6
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vilda, thanks for the reply.
"A literal is a fixed value that doesn't need further calculations to be assigned to any variable."
Actually I'm still confused about this definition.. For example, String is a object. "abc" is a fixed value and it doesn't need further calculation to be assigned to any string. But the literal of String is still null. So why "abc" is not literal value of String?
 
Liutauras Vilda
Bartender
Pie
Posts: 2806
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that is why my first post wasn't fully right and I'd say a bit misleading, so probably I confused you.
"abc" is not a String literal, it is a String value.
Since actual value cannot be directly assigned to reference variable (as reference variable doesn't hold that value itself, just pointing to the location in memory where it is stored) it is not called a String literal.

In order do not mislead you more, you will find that information explained in details in Chapter 4. Sorry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:"abc" is not a String literal, it is a String value.

Be very careful: depending on how that String is created, "abc" is a String literal or not!

In this code"abc" is definitely a String literal (and will be added in the String Literal Pool). But in this code it isn'tDon't forget that although String is an object, it's a special one. And because it's a special object you can do things which are impossible with other objects: e.g. create a new instance without using the keyword new, use it in a switch statement,...

James Qu wrote:But the literal of String is still null. So why "abc" is not literal value of String?

Because it makes more sense to have null as default value for a String than "abc" or "Java rocks".

Hope it helps!
Kind regards,
Roel
 
James Qu
Greenhorn
Posts: 6
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Liutauras Vilda wrote:"abc" is not a String literal, it is a String value.

Be very careful: depending on how that String is created, "abc" is a String literal or not!

In this code"abc" is definitely a String literal (and will be added in the String Literal Pool). But in this code it isn'tDon't forget that although String is an object, it's a special one. And because it's a special object you can do things which are impossible with other objects: e.g. create a new instance without using the keyword new, use it in a switch statement,...

James Qu wrote:But the literal of String is still null. So why "abc" is not literal value of String?

Because it makes more sense to have null as default value for a String than "abc" or "Java rocks".

Hope it helps!
Kind regards,
Roel

Roel, thanks for the reply. But to the objects other than String, is null the only literal value? For example, to String objects, "abc" can be the literal value.

regards,
James
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Qu wrote:But to the objects other than String, is null the only literal value? For example, to String objects, "abc" can be the literal value.

Like I said before, String is a special object and each hard-coded String in your code will be considered as a literal value (e.g. "abc", "Java rocks!",...), strings created at runtime will not be considered as a literal. Each String literal will be added to the String Literal pool. All other objects don't have any literal value, except for null of course. The reason is very simple: to create any object (except for String) there is always some calculations and constructing (because you have to invoke constructor) involved.

And don't get fooled by code like thisIt looks like myInt is assigned a literal value, but it isn't. This code has an int primitive (1) which is definitely a literal! But myInt refers to an Integer object (with 1 as value) and is not a literal.

Hope it helps!
Kind regards,
Roel
 
James Qu
Greenhorn
Posts: 6
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
James Qu wrote:But to the objects other than String, is null the only literal value? For example, to String objects, "abc" can be the literal value.

Like I said before, String is a special object and each hard-coded String in your code will be considered as a literal value (e.g. "abc", "Java rocks!",...), strings created at runtime will not be considered as a literal. Each String literal will be added to the String Literal pool. All other objects don't have any literal value, except for null of course. The reason is very simple: to create any object (except for String) there is always some calculations and constructing (because you have to invoke constructor) involved.

And don't get fooled by code like thisIt looks like myInt is assigned a literal value, but it isn't. This code has an int primitive (1) which is definitely a literal! But myInt refers to an Integer object (with 1 as value) and is not a literal.

Hope it helps!
Kind regards,
Roel


I get it now. Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic