Take the 2-minute tour ×

I am writing a little Code in J2ME I have a class with a function 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

like to set Long value i can use setLongValue(100L) and it works. Now Whats "L" is called here and what character is for Short value.


share|improve this question
L is just a suffix used to indicate a long literal. –  missingfaktor Feb 19 '10 at 8:45

3 Answers 3

up vote 35 down vote accepted

In Java integer literals are of type int unless they are suffixed with letter 'L' or 'l' (Capital L is preferred as lower case L is hard to distinguish from number 1). If suffixed with L, the literals are of type long.

The suffix does not have any special name in the Java Language Specification. Neither is there suffixes for any other integer types. So if you need short or byte literal, they must be casted:

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

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

share|improve this answer
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 8:48
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 8: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 8:55
@Joachim: No cast required only for J5+; J2ME is unfortunately still at J4 (a seriously stripped down J4). –  Lawrence Dol Feb 20 '10 at 4:55

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

share|improve this answer

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.

share|improve this answer
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 8:42
There is no "J4" (and no "J5". Please make the Java versioning/naming scheme any more confusing than it already is. –  Joachim Sauer Feb 20 '10 at 7:22
@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 7:48
@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 9:07
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

Your Answer


