• 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

Primitive conversions

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
While doing a mock exam recently, I was asked to select whether
the statement would compile or not -
char chr = (int)Integer.MAX_VALUE;
I felt it would compile as it was a narrowing conversion, but was proved incorrect. Wrote a small test program to check this.

If the line is uncommented out, the compiler complains about possible loss of precision. In the case of narrowing conversion for the int variable j,this does not happen and I get the output as- Int j is 2147483647, clearly showing that loss of precision is tolerated in this case. So there is a difference between how it behaves for a conversion to a char vs other narrowing conversions.

I also checked the JLS, but could not find any explanation for a compilation error. It says-
quote..
A narrowing conversion of a character to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though characters represent 16-bit unsigned integer values.
unquote
Hope someone can help me here.
Thanks a lot!
Sajida
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be confusing narrowing conversion and casting.
Here's the mnemonic from RHE:
byte-short-int-long-float-double
char-int
Going left to right on any of the two paths above is a widening conversion; going right to left would be a narrowing conversion.
In the statement char chr = (int)Integer.MAX_VALUE;, you are attempting to do a narrowing conversion (assigning an int to a char). This is illegal without an explicit cast to (char).
In the statement int j = (int)9494949895959504040404.0;, no narrowing conversion is performed since you are assigning an int to another int. However, because you cast a floating point value (double) to an int, the compiler trusts that you know what you are doing and happily complies by truncating the bit representation of the double (64 bits) to the size of an int (32 bits). No error is reported.
Junilu Lacar
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
Thanks for your response, that was a mix up on my part.
Just another question, regarding your statement about attempting to do a narrowing conversion (assigning an int to a char). This is illegal without an explicit cast to (char).
If this is the case, then why does the statement char chr1 = (int)65535 compile successfully ? If it is changed to
char chr1 = (int)65538, then I get the same error. This implies that the compiler allows char =(int) something in those cases where the value can be accomodated in char and in other cases it raises an error.
Is this correct ?
Regards,
Sajida
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will take char chr = (int)65535 because 65535 is a literal constant and it is within the legal range of values for a char. It's the same reason why the compiler will take the statement
byte b = 1;
Under the rules of implicit conversion, 1 is converted to an int but since the compiler can determine at compile time that there is no danger of loss of precision, it accepts the statement.
When you assign a value of variable, there is no way for the compiler to tell at compile time what the value of the variable will be so it assumes the worst and warns of a possible loss of precision.
Here's something you might want to try: Assign the value of a static final int. You will see that if the value in the static final int falls within the valid range of values for char, the compiler will accept it too. Again, this is because the compiler is able to determine at compile time that there will be no loss of precision.
<pre>
static final int INT_VALUE = 1;
void someMethod() {
char c = INT_VALUE; // Ok
}
</pre>
Junilu

Originally posted by sajida kal:

Just another question, regarding your statement about attempting to do a narrowing conversion (assigning an int to a char). This is illegal without an explicit cast to (char).
If this is the case, then why does the statement char chr1 = (int)65535 compile successfully ? If it is changed to
char chr1 = (int)65538, then I get the same error. This implies that the compiler allows char =(int) something in those cases where the value can be accomodated in char and in other cases it raises an error.
Is this correct ?
Regards,
Sajida


 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,
Thanks a lot for your detailed explanation.!
Regards
Sajida
 
ice is for people that are not already cool. Chill with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic