• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Implication of autoboxing

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Can anybody explain me here in the code the implication of autoboxing...

public class Ex {

Integer i;
int j;

public void go(){
//i=j;
j=i;
System.out.println("value of j :"+ j);
System.out.println("value of i :"+ i);
}

public static void main(String[] args) {
Ex obj=new StaticEx();
obj.go();

}

}

Regards,
Amazer
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
public class Ex {

Integer i=0;
int j;

public void go(){
j=i; //line 7
System.out.println("value of j :"+ j);
System.out.println("value of i :"+ i);
}

public static void main(String[] args) {
Ex obj=new Ex();
obj.go();

}

}
/[code]
in code above i set Integer i=0;
then unboxing will occur at line 7.
in your code Integer i is null so there is NullpointException.
Boxing will occur when Integer i will refer to some Object.
But in your code Integer i donot refer to any object so boxing will not occur.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in code above i set Integer i=0;
then unboxing will occur at line 7.
in your code Integer i is null so there is NullpointException.
Boxing will occur when Integer i will refer to some Object.
But in your code Integer i donot refer to any object so boxing will not occur.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic