Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default values for primitives

In Java, what would a variable of type int hold if it was not initialized (I know that it will not let me compile if I used x directly before initializing)?

say, if I did:

int x;

and how about

int[] x;

Thanks

like image 932
vtshadow Avatar asked Nov 20 '12 03:11

vtshadow


People also ask

What are default values of boolean primitive types?

The default value of primitive boolean is false .

What are the default values of non primitive data types?

In simple words, it means that whenever we are passing a non-primitive data type to a function or a variable, we are simply passing the address where the data is stored. The default value of reference variables is null.

What is a default value in Java?

In Java, the default value of char is “u0000”.


1 Answers

Instance variables will be defaulted to a 'reasonable' value. Local variables will contain garbage.

Read up on the topic here.

like image 58
Perception Avatar answered Oct 13 '22 23:10

Perception