Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Question on Boxing

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question 1)

public class Boxing1 {

public static void main(String[] args) {
Integer i = null;
method(i);
}
static void method(int k){
System.out.println(k);
}
}

What is the output of the above program?

Question 2)

public class Boxing1 {
public static void main(String[] args) {
Integer i = new Integer("7");
method(i);
}
static void method(int k){
System.out.println(k);
}
}

Question 3)

public class Boxing1 {
public static void main(String[] args) {
Integer i = new Integer();
method(i);
}
static void method(int k){
System.out.println(k);
}
}

What is the output of the above program?




These three questions are similar except one line is changed while declaration of Integer Type.

Can any one explain the differnence ?

Thank You in Advance
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first example should raise NullPointerException at the place where you tried to call the static method. The value of 'i' is set to null. While java tries to fetch the integer value from the null Object for you, it will raise an exception.

The second example should print 7, it's quite obvious.

For the third example, if you look at carefully to the code, it will give a compilation error because the constructor you used (new Integer()) is not available.
 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will Happen

If we change

Integer i=null;

with

Integer i= new Ineger(0);
i=null;

See in first case no object is made at all..In Second object is created using 'new' and then reference is set to null
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case also same null pointer exception because here we are trying to access wrapper class reference of value null so it will through null pointer exception. May be i am not so clear but its the result.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First place, whether these programs would get compile ? Put some light on it.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Compiles throws Nullpointer excepttion at method call
2. Compiles and runs (o/p 7 as expected)
3. Doesnt compile(constructor doesn't exist)
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic