• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

cant assign final long to byte

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final int X=100;
byte b=X;
the above two statements do not give any compile error.but
final long X=100;
byte b=X;
gives a compile time error..I can,t imagine why?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madushika. Welcome to the Ranch!

The first example you give is explicitly allowed in the Java Language Specification - see here. It says:

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.


But you'll notice it doesn't mention long, so that isn't allowed. Why? I can't tell, other than to say it's hard to think of a good reason why you'd need it.
 
Marshal
Posts: 76887
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe they forgot to allow longs. It is likely, however, there is a reason for that omission; presumably they thought that longs, which are intended for larger numbers, are unlikely to be used as bytes. Unless you meet one of the original designers and ask them, you will probably never know.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I've been writing Java code since 1996 and have helped out on Java forums since 2005 and I've never noticed that before now, which just goes to show:
1. You learn something new every day
2. Matthew is correct when he says "it's hard to think of a good reason why you'd need it".
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Wow, I've been writing Java code since 1996 and have helped out on Java forums since 2005 and I've never noticed that before now, which just goes to show:
1. You learn something new every day



'98 and '99 for me, but otherwise, ditto.


 
Water! People swim in water! Even tiny ads swim in water:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic