0

I am confused with null in java. Book says that if we declare a variable of reference type without initialization, the variable will have default value null. So I have a try like this

import edu.princeton.cs.algs4.*;

public class test
{
     public static void main(String[] args)
    {
         String a;
         StdOut.println(a);
    }
}

However it is wrong in java because variable a isn't initialized. This contradicts to that a will have default value null.

I have another confusion is that if I run the code below

import edu.princeton.cs.algs4.*;

public class test
{
    public static void main(String[] args)
    {
         StdOut.println(null+"a");
         StdOut.println("null"+"a");
    }
}

both code lines print nulla. So is it true that string "null" equals null?

Can someone help me clear these two questions? Any advice is helpful. Thx.

gaoxinge
  • 459
  • 2
  • 8
  • 21
  • "This contradicts to that a will have default value null." I'm pretty sure the book is talking about java code that *compile*, your sample doesn't (see https://ideone.com/GT31ar) –  Feb 01 '16 at 05:55
  • @RC It is exactly my problem. Why is there no standout output? a=null? – gaoxinge Feb 01 '16 at 05:59

2 Answers2

4

Book says that if we declare a variable of reference type without initialization, the variable will have default value null.

What book? It's wrong, if that's what it really says. That statement only applies to static and instance variables, not to local variables. Local variables must be explicitly initialized before use, and the compiler knows you haven't done so, and gives a compilation error (rather than let your program execute in an undefined way).

is it true that string "null" equals null?

It is true that String.valueOf(null) equals "null".

user207421
  • 305,947
  • 44
  • 307
  • 483
  • you have a typo double "knows" +1 – Frakcool Feb 01 '16 at 06:09
  • So system.out.println can transfer *null* into *"null"*? – gaoxinge Feb 01 '16 at 06:19
  • @gaoxinge It's more complex than that. `println(null)` calls `println(Object)`, which calls `String.valueOf().` `println(null+"a")` causes `null+"a"` to be evaluated via string concatenation using `StringBuffer` or `StringBuilder`, which will use `append(null)` which calls `String.valueOf()`. Something like that. – user207421 Feb 01 '16 at 06:24
1

What you are doing in your program is wrong, as if you are creating a local variable then you must have to initialize it with some value. A value is assigned to instance or static class level variable by JVM if it is not initialized but not to the local variable. So if you write your code as follows:

import edu.princeton.cs.algs4.*; 

public class test 
{ 
     static String a;
     public static void main(String[] args)
    {              
         StdOut.println(a);
    } 
} 

This will give you null as output because now a is not a local variable.

Now your second question: The relation between null and "null" in java

If we look at the java documentation for String.valueOf

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
} 

From this we can easily see that if you try to get the value of an object that is null, it will return a String that is not null and has a value "null". This is the reason why in both the ways you are getting the same value "nulla"

I hope my answer will be helpul to you to get an idea of null.

amit
  • 807
  • 6
  • 14