151

I am writing a little code in J2ME. I have a class with a method setTableId(Short tableId). Now when I try to write setTableId(100) it gives compile time error. How can I set the short value without declaring another short variable?

When setting Long value I can use setLongValue(100L) and it works. So, what does L mean here and what's the character for Short value?

Thanks

Gilberto
  • 893
  • 1
  • 13
  • 28
Mubashar
  • 12,300
  • 11
  • 66
  • 95

4 Answers4

235

In Java, integer literals are of type int by default. For some other types, you may suffix the literal with a case-insensitive letter like L, D, F to specify a long, double, or float, respectively. Note it is common practice to use uppercase letters for better readability.

The Java Language Specification does not provide the same syntactic sugar for byte or short types. Instead, you may declare it as such using explicit casting:

byte foo = (byte)0;
short bar = (short)0;

In your setLongValue(100L) method call, you don't have to necessarily include the L suffix because in this case the int literal is automatically widened to a long. This is called widening primitive conversion in the Java Language Specification.

Bren
  • 605
  • 4
  • 15
Lauri
  • 4,670
  • 2
  • 24
  • 17
  • 20
    There are suffixes for other types as well: `d`/`D` makes a `double` and `f`/`F` makes a float! – Joachim Sauer Feb 19 '10 at 08:48
  • 7
    Also: literals that fit into the size don't need to be cast: your two examples work without the cast as well. – Joachim Sauer Feb 19 '10 at 08:49
  • You are right on both. I should have been more clear that I'm talking about integer literals here, not about floating-point literals. – Lauri Feb 19 '10 at 08:55
  • 4
    @Joachim: No cast required only for J5+; J2ME is unfortunately still at J4 (a seriously stripped down J4). – Lawrence Dol Feb 20 '10 at 04:55
  • 3
    @JoachimSauer, what does "fit into the size" mean? I'm asking because I just had to specifically cast `0` as `(short)0` to get around a `possible lossy conversion from int to short` error, even though 0 is a short. – ryvantage Oct 27 '16 at 01:55
  • @ryvantage What Java version? – Koray Tugay Nov 21 '16 at 05:02
40

There is no such thing as a byte or short literal. You need to cast to short using (short)100

er4z0r
  • 4,711
  • 8
  • 42
  • 62
22

Generally you can just cast the variable to become a short.

You can also get problems like this that can be confusing. This is because the + operator promotes them to an int

enter image description here

Casting the elements won't help:

enter image description here

You need to cast the expression:

enter image description here

matt burns
  • 24,742
  • 13
  • 105
  • 107
  • 2
    Don't forget there's a reason why short + short = int. If the sum of two shorts is higher than the maximum short value of the machine, casting it to short will either produce unexpected results or throw an exception, if supported by languaje. – DGoiko Jan 25 '20 at 19:48
  • 19
    With that logic, adding two ints would return a long ;) – matt burns Jan 26 '20 at 08:17
  • 1
    And I look for that if my sums can surpass Integer.MAX_VALUE. I'm not defending the decision of returning int (which is probably linked to the way the HW actually performs the sums), i'm just saying people should confirm that the result actually fills in a short before puting it there. I've encountered more bugs capping short capacity than capping int aswell, probably due to the size of the numbers involved. 2 tiny bytes java reservers for short cap up really fast – DGoiko Jan 26 '20 at 17:11
  • 1
    I readed my comment again and I didn't make my point clear. The first sentence is confusing and I should have not said that and just left the rest, it looks like there are two related statements.The reason behind short + short being int can be read here: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.11.1 , there are no sum operations for short in JVM. Java's int is 32 bits, and when this deccisions where made, most computers were 32 bits, so int looked like the best idea, I guess. – DGoiko Jan 26 '20 at 17:19
  • 1
    Also note that, as explained here https://stackoverflow.com/a/27123302/9465588 , JVM did actually serverse at least 32 bits of its virtual memory space per class field, so declaring short fields does not save memory at all. I don't know if Java changed this. I tend to never use short values unless tere's an external restriction involved, like in a DAO – DGoiko Jan 26 '20 at 17:28
10

You can use setTableId((short)100). I think this was changed in Java 5 so that numeric literals assigned to byte or short and within range for the target are automatically assumed to be the target type. That latest J2ME JVMs are derived from Java 4 though.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
  • PS: Welcome to the stuck in the middle-ages pain of coding for J2ME. Can't wait for hand-held devices to catch up to year 2000 desktops. – Lawrence Dol Feb 19 '10 at 08:42
  • 3
    There is no "J4" (and no "J5". Please don't make the Java versioning/naming scheme any more confusing than it already is. – Joachim Sauer Feb 20 '10 at 07:22
  • 2
    @Joachim: Java 1, Java 2, Java 5, Java 6 and Java 7 are well known and are so referred; it's not too hard to extrapolate what would be meant by Java 3 and Java 4. "Jn" is simply an abbreviation of the obvious. Adopting Sun's current (and hopefully final) nomenclature for all versions reduces confusion. – Lawrence Dol Feb 20 '10 at 07:48
  • Then please tell me what exact version is "Java 2"? – Joachim Sauer Feb 20 '10 at 08:47
  • 2
    @Joachim: According to Sun's last word on the subject, the leading "1." of Java "1.x" is to be treated as if it were never there when referring to versions in discussion, and is being retained in the version emitted by JVMs only for compatibility. Thus, Java 2 is that version which was previously known as 1.2, from where J2SE originally came (you'll note that at the same time Sun recommended no longer using J2xE, but rather JavaEE, JavaSE and JavaME). It follows that 1.3 is Java 3, 1.4 is Java 4, 1.5 is Java 5, 1.6 is Java 6 and 1.7 is Java 7. Seriously, this is not that hard to reason out. – Lawrence Dol Feb 20 '10 at 09:07
  • 2
    The "1." was *only* cut of in Java 5 and later. Java 1.0-1.4 are *always* refered to by that name by Sun. And that is done intentionally, because Sun used "Java 2" to refer to Java 1.2 up to Java 1.5/Java 5. It is very confusing, but inventing new names that Sun never used doesn't make it any easier. – Joachim Sauer Feb 20 '10 at 10:20