• Post Reply Bookmark Topic Watch Topic
  • New Topic

initializing primitive variables with numeric literals  RSS feed

 
Richard Legué
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I initialize a byte or short variable with a numeric literal - for instance 'byte x = 100', the compiler doesn't complain although a numeric literal is seen by de compiler as an int-type. I don't need a cast to make this initialization work. But when I'm passing a numeric literal as a parameter to a method that takes a byte, the compiler complains about 'incompatible types'. for instance - 'public void takeArgs(byte x){ }' called by 'takeArgs(100);' makes the compiler complain. Changing the method call in 'takeArgs (byte 100) makes the code compile. I'm confused; both examples have to do with initialization (right?) but are treated differenly. Can somebody tell me why this is? Thanks
 
Pete Letkeman
Ranch Foreman
Posts: 914
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the value  that is passed to the method when you do this? You may be surprised.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Conversions for assignments are defined by section 5.2 of the Java Language Specification ... https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

Conversions for Method calls (passed values) are defined by section 5.3 of the JLS ... https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3

Noticed that there is a part regarding "compile time constants" in section 5.2, but this part does not exist in section 5.3?

Anyway, it does make sense right? If you are the compiler, and you are generating code for the method, how the heck are you going to detect that it is a compile time constant? The calling code may be in a completely different class, may be in many classes, and may even be in classes that haven't been written yet.

Henry
 
Richard Legué
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you help. Learning all the time!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:

There is no casting happening on line 5: You're simply declaring a byte type variable and initializing it with a literal value. The conversion for assignments that Henry referred to above is what determines whether the line will compile or not. The compile-time error that occurs if you tried to initialize it with the literal 300 is because the compiler recognizes that 300 can't be represented as a byte, not because it can't cast it to a byte.

If you cast the value, it will compile but it won't necessarily give you the same value:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!