• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding null  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: It will throw a RuntimeException at run time. That is because of str.concat(str+" "+ind). I do not understand null in general. You can assign null to any object, but not to a primitive variable. I do not know how to treat null. Is it an object? You can add and remove null from an ArrayList. The output is null. You can even print it, thus null is not nothing; it is something. What is null?
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found one more text about null: http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html

null seems to be something problematic, there has been introduced a new class in Java SE 8: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

To my understanding null is the absence of any value. I do not really know how to deal with the absence of any value. You can even print this absence of any value - no value is a value too.
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote: It will throw a RuntimeException at run time.

Because when you instantiate an array in that way, you are creating an array with 2 elements that are both initially null.
And when you do str.concat(str+" "+ind) , you can't invoke concat on a null reference.

Another way to create an array:


This has 2 elements AND they are initialized.


Urs Waefler wrote:


null is for reference types only ... NOT for primitive types.

Urs Waefler wrote:What is null?

A variable of reference type (String, Integer, etc...) that contains a null means: that variable does NOT refer to an object.
 
Stephan van Hulst
Saloon Keeper
Posts: 7968
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables of reference types don't hold objects. They hold references to objects. null is a reference to no object.

I'm confused, wasn't this part of the SCJP 1.4?
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SCJP 1.4 is almost ten years back. Since then I never had to do with Java; now I want to renew the knowledge and in the year 2007 you did not have to reach 100 % to pass the exam. If I remember right, it was just 52 %. This line does not throw a RuntimeException. In the meanwhile I understand that if you call a method on null it throws a RuntimeException. I would like to know more about null and its history, null stands for the absence of any value. What does that mean in details?
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote: This line does not throw a RuntimeException.

Indeed it IS correct.

But it is equivalent to write:

String[] arr = { null, null };

That is: an array having 2 elements initially with null value.

And when you do:


You are doing 2 cycles and on each cycle str is null. And I repeat, a method cannot be invoked on a null.
 
William Ng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null is a valid value in Java, and is used as a placeholder for objects that do not have a reference. Primitives are not objects. They are simple types, for simple values. It is the default value for ojbects that have  reference variables that have not yet be assigned an object. Think of it has the same as a 0 or "".



At a high level, that is really all it is. You pass null around like a 0. If you know C/C++, then you will know exactly what i mean There are also some necessary differences between the above and null, is that you can really perform operations on it like you would the others. If you try, you get a NullPointerException.
 
Stephan van Hulst
Saloon Keeper
Posts: 7968
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Ng wrote:null is a valid value in Java, and is used as a placeholder for objects that do not have a reference.

No, null is a reference to no object.

It is the default value for ojbects that have  reference variables that have not yet be assigned an object.

You mean fields that have a reference type.


It's String, not string. "" is NOT the default value for String fields, because String is a reference type.

You pass null around like a 0. If you know C/C++, then you will know exactly what i mean

And that's a really bad way of thinking about it. 0 represents a value. null does not. It represents the lack of a value.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an object reference is like a notecard where you keep track of houses you've bought by writing the address down on it.  A reference set to null is like a blank card - there's nothing written on it, so it doesn't make sense to say "go to the address on this card and tell me how many bedrooms that house has".  If you did ask someone to do that, they's say "umm...this card is blank. I can't go there because there is no 'there'".

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!