my dog learned polymorphism
The moose likes Beginning Java and the fly likes int as expression value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int as expression value" Watch "int as expression value" New topic

int as expression value

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84

here b and c are byte but the expression (b+c) returns value as int
why the byte values are automatically converted to int..why not operate in
byte as well

"I Love Java Ranch"
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15036

Same reason as in your other question; the declaration with assignment works because this is a special case. But the value that you are assigning must be a compile-time constant. The b + c expression in line 3 is not a compile-time constant.

And (byte)b + c doesn't work. The cast has a higher precendence than the + operator, which means that (byte)b + c is the same as ((byte)b) + c. So you cast b to byte (which is unnecessary because it is already a byte), then the + happens which returns an int. To make it work, you must cast the result of the + operator to byte by putting parentheses around the expression:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
thanks for your answer and correction in my question...

but why the integer literals are automaticlly converted to int by default..why not byte,short,long
Rob Spoor

Joined: Oct 27, 2005
Posts: 20180

I guess that's a design decision of the Java developers. In the same line of thinking, you could ask yourself these questions:
- Why are literals from -128 to 127 int literals and not byte literals?
- Why are literals from -32768 to 32767 (excluding those from -128 to 127) int literals and not short literals?
- Why do long literals need an L? Any literal outside the range of int but inside the range of long could be long automatically.

In other words, the type of integer literals could have been dynamic, based on the range. The developers decided not to do this but use int as the default type, for both literals and mathematical expressions. That's something you'll now have to live with.

How To Ask Questions How To Answer Questions
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4199

Because that's what they are. Integer literals are integers. How do you propose the JVM determines whether they should be bytes or shorts?

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20180

The range? There is a difference between integer (every whole number) and int.
For any given value x you could have the following type mapping (pseudo code):

Oh, and it isn't the JVM that should determine the size, it's the compiler. During runtime the variable containing the value determines the type.
I agree. Here's the link:
subject: int as expression value
It's not a secret anymore!