14

All numbers in Java are supposed to be of int type. The following line is legal in Java>1.5

Short s = 1; // Will compile to Short s = Short.valueOf((short)1) - thus you can't exceed short max value i.e.
Short s =  4444; // is invalid for autoboxing

Same mechanics go for Integer and Byte instantiation. But Long works completely different. The following code gives compile time error

Long l = 10;

Long uses the same approach for autoboxing of long types, so

Long l = 10L; //is valid and is translated into Long.valueOf(10L)

I can't see why int cannot be assigned to a Long variable. Any thoughts on this matter?

3
  • 1
    Yeah, i understand that Long is wrapper type while int is a primitive. The question concerns autoboxing itself. Why doesn't compiller make Long l = Long.valueOf(1); or Long l = new Long(1); while it can do this for Short and Byte wrapper types? Commented Oct 14, 2011 at 9:58
  • 1
    "All numbers in java are supposed to be of int type." ...what? Commented Oct 14, 2011 at 11:31
  • actualy, i meant: if you write somevar = 10 - 10 is implicitly int same goes for statement byte a = 10 which actualy turns into byte a = (byte) 10 where 10 is actualy of int type Commented Oct 14, 2011 at 11:49

3 Answers 3

10

I think the question was not about casting primitives and wrappers in general. The question was about difference between casting int to java.lang.Long and int to java.lang.Short for example.

JLS: "In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int:

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
  • A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
    • Byte and the value of the constant expression is representable in the type byte.
    • Short and the value of the constant expression is representable in the type short.
    • Character and the value of the constant expression is representable in the type char".

So all <=32bit primitives can be casted easily and long (64bit) requires special casting. It seems illogically.

All illogical things as usual has explanation in backward compability or historical evolution in java. E.g. classes Integer and Long exist in java since version 1.0. Classes Short and Byte exist in java since 1.1. That is at the start point integral number can be two types: integer or long. So I think there are different casting rules for these two types of numbers. And then short and byte were added. I suppose short and byte can have 32-bit implementation in concrete JVMs.

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

Comments

1

Because Long with the first capital letter is a wrapper class and not the primitive type .

Take a look here .

1 Comment

Short is a wrapper class too. This does not address the difference in behavior.
1

You can cast int to long and long to Long
but you can't cast int to Long

it is correct to write Long l = (long) 10;

1 Comment

But you can assign Short this way. The question is: Why the difference?

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.