my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Doubt" Watch "Casting Doubt" New topic
Author

Casting Doubt

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Hello Friends,

I came across this weird observation(or may be,i'm the only one finding this weird,point me out if that is the case)

In java,

we can write the following:
(until the int is well in range of byte)

even though it is an int and no explicit casting is required (like the one required when you write: (even though 14 is well in range of int))

The same is true for short and char.


However if i write a code like this:


i get compilation error saying : fount int but required short....

Why is it the case?

You can assign int(well in range of short) directly to a short variable but you can't pass an int to a short variable, what does that mean? Why is the contradiction coming?

Does it mean rules of assignments and rules of passing values between functions are different.

If that is the case, it is disguisting, i'm studying java and i have found so much of such things, do i need to remember them as exception....

Help!!!


Waiting for enlightenment....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Prateek Rawal wrote:
You can assign int(well in range of short) directly to a short variable but you can't pass an int to a short variable, what does that mean? Why is the contradiction coming?

from JLS:

a narrowing primitive conversion may be used if all of the following conditions are satisfied:

The expression is a constant expression of type byte, short, char or int.
The type of the variable is byte, short, or char.
The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable

but in case of method calls, compiler dont do the implicit narrowing conversion of the argument value .
from JLS:

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion (ยง5.2). The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process


hth
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
That means my observation is correct....

and there lies difference in the assignment rules and method invocation rules, and i should remember it as another fact(huh..... )

Is that what i should conclude?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

yes
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
You should not do because iff they allowed the same rule in method argument passing mechanism then
solving method overloading questions would become


SCJP 6.0 96%

(Connecting the Dots ....)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sahil Kapoor wrote:

what makes you to do this?
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Thanks everyone for taking your time off and enlightening me!!!

Thanks.....
ravi keshri
Greenhorn

Joined: Jun 30, 2010
Posts: 3
hiiiiiiiiiii friend At first i want to thank you to raising this topic ..........
I CAN CLEAR YOU CONFUSION.........
AT --------int x=14L;
this line is right no problem as we know
but when we call a method(short x) with value method(7) then our parameter is 7 which is int .At first jvm search method with parameter int jvm not found then jvm try to match but by widening,boxing,var-args its not found so,it gives compile error and here is not the assigment here is used polymorphism
HAVE A GOOD DAY...............
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
int i = 14L; //Compilation Error

This line is not right, explicit casting is required as shown below:

int i = (int)14L;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Casting Doubt
 
Similar Threads
Assingment and casting
int -> float -> int and loss of precision
Cast compiler error
range of values
few scenarios in JAVA where instinct doesn'n play right