File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Doubt

 
Prateek Rawal
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not do because iff they allowed the same rule in method argument passing mechanism then
solving method overloading questions would become
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:

what makes you to do this?
 
Prateek Rawal
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for taking your time off and enlightening me!!!

Thanks.....
 
ravi keshri
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 14L; //Compilation Error

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

int i = (int)14L;
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic