• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Type Conversion in Assignment Context

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
Extract from Khaled Mughal:-
Implicit Narrowing conversions can occur when :-
1. The source is a constant expression of either byte,short,char or int .
2. The target type is either byte, short or char .
3. The value of the source is determined to be in the range of the target type at compile time .

Now in accordance to the above rules:-
int i2=-20; (1)
final int i3=i2; (2)
final int i4=200; (3)
short s3=(short) i2; (4)
char c3=(char) i3; (5)

(4) gives compiler error . I cant understand why ?


 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Funny but I dont get an error.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:
int i2=-20; (1)
final int i3=i2; (2)
final int i4=200; (3)
short s3=(short) i2; (4)
char c3=(char) i3; (5)

(4) gives compiler error . I cant understand why ?



Ragi, do you mean to ask without the explicit cast why is it failing?? because the above code compiles fine with the cast in place?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct the indicated error in your question. And have a look on the JLS.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic