• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Why can't string be null?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why if I remove the else statement my code doesn't compile?
Shouldn't it just print null if it doesn't get initialised?

Thanks.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's because local can't be null, but that the compiler knows that it may not have been initialized.
 
Moe Jackson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I don't think it's because local can't be null, but that the compiler knows that it may not have been initialized.



Shouldn't it just print null if it isn't initialised?
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Moe Jackson wrote:Shouldn't it just print null if it isn't initialised?



No. Local variables must be initialized. Instance and class variables, on the other hand, don't need to be initialized and they default to zero or null if you don't initialize them.
 
Moe Jackson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok thanks.
 
Rancher
Posts: 179
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The distinction between "null" and "non-initialized" is more clear in other languages like C++. Java is usually a safe enough language where you don't have to worry about it, except in this specific case.

In almost all programming languages (Java included), variables do not store the value of the object declared inside them. If you declare that "String s = "abc"", the String variable s does not store "abc". Rather it stores an integer which points to a location in memory where the actual string "abc" is stored. On the other hand, if a variable is set to null, that variable stores the number 0. Zero is a pointer to the "null address" of memory.

In Java, most of this is abstracted away from you. There's no way to get the actual pointer value of a variable. However it's still important to understand in order to know how objects are going to perform. If you create an object like this:

You have only created one object, but two references to that object.

Now why does this matter for the topic at hand?
Well, if you make an uninitialized variable:

You might imagine that s is null, but it is not. You didn't set s. It's not a pointer to memory address 0 (null), it's actual nothing at all.
Java tries to be a really safe language so you can't mess yourself up with this. That's why it forces you to initialize variables. However in C++ and lower level languages, you CAN use variables when they are not initialized, and they aren't null. You can either crash your program or get data from some other place in memory. That's why this is important. Java also does try to be safe by usually automatically initializing variables to null or some other default if you don't.
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there such a thing as a null address, or does the runtime simply interpret 0 as meaning null?
 
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Instance and class variables, on the other hand, don't need to be initialized and they default to zero or null if you don't initialize them.



Actually, in Java, all variables are initialized. But if they are not explicitly initialized, they default to implicit initialization of 0 for arithmetic values or null for objects. This was a conscious design decision on the part of Gosling and his team because Java was intended to be secure and reliable and one of the biggest sources of hard-to-detect bugs in C and C++ was uninitialized variables, since they were effectively initialized all right, just with whatever random value had been living in their alloted RAM location.

Campbell Ritchie wrote:Is there such a thing as a null address, or does the runtime simply interpret 0 as meaning null?



There is no such thing as a "null address" in Java, because Java does not support pointers. Object references are not the same either theoretically nor in many cases, in practice.

But yes, in C and C++ "0" is officially designated as the null pointer value even on hardware where there is no concept of memory location zero.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not type
String local = "";
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . all variables are initialized. But if they are not explicitly initialized, they default to implicit initialization . . .

I thought implicit initialisation only applies to fields. In the case of local variables, every path of execution must contain an explicit initialisation of that variable. It appears in this Java Language Specification (=JLS) chapter.

That JLS chapter wrote:Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.

There is no such thing as a "null address" in Java . . .

That is what I though. Thank you.

Denissio Tor wrote:. . . String local = "";

Yes, that would correct the compiler error.
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic