Abhishek Sanghvi
Abhishek Sanghvi

Reputation: 4651

java basics about String

To what value is a variable of the String type automatically initialized?

Upvotes: 1

Views: 724

Answers (10)

adibro500
adibro500

Reputation: 171

  1. Any variable that is declared within a class is automatically initialized.
  2. Any variable that is declared within a method must be initialized otherwise it will generate an error.
  3. Strings are initialized to null,ints to 0 and so on..

Check this page for more information...

Upvotes: 0

Maestrot
Maestrot

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

Rajan Marimuthu
Rajan Marimuthu

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

user1056075
user1056075

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

DigitalRoss
DigitalRoss

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

coobird
coobird

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:

  • If the String is a local variable, it will not be initialized.
  • If the 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

Brandon E Taylor
Brandon E Taylor

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

Randell
Randell

Reputation: 6170

null

Upvotes: 1

Martin v. Löwis
Martin v. Löwis

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

Noon Silk
Noon Silk

Reputation: 55072

null

Unless it's inside a method (local variable), in which case it's not declared to anything.

Upvotes: 7

Related Questions