• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting Doubt

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Kapoor wrote:


what makes you to do this?
 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for taking your time off and enlightening me!!!

Thanks.....
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic