Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what it means

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:

public class Boxing2 {
public static void main(String[] args) {
byte b = 10;
method(b);
}
static void method(int i){
System.out.println("Primitive Type call");
}
static void method(Integer i){
System.out.println("Wrapper Type Call");
}
}

The output is :
Primitive Type call

Consider this code:
public class Boxing3 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Long l){
System.out.println("Widening conversion");
}
}

Output is: compiler error

In the first code, a byte is widened to a int but in the second code why can't a int be widened to long???

Any explanation appreciated.

Regards,
Guru
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guru dhaasan:
Consider the following code:

public class Boxing2 {
public static void main(String[] args) {
byte b = 10;
method(b);
}
static void method(int i){
System.out.println("Primitive Type call");
}
static void method(Integer i){
System.out.println("Wrapper Type Call");
}
}

The output is :
Primitive Type call

Consider this code:
public class Boxing3 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Long l){
System.out.println("Widening conversion");
}
}

Output is: compiler error

In the first code, a byte is widened to a int but in the second code why can't a int be widened to long???

Any explanation appreciated.

Regards,
Guru


In the first case, a byte can be widened to an int without any problem.

In the second case, because the parameter of the method is an object reference, first the int is boxed into an Integer.

An Integer can't be sent to a method that expects a Long because Integer does not have the "is a" relationship with Long.
 
ramesh kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static void method(int i){
System.out.println("Primitive Type call");
}
static void method(Integer i){
System.out.println("Wrapper Type Call");
}
}

here why first method called is java gives preference to old features when there is conflict b/w new and old features
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this

http://faq.javaranch.com/view?ScjpFaq#mostSpecific
 
JB Ramesh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this code:
public class Boxing3 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Long l){
System.out.println("Widening conversion");
}
}


The rule here is "primitive values should be boxed and then widened. Not widened and then Boxed"

int i is boxed into Integer. And Integer tries to find method having Integer as parameter or widened reference(eg: object) as paramter. As it cant find method,compilation fails.
[ March 21, 2007: Message edited by: JB Ramesh ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic