wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting error" Watch "casting error" New topic
Author

casting error

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
CertPal fundamentals


William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Eclipse will tell you that those are int parameters.

Your signature defines shorts.

WP
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
How do I cast that to short?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i think the integer literals in java default to integer type(int). so even thought all those numbers are in the range of short they still are of the type int.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

Janki Shah wrote:How do I cast that to short?


By using the explicit cast operator --- meaning ..... "(short)" in front of the value / expression that you want to cast to short.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

gurpeet singh wrote:i think the integer literals in java default to integer type(int). so even thought all those numbers are in the range of short they still are of the type int.


There is a rule regarding compile time constants being implicitly casted from int to short, provided that they are in range. However, this rule doesn't apply to method call conversions, so this is why it doesn't work here.

Henry
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Henry Wong wrote:
Janki Shah wrote:How do I cast that to short?


By using the explicit cast operator --- meaning ..... "(short)" in front of the value / expression that you want to cast to short.

Henry


Yes, it worked this way.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: casting error
 
Similar Threads
Inner-Outer class problem
Why is autoboxing not happening
empty try block
Method overloading using a wrapper and primitive var-args long methods
Foo6 compilation