• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

int as expression value

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because that's what they are. Integer literals are integers. How do you propose the JVM determines whether they should be bytes or shorts?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic