File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
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: 859

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: 19060
    
  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: 19060
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting error