+ 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?
13 Antworten
+ 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
+ 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."
+ 6
null 💗🙌🤗
+ 4
For primitive types 0
For reference types null
For boolean false
+ 3
Ace thanks.
+ 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.
+ 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
+ 2
@Hatsy Rei and @Ace can you explain whats this error
https://code.sololearn.com/c7X7wO7YS1e3
+ 1
always null
0
Obj will have null value
0
its null :)