chris Frisina
chris Frisina

Reputation: 19688

Why is a empty variable declaration of type String "null" and not "" (blank or empty)

Declaring a variable as such:

private String concatenation;

would have System.out.println(concatenation) show this:

null

Which would be a string of length 4, and not a null(of boolean type as in null or !null) value. This would make sense if I delcared it … = "null";, but I didn't.

but if it is declared as:

private String concatenation = "";

then, System.out.println(concatenation) would show this:

` ` // No clue how to get a space to show up in SO so I am surrounding it with ticks

Why does Java not just create it as an Object with an empty string, why do I have to define it as being empty.

Upvotes: 1

Views: 5690

Answers (4)

Sello
Sello

Reputation: 297

As previously said String is a class and variable concatenation is a reference variable and reference variables have default value of null, seing this output tells you that whatever reference variable that was, its not initialised, points to no object, so an empty string != null, these are two different values that mean different things. I think if it where to automatically create an empty string,it'l be creating unnecessary objects..

Upvotes: 0

user2277872
user2277872

Reputation: 2973

The String is not an object, it is a class. It is needed just like any other class if you're wanting to create an object off it. An example, if you did:

int i;

you would eventually have to initialize and set a value for i. Just like with String. You cannot have an object holding NULL (no specific spot allocated in memory), that will tell the program there is no such memory inside that object. That is why you get null being printed out. If you do "", it is blank, BUT, it is also initialized because the object has a spot in the memory system for the "" to be placed, and eventually searched and accessed later. I have had a bit of struggle with this throughout my Java programming, but I eventually learned that if I have my Strings, int(s), and etc initialized (set a value to the object for memory reference later) then nothing would cause a problem

Upvotes: -1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

It's not "null", it's null. But println displays it like that because it's defined to do so (see the Javadocs).

As for why it's not initialized to an empty string, that's because String is a class type, and the default value for references to class instances is always null.

There is a big difference between no object at all (i.e. null) and an object in an "empty" state.

Upvotes: 16

tgkprog
tgkprog

Reputation: 4598

This is because that is the way system out println works -> it prints null when the object send to it is null (or its a reference with no current reference)

You could over write it :

String concatenation = null;
System.out.println(concatenation ==null ? "" : concatenation )

This is sending an empty string to the println function instead of null. FYI : The function actually calls toString on most object to get what should be displayed, unless the object it self is a string .

More theory, try:

System.out.println(concatenation == null);

and see what happens. –( jlordo)

Upvotes: 1

Related Questions