• 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

Help with question 3 in chapter 3 from k&B book

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:
class Alien{
String invade(short s){ return "a few";}
String invade(short... s){ return "many";}

class Defender{
public static void main(String args[]){
System.out.print(new Alien().invade(7));
}

}


Answer :Compilation fails . 7 needs to be explicitly cast to short


7 is a compile time constant that we are passing to the method. When the compiler can implicitly cast an int literal during assignment( short s=7; is legal) why cannot it do the same in the above scenario.Why do we need an explicit cast? Could somebody please clarify my doubt
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you write short s=7 ,it is assignment not casting .But when you directy passing 7 then compiler will always treat it as int.It is the default case.But when you declare it then there is no problem.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

7 by default is considered as an int and it is not able to cast it to short.

Because of this, you are getting an error.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandhya,

When there's a method call, the compiler's job is to match the right method for the call, and in this case it looks for a invade(int) method.
If its unable to find it, it knows that a invade(long) can accomodate it and looks for that... if it doesn't fond that too it knows that a int Integer can accept it and looks for a invade(Integer)...

To put it in short, compiler looks for a method prototype that can for sure accept the value passed... And it never regards a invade(short) as a match for an int value as its not sure whether it can accomodate the int being passed without problems...

In the noraml case, short a=7; its like we are sayin the compiler, "I know what i'm doing, just do it..."
Even in this case, when it exceeds beyond 128 (i think) the compiler shouts out an error...


HTH,
Vishwa
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandhya Bhaskara welcome to Javaranch ,

In short, integer literals are by default int according to the Java language semantics.And narrowing requires an explicit cast.
This is similar as floating point numbers are double by default.


Hope this helps
[ October 03, 2008: Message edited by: Amit Ghorpade ]
 
Sandhya Bhaskara
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for clearing my doubt
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't mean to be mean or anything, but it might just help if you try the code yourself on your editor. You can even explore further and it might just help you for your exam.

Hope this helps your (AND I DIDN'T MEAN TO BE MEAN),
Abhinav
 
Sandhya Bhaskara
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhinav,

I tried to compile this on eclipse and it did gave me the error. My question was WHY the compiler gave an error when it could have cast the literal 7 implicitly to short and people have already clarified my doubt.

Thank you
Sandhya
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Be VERY careful if you use an IDE when studying for the SCJP - we STRONGLY recommend that you do your studying using the command line! All the code for the test was created and tested entirely from the command line.

hth,

Bert
 
reply
    Bookmark Topic Watch Topic
  • New Topic