• 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

Java literal and assignment to different primary types

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 21
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Dean Nillson
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic