• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting error

 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CertPal fundamentals


 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse will tell you that those are int parameters.

Your signature defines shorts.

WP
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I cast that to short?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic