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

scjp5.0 question

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Alien {
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
}
class Defender {
public static void main(String [] args) {
System.out.println(new Alien().invade(7));
}
}

What is the result?

many

a few

Compilation fails.

The output is not predictable.

An exception is thrown at runtime.

the answer is compilation fails
but why do we have to cast the invade(7) to short
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During method call 'invade(7)' must be cast to 'short' because while parameter passing no implicit type conversion occurs.

here 7 is an 'int' type and invade(7) is read by compiler as 'invade(int)'.

So passing an 'int' arguement to a 'short' variable cannot have implicit type conversion. Explicit cast is a must.
[ November 21, 2007: Message edited by: Mohit Jain ]
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nikunj,

I believe you posted the exact same question a few days ago. You may wish to check the replies in that thread:

https://coderanch.com/t/266326/java-programmer-SCJP/certification/help-scjp

Also, please note Bert's request that you quote your sources. Thanks.
[ November 21, 2007: Message edited by: Kelvin Lim ]
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic