Reputation: 4651
To what value is a variable of the String type automatically initialized?
Upvotes: 1
Views: 724
Reputation: 171
Check this page for more information...
Upvotes: 0
Reputation: 43
If the variable of type String is within a method, it would not automatically initialise. Otherwise it would be initialised with null as a value.
Upvotes: 0
Reputation: 1
the string value should be NULL in default , no need to initialize it. string class objects are NULL by default only if they are defined as class level atttributes, otherwise string objects don't have any default value they need to be explicity initialized.
Upvotes: 0
Reputation:
String str=null
means that the str is a object of String class which is not pointing to anything...but when we talk abt memory allocation,memory will be allocated to str as soon as it comes into existence....u can check the amount of memory by using the profiling option in netbeans..
Upvotes: 0
Reputation: 146053
It's null
unless it's local, in which case it is technically uninitialized, but in fact you can't use it, for that reason, so the language is still type-safe. You can't deref a garbage pointer.
Upvotes: 2
Reputation: 160954
Here's a summary of the answers posted by Martin v. Löwis and silky.
We can say the following about the initialization of a String
object:
String
is a local variable, it will not be initialized.String
is a class variable, instance variable, or an array component, then it will be initialized to null
.The reasoning is as follows:
As a variable with the type of String
is a reference type, according to The Java Language Specification, Third Edition, Section 4.12.5: Initial Values of Variables says the following:
Every variable in a program must have a value before its value is used
It goes on to say the following about the initialization of reference types:
- Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
- [removed information on irrelevant information]
- For all reference types (§4.3), the default value is
null
.
And finally, the follow about local variables:
- A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified by the compiler using the rules for definite assignment (§16).
Upvotes: 5
Reputation: 25339
If the variable is a class variable, instance variable, or array component, it is initialized to null
(since the default value for a reference type is null
)
If the variable is a local variable, then it must be given a value explicitly (i.e. it has no default value in this case).
Upvotes: 3
Reputation: 127447
A variable of type String is a reference variable. As an instance variable, it gets initialized to null
, see the specification for the discussion of other cases.
Upvotes: 2
Reputation: 55072
null
Unless it's inside a method (local variable), in which case it's not declared to anything.
Upvotes: 7