• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short Constructor (Dan's Q)

 
Cathy Song
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I dont understand why line 2 says parameter is an int? Notice, line 1 works just fine.
Thanks.
[ October 23, 2003: Message edited by: Cathy Song ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's because both operands are automagically promoted to an int, so the result is an int.
�5.6.2 Binary Numeric Promotion

If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.

so, you need to change your code to something like this:
Short obj2 = new Short( (short) (1-2) ); //line 2
 
Doug Dunn
Author
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the previous responder is exactly right. You are casting the operands which must be fully evaluated before the binary operation can be performed. Thus your cast operators are meaningless. If you cast the result instead of the operands it will work.
 
Cathy Song
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jessica and Doug.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic