The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting doubt" Watch "Casting doubt" New topic
Author

Casting doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583



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 ?


Tell the difficulties that i am difficult.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Feb 09, 2012
Posts: 583

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
Bartender

Joined: May 31, 2011
Posts: 1776
Are you asking me or arguing char should allow at least integers in its range?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

The int value you have is a compile-time constant, so the compiler knows whether the narrowing conversion is safe or not.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Feb 09, 2012
Posts: 583

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

    Joined: Feb 09, 2012
    Posts: 583

    Thank you Henry now i think my doubt would be over here
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Casting doubt