• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java literal and assignment to different primary types  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought numeric literal were by default int or doubles, depending on if have a . and numbers after the .
But I wrote a quick test program as listed below

I understand the float float floatA = 5.5; failed to compile since 5.5 is a literal of type double and you are trying to assign this to a floag

What I am having problems with is byte byteA = 5;
5 is a literal of type int and this is being assigned to a byte and compiler should complain.
The compiler does not allow two byte values to be added and assigned to a byte since the result of the addition is an int



class literalTesting{
public static void main(String[] arg){
byte byteA = 5; // allowed WHY I thought literal is an int and assigning int to byte
byte byteB = 10; // allowed

//byte bytec = byteA + byteB; // NOT ALLOWED byte + byte results in an int value trying to assign to byte
// ERROR incompatible types: possible lossy conversion from int to byte

short shortA = 5; // allowed
char charA = 5; // allowed

int intA = 5; // allowed

long longA = 5; // allowed

//float floatA = 5.5; // not allowed literal 5.5 returns a double - can not assign double to float without cast or F suffix
float floatA = 5.5F;
float floatB = (float)5.5;



}
 
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is simply because the JLS says that's the way it works, see http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2.

The important bit in your case is:
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.

Or put simply the compiler can auto downcast the expression if it is a constant of type byte, short, char, or int and it is being assigned to a variable of type byte, short, or char and it will fit into that variable. ie 127 can be assigned to a variable of type byte but 1024 can't be.
 
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Or put simply the compiler can auto downcast the expression if it is a constant of type byte, short, char, or int and it is being assigned to a variable of type byte, short, or char and it will fit into that variable. ie 127 can be assigned to a variable of type byte but 1024 can't be.


Do you know in which version of Java this automatic casting was added? I was expecting to have to cast the literal to short.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what the Java 1.0 JLS says about that topic: http://titanium.cs.berkeley.edu/doc/java-langspec-1.0/5.doc.html#170768

I invite you to compare the two and see if there are any differences. I wouldn't think there were, since language features related to primitives haven't changed at all as far as I know. There's boxing and unboxing, and there's parameter typing, but I don't believe any of those should affect how primitives work. The Java designers are quite careful about forward compatibility between versions.
 
D Nobels
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Here's what the Java 1.0 JLS says about that topic: http://titanium.cs.berkeley.edu/doc/java-langspec-1.0/5.doc.html#170768

I invite you to compare the two and see if there are any differences. I wouldn't think there were, since language features related to primitives haven't changed at all as far as I know. There's boxing and unboxing, and there's parameter typing, but I don't believe any of those should affect how primitives work. The Java designers are quite careful about forward compatibility between versions.


Thank you Paul. I guess my memory is failing me. It has been this way indeed.

Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!