Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ranch roundup game question

 
Jas Oberai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • 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!!!
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Karthikeyan Varadarajan
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James and BlackFire,
So,that only happens for narrowing and not widening..got it!!!
thanks
 
Raghu Shree
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James and blackfire,
Nice Example. I got the point.
[ May 06, 2005: Message edited by: Raghu Shree ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that you can do this...



But, you can't do this...

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic