Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Assigning char to int

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Consider the following code:


c = i fails to compile because of possible lossy conversion, while c = (int)5 compiles without problems. How is this possible? What is the difference between these two assignments?

Thanks!

Regards,
Shane
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
c = i fails to compile because of possible lossy conversion, while c = (int)5 compiles without problems. How is this possible? What is the difference between these two assignments?



See section 5.2 of the Java Language Specification ...

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

The reason the second example compiles is due to the compile time constant clause in the specification. And of course, the first example does not satisfy the compile time constant requirement, hence, that clause does not apply.

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shane Jensen wrote:
c = i fails to compile because of possible lossy conversion, while c = (int)5 compiles without problems. How is this possible? What is the difference between these two assignments?



See section 5.2 of the Java Language Specification ...

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

The reason the second example compiles is due to the compile time constant clause in the specification. And of course, the first example does not satisfy the compile time constant requirement, hence, that clause does not apply.

Henry


Thanks for the explanation. Makes sense.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:How is this possible? What is the difference between these two assignments?


First of all, it seems you are trying to assign an int to a char (and not the other way around as the class name and the subject of this topic suggests).

Secondly the cast to int is not needed. An integral number is by default of type int (a floating-point number by default double).

And finally 5 is an int literal (and thus by definition a compile-time constant), so the compiler can verify it's within the char range; int i = 5; is not a compile-time constant, so the compiler can't verify f it's within the char range and therefore an explicit cast is required.

Pop quiz question: would the outcome be different if you would have marked variable i as final?
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Shane Jensen wrote:How is this possible? What is the difference between these two assignments?


First of all, it seems you are trying to assign an int to a char (and not the other way around as the class name and the subject of this topic suggests).

Secondly the cast to int is not needed. An integral number is by default of type int (a floating-point number by default double).

And finally 5 is an int literal (and thus by definition a compile-time constant), so the compiler can verify it's within the char range; int i = 5; is not a compile-time constant, so the compiler can't verify f it's within the char range and therefore an explicit cast is required.

Pop quiz question: would the outcome be different if you would have marked variable i as final?


True, I noticed that as well. Should've been the other way around.

Thanks for the additional clarification.

And yes, I already tried marking i as final, which would of course make it a compile-time constant.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic