• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Ranch roundup game question

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I got this question:

But,I answered "No",as I read this in the JLS specification:

The also explained that it was done so as not to confuse regarding overloading.So,why the answer was "Yes",please explain this to me guys!!!
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not "narrowing" when you go from short to int. It would be narrowing when you go from int to short.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can send int while the funtion expects int. but the vice versa is true.

while coming to overloading ,

the compiler try to match the argument type and if it is not find a correct method , then try to widen the values and match (short to int or int to long).

<code>
public class OverLoadTest {

public static void main(String[] args) {

OverLoadTest test = new OverLoadTest();
short ding = 5;
test.checkOverLoad(ding);

}


/* public void checkOverLoad (int one)
{
System.out.println("CheckOverLoad for int called");
}*/

/* public void checkOverLoad (short two)
{
System.out.println("CheckOverLoad for Short called");
}*/

public void checkOverLoad (long three)
{
System.out.println("CheckOverLoad for Long called");
}
}

</code>
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James and BlackFire,
So,that only happens for narrowing and not widening..got it!!!
thanks
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James and blackfire,
Nice Example. I got the point.
[ May 06, 2005: Message edited by: Raghu Shree ]
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi james

i didn't understand what the jls saying Method Invocation Conversions do not include implicit narrowing of integer constants as in Assignment conversions.

i learnt that in assignment coversion. we can assign int to short i.e narrowing on explicitly casting.
i.e Short s=(Short)5;

is the jls saying the above conversion is not allowed during the method invocation.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means that you can do this...



But, you can't do this...

reply
    Bookmark Topic Watch Topic
  • New Topic