• 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

Literal Assignment for following Example

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am Shivey Upadhyay, I am preparing for SCJP1.5 and have some problems with the following code:

class LiteralTest {
public static void main(String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8
}}

I know Short is 16 bit signed and under the hood char is also 16 bit unsigned int.
They get conerted into int defaultly.
but assigning an Int value without explicit cast to a byte does not give Compiler Error in Line //3 because s1 is marked final.
what difference final make in this case?
If i remove final, the behaviour at Line //3 will become as required?

Thanks,
Shivey
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because s1 is final, it is a compile-time constant. The compiler knows that it's a fixed value that can't change, so the compiler can already check if the value fits in a byte (it does, so you don't get an error message).

If s1 is not final, then it's not a fixed value that's known at compile time, so the compiler can't check if it can be converted to a byte without loosing data.
 
Shivey Upadhyay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jesper Young
But, I am sorry, i want to confirm it.

According to this property,
final int x=2;
byte b =x;//will compile
i.e., the value of x, whether it is int, short or char the value should be between -128 to127 and the value will be assigned to b?
Am i correct?

Thanks,
Shivey
 
Shivey Upadhyay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jesper Young
But, I am sorry, i want to confirm it.

According to this property,
final int x=2;
byte b =x;//will compile
i.e., the value of x, whether it is int, short or char the value should be between -128 to127 and the value will be assigned to b?
Am i correct?

Thanks,
Shivey
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the value of x, whether it is int, short or char the value should be between -128 to 127 and the value will be assigned to b?
Am i correct?


Thats correct, otherwise you ll get a Compiler error "possible loss of precision"
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic