Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

initializing primitive variables with numeric literals

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the value  that is passed to the method when you do this? You may be surprised.
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you help. Learning all the time!
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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