• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference in Objects between non-initialization and initializing to null?  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am faced with this question:
"What Java code is used for declaring and initializing a null object?

A.Object object = new Object();

B.Object object = new Object(null); //there is no such constructor

C. Object object = null

D.Object object = null();

The correct answer is C: To create or set an object to null, the null keyword can be used. Objects are also set to null when they are declared but not yet initialized.
A is incorrect because when the new keyword is used, it initializes the object to a new non-null object. "


The thing that confuses me is that I thought that when declaring a new Object without initializing it, then the default value in null.
for example if I write


then the String object value will be null.

Doesn't the same apply when I write



?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you seem to be mixing the concepts of reference variables, and the objects that they point to. Reference variables are declared -- not objects. Objects are instantiated. And also, the null keyword, is used when the reference variable is not pointing to any object.

Ioanna Katsanou wrote:
The thing that confuses me is that I thought that when declaring a new Object without initializing it, then the default value in null.
for example if I write


then the String object value will be null.

In this case, you are declaring a String reference variable (named value), and you are assigning it to a String object (blank zero-length string) that you just instantiated (with the new operator).

If you want to declare a String reference variable, and assign it to null, then it would be...
Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
First of all, you seem to be mixing the concepts of reference variables, and the objects that they point to. Reference variables are declared -- not objects. Objects are instantiated. And also, the null keyword, is used when the reference variable is not pointing to any object.

Ioanna Katsanou wrote:
The thing that confuses me is that I thought that when declaring a new Object without initializing it, then the default value in null.
for example if I write


then the String object value will be null.

In this case, you are declaring a String reference variable (named value), and you are assigning it to a String object (blank zero-length string) that you just instantiated (with the new operator).

Henry


Ok, the problem that confuses me is what is the difference between



and



Don't both objects point to null??


 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:The thing that confuses me is that I thought that when declaring a new Object without initializing it, then the default value in null.
Instance variables which are just declared in a class are initialized to their default values when an object of that class is created. Default value of String (or any object) is null, see default value table provided in that link.

See If below example helps:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... I seemed to have edited my post at the same time as this one. But, I can answer this again...

Ioanna Katsanou wrote:
Ok, the problem that confuses me is what is the difference between

and

Don't both objects point to null??


Reference variables point to null. Objects don't directly point to anything. So, neither is an example of "objects point to null"!

The first is a reference variable declaration that is initialized to point to a zero-length string (ie. "", that is instantiated). The second is a reference variable declaration that is initialized to point to null.

Henry

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:Ok, the problem that confuses me is what is the difference between
and Don't both objects point to null??

You are mistaken about what new String() produces. It is NOT null. It produces a String object that just happens to contain zero characters, i.e., an empty String.

Also, I don't think it's appropriate to say "points to null." Notice that the text that you cited says "set to null." A reference that is set to null points to nothing. That is, a null reference IS null, it doesn't "point to" null. When you assign the result of new Whatever() to a reference variable, then it is said to reference/point to something, i.e. the Whatever object that was instantiated.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk Thank you all !! I finally understood this !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!