| Author |
Question on Integer type
|
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
Hi All,
I thought answer would be 4 but answer is D
Can anyone please explain
Source : Whizlabs simulator
Thanks
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
Rule : Instance class variables, if not initializes, assign null object and instance primitive variable, if not initialize, get assigned default (0) value.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
What this i refers to at runtime? That would give you the answer.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
Thanks got it
I was null hence got null pointer
|
 |
ram prabu
Greenhorn
Joined: Dec 23, 2008
Posts: 15
|
|
hi
The primitive instance variable default value is 0 (if not explicitly declared).
int i;
now i value is 0
Wrapper instance variable default value is null. Because the wrapper is object
Integer i;
now i value is null
Now the answer is null pointer exception because you are adding (null+value). No way to add by this way.
Thanks
Ram
|
 |
Till Stoschus
Greenhorn
Joined: May 28, 2009
Posts: 14
|
|
Hi "ram prabu"
but the exception is thrown because the implicit Method call i.intValue() in "x = i+y;" refers on a null-value, right?
Till
|
 |
ram prabu
Greenhorn
Joined: Dec 23, 2008
Posts: 15
|
|
hi
yes till
we got null pointer exception when we are adding null with value.
Thanks
Ram
|
 |
 |
|
|
subject: Question on Integer type
|
|
|