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
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,Fto 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:
In your
setLongValue(100L)method call, you don’t have to necessarily include theLsuffix because in this case the int literal is automatically widened to a long. This is called widening primitive conversion in the Java Language Specification.