Setting a short Java value

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

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

thank

+66
java primitive-types literals
Feb 19 '10 at 8:36
source share
3 answers

In Java, integer literals are int if they do not have the letter β€œL” or β€œl” (capital L is preferred because lowercase L is difficult to distinguish from number 1). If the suffix is ​​with L, literals are of type long.

The suffix does not have a special name in the Java Language Specification. There are also no suffixes for any other integer types. Therefore, if you need a short or byte literal, they should be stripped:

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

In setLongValue (100L), you do not have to include the L suffix, because in this case int literal automatically expands to a long one. This is called the extension of primitive conversion to the Java language specification.

+102
Feb 19 2018-10-19T00
source share

There is no such thing as a byte or a short literal. For a short press you need to use (short)100

+22
Feb 19 '10 at 8:39
source share

You can use setTableId((short)100) . I think this has been changed to Java 5, so that numeric literals assigned to a byte or short and within a range for the target are automatically considered the target type. However, the latest J2ME JVMs are based on Java 4.

+7
Feb 19 '10 at 8:37
source share



All Articles