This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Short literal representation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Short literal representation" Watch "Short literal representation" New topic
Author

Short literal representation

Greg Werner
Ranch Hand

Joined: May 07, 2009
Posts: 54
Short and sweet. The following obviously does not compile.



Other than casting line 3 RHS to be a short, is there any other notation I can use for a valid line of code (such as 14s which obviously is not Java).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

I believe this works:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Casting is the only option. The reason is because the result of any mathematical operation is at least int, never short, char or byte.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
One of the main rules in Java is that any mathematical operation's result will be stored in a large size variable to avoid truncation. For example if you are adding int with long the result will be long. Hence, any operation on byte, char, or short will result an int even if you added 1 to a byte.There are 2 ways to store the result in the same data type:

a) you do explicit casting:

b) You can use the auto increment of short hand operations to ask the JVM to do implicit casting:


OR
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Mina Daoud wrote:One of the main rules in Java is that any mathematical operation's result will be stored in a large size variable to avoid truncation. For example if you are adding int with long the result will be long. Hence, any operation on byte, char, or short will result an int even if you added 1 to a byte.

That's true, but even the following will fail:
Because like I said, the result is never smaller than int.
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
Rob Prime wrote:
That's true, but even the following will fail:
Because like I said, the result is never smaller than int.


Yea, that's why i mentioned if you added 1 to a byte the result will be int

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

That case is slightly different since technically, 1 is an int value.
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
Yes I agree with you in that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Short literal representation
 
Similar Threads
Doubts with parameter assignment
Primitive Casting
wrapper
Short Constructor (Dan's Q)
implicit and explicit conversion