Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Java String's default initial value?

Tags:

java

string

Consider a Java String Field named x. What will be the initial value of x when an object is created for the class x;

I know that for int variables, the default value is assigned as 0, as the instances are being created. But what becomes of String?

like image 571
Selvin Avatar asked Mar 22 '11 09:03

Selvin


People also ask

What is default initialization in Java?

From Java Language Specification. Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10): For type byte, the default value is zero, that is, the value of (byte)0. For type short, the default value is zero, that is, the value of (short)0.

What is default initial value?

The initial value of a CSS property is its default value, as listed in its definition table in the specification. The usage of the initial value depends on whether a property is inherited or not: For inherited properties, the initial value is used on the root element only, as long as no specified value is supplied.

What is the default initial value of a string?

An instance variable can also be a variable of object type. For such variables, the default initial value is null. (In particular, since Strings are objects, the default initial value for String variables is null.)


2 Answers

It's initialized to null if you do nothing, as are all reference types.

like image 186
duffymo Avatar answered Oct 31 '22 03:10

duffymo


That depends. Is it just a variable (in a method)? Or a class-member?

If it's just a variable you'll get an error that no value has been set when trying to read from it without first assinging it a value.

If it's a class-member it will be initialized to null by the VM.

like image 33
Dexter Avatar answered Oct 31 '22 03:10

Dexter