• 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

Casting doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


here i am assigning integer literal to char variable type that is narrowing conversion and as we know narrowing conversion always required explicit casting .I was expecting compile error "possible loss of precision;need a cast" but nothing so, why ?

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char size is to hold a 16-bit unicode value. If you read this Java tutorial's Primitives you can see it holds till 65535.

Hence till char c=65535; you won't get compiler error.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:char size is to hold a 16-bit unicode value. If you read this Java tutorial's Primitives you can see it holds till 65535.

Hence till char c=65535; you won't get compiler error.



If you know about char size that is unsigned 65535 then you may also know about integer size that is from -2,147,483,648 to +2,147,483,647.So matter is not that how much range it will allow ,matter is that it is a narrowing conversion and there must be explicit casting .
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking me or arguing char should allow at least integers in its range?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Are you asking me or arguing char should allow at least integers in its range?



You lost question , i was not asking about range because i know about it.matter is not that how much range it will allow or not ,matter is that it is a narrowing conversion and there must be explicit casting that was i am asking here.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The int value you have is a compile-time constant, so the compiler knows whether the narrowing conversion is safe or not.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

John Jai wrote:Are you asking me or arguing char should allow at least integers in its range?


You lost question , i was not asking about range because i know about it.


Yes I did. Sorry!
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:The int value you have is a compile-time constant, so the compiler knows whether the narrowing conversion is safe or not.



Thanks Matthew but i have still confusion i read that all primitive literals are compile time constant.It means compile should do decide itself for all type cases that it's narrowing conversion is safe or not then why we do some time casting for narrowing conversion
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:
Thanks Matthew but i have still confusion i read that all primitive literals are compile time constant.It means compile should do decide itself for all type cases that it's narrowing conversion is safe or not then why we do some time casting for narrowing conversion



An explicit cast is needed if the compiler can't tell that the cast is safe. Can you give an example that's confusing you?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some more read to clear this doubt after that if i don't get it then i will continue this again.thanks again
 
author
Posts: 23951
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

saloni jhanwar wrote:I need some more read to clear this doubt after that if i don't get it then i will continue this again.thanks again



The exact definition regarding how compile time constants are treated with assignment (implicit casting of) is in section 5.2 of the JLS...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

Specifically, this portion...

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.



  • Henry
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Henry now i think my doubt would be over here
    reply
      Bookmark Topic Watch Topic
    • New Topic