168

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

1
  • L is just a suffix used to indicate a long literal. Commented Feb 19, 2010 at 8:45

4 Answers 4

252

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.

Sign up to request clarification or add additional context in comments.

6 Comments

There are suffixes for other types as well: d/D makes a double and f/F makes a float!
Also: literals that fit into the size don't need to be cast: your two examples work without the cast as well.
Not correct in Java 8. A method that takes a short requires you to cast a literal.
You are right on both. I should have been more clear that I'm talking about integer literals here, not about floating-point literals.
@Joachim: No cast required only for J5+; J2ME is unfortunately still at J4 (a seriously stripped down J4).
@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.
42

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

Comments

30

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

5 Comments

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.
With that logic, adding two ints would return a long ;)
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
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: docs.oracle.com/javase/specs/jvms/se8/html/… , 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.
Also note that, as explained here 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
11

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.

6 Comments

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.
There is no "J4" (and no "J5". Please don't make the Java versioning/naming scheme any more confusing than it already is.
@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.
@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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.