+ 13

[Solved] What is the default value of an unassigned object . ( edit : Now answered 🤩)

In java, When an object is defined but not assigned, e.g. MyClass obj; what happens? is the value null, can it be compared to null, or is there an error at compile time?

8th Sep 2018, 9:18 PM
ifl
ifl - avatar
13 ответов
+ 8
if you did not Define The Value The initial Values will be as follows 1) Boolean - false 2) byte - 0 (byte) 3) short - 0 (short) 4) int - 0 5) long - 0L 6) char - \u0000 7) float - 0.0f 8) double - 0.0d 9) Object Reference - null
9th Sep 2018, 3:48 PM
The Shiva
The Shiva - avatar
+ 7
In the case of local variables: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5 "A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified using the rules for definite assignment."
8th Sep 2018, 9:41 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
null 💗🙌🤗
10th Sep 2018, 5:22 AM
NimWing Yuan
NimWing Yuan - avatar
+ 4
For primitive types 0 For reference types null For boolean false
10th Sep 2018, 1:38 AM
Linda
Linda - avatar
+ 3
Ace thanks.
9th Sep 2018, 2:23 AM
ifl
ifl - avatar
+ 3
Yesh Jadav there is no error. it displays the string provided as expected, now that you changed the initial code to get variables initialized.
9th Sep 2018, 10:00 AM
ifl
ifl - avatar
+ 2
Ace So why do I have an error in the playground? "variable might not have been initialised" https://code.sololearn.com/c540uBwzKuus/?ref=app
8th Sep 2018, 9:29 PM
ifl
ifl - avatar
+ 2
@Hatsy Rei and @Ace can you explain whats this error https://code.sololearn.com/c7X7wO7YS1e3
9th Sep 2018, 9:13 AM
Yesh Jadav
Yesh Jadav - avatar
+ 1
always null
10th Sep 2018, 3:37 AM
MORE AMOL PANDURANG
0
Obj will have null value
10th Sep 2018, 5:03 PM
Abhishek Arora
Abhishek Arora - avatar
0
its null :)
17th Sep 2018, 9:55 AM
David Lee White
David Lee White - avatar