Affinity IT
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

Question on Integer type

Sneha Kapoor
Ranch Hand

Joined: Jun 08, 2009
Messages: 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
Messages: 2504

Rule : Instance class variables, if not initializes, assign null object and instance primitive variable, if not initialize, get assigned default (0) value.

[LEARNING bLOG] | ["Rohan" is part of my surname]
Vijitha Kumara
Ranch Hand

Joined: Mar 24, 2008
Messages: 1905



What this i refers to at runtime? That would give you the answer.

SCJP 5, SCWCD 5
Sneha Kapoor
Ranch Hand

Joined: Jun 08, 2009
Messages: 49

Thanks got it

I was null hence got null pointer
ram prabu
Greenhorn

Joined: Dec 23, 2008
Messages: 14

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
Messages: 12

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
Messages: 14

hi
yes till
we got null pointer exception when we are adding null with value.

Thanks
Ram
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
IntelliJ open source