File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what it means Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what it means" Watch "what it means" New topic
Author

what it means

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 15, 2006
Posts: 25
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

Joined: Dec 28, 2006
Posts: 81
Read this

http://faq.javaranch.com/view?ScjpFaq#mostSpecific


SCJP 1.5<br />SCWCD 1.4
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what it means
 
Similar Threads
Doubt in Sierra Bates chapter 3
Exam Lab Test 2 Question
Study Guide K&B 1.5: Pg 243
Method matching.
var args method