[java] Setting Short Value Java

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

This question is related to java literals primitive-types

The answer is


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


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.


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