• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Java Primitive Literals

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have verified the following statements and results:
float f1 = 1.0 --> compile error
byte b1 = 2 --> compiles and runs
byte b1 = 129 --> compile error
I understand that a literal of:
1 --> is interpreted as an int
1.0 --> is interpreted as a double
and further, that 'downcasting' requires a 'cast' opertion.
Why then, does the statement: byte b1 = 2
not generate a compile error also?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case that you use a literal to initialize a variable smaller than an int (in this case, a byte), the compiler will check to see if that literal is within the range of the variable and only cause a compiler error if the value is out of range. This is why, when yo assign the value 1 to a byte, it works and when you assign 128 to a byte, it fails (128 is outside the valid range of a byte). Java does the same thing with final variables. This code causes a compiler error:

While this code compiles and runs perfeectly:

I hope this helps,
Corey
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because as long as the literal fits within the range of the primitive type (2 is within the byte range -128,127) the compiler won't complain.
[ April 17, 2002: Message edited by: Valentin Crettaz ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per the JLS, §5.2 Assignment Conversion:


In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
- The expression is a constant expression of type byte, short, char or int.
- The type of the variable is byte, short, or char.
- The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.


So, there's your answer in black and white.
Corey
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Randy,

float f1 = 1.0 --> compile error


Just for formality, from Sun's Java tutorial: "A series of digits with a decimal point is of type double. You can specify a float by putting an 'f' or 'F' after the number."

byte b1 = 129 --> compile error


Ditto with Corey and Valentin. But let's just reminisce about the basics: how do you determine the range? Since a byte has a size of 8 bits, you can determine it manually by multiplying 2 (a bit has 2 possible values, "1" or a "0", or "true" or "false") eight times (or 2 raised to the power of eight), which equals 256. Thus the range of values for the byte data type is -128 to 127 inclusive, which does not include 128. Why just 127? because you have to sneak in the special number "0" to the range (...,-1, 0, 1,...).
Ex Animo Java!
-- Val
[ April 17, 2002: Message edited by: Val Pecaoco ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic