• 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

narrowing conversion

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

public class MyClass {
public static void main(String args[]) {
char c;
int i;
c = 'a'; // (1)
i = c; // (2)
i++; // (3)
c = i; // (4)
c++; // (5)
}
}
Khalid Mughal (Section 3.6) says line (4) causes the program to
give a compile time error.
I'm not sure but I think 'a' is 97, hence c= 97 (1)
i = 97 (2) - Widenning conversion
97++ -> 98 (3) - Postfix addition operator
c = 98 (4) - Implicit conversion Since 98 is
within char legal value
98++ -> 99 (5) - Postfix addition operator
I was wondering if I was right?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to compile and run it? What happened?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,
remember that an implicit conversion is calculated on the type, not on the value (except for literals, but literals are literals are literals, and from marking (2) on, there do not appear any literals).
So char = int has to be casted; in this case, "c = (char) i" would be ok� - first for the compiler, but than also at runtime (you gave the explanation by yourself).
Hope it helps
Detlev
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn and Detlev.
I compiled it and Khalid Mughal is right. It gives me a compile
time error for c = i
Detlev, I did not understand your explanation. Is there any resource where I can try to read and understand this.
Because, I always thought if a variable,
byte, char, int, float, .... if it can hold the variable then
there need not be an explicit conversion.
for e.g,
byte b=0;
b = (byte) 256; // Here we need to cast as b can hold only upto 127
System.out.println(b) // b=0
Any help is greatly appreciated.
-Jeelani
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jay Kay:
I always thought if a variable,
byte, char, int, float, .... if it can hold the variable then
there need not be an explicit conversion.


That only applies to literals and runtime constants. Try the following lines:
int i = 100;
final int j = 100;
byte b;
b = i; // compiler error
b = j; // this one is fine
The idea here is that the compiler knows that a literal or a compile time constant will not change its value and so it can check to see if it'll fit into the variable it is being assigned to. On the other hand if it is a normal variable then the compiler has no way of knowing if it'll ever be larger than the type it is being assigned to so you need a cast.
hope that helps


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally by Dave:
On the other hand if it is a normal variable then the compiler has no way of knowing if it'll ever be larger than the type it is being assigned to so you need a cast.



but Dave char and int have same size ??? then why casting should be needed ??
If it depends on 'type' then casting should be needed in case of "i = c" also???
Regards
Ravish


[This message has been edited by ravish kumar (edited October 15, 2001).]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ravish
The assignment of a char to an int is a widening conversion. A char is an unsigned variable with a size of 2 bytes ( 0 - 65535) while an int is a 4 byte signed variable. So taking a char variable and assigning it to an int is doen without a cast. If you want the straight scoop, along with all of the combinations, check out the JLS section 5
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Detlev,
I tried the above by compiling by casting to (byte).I get for values above
2^32-1 error that it is above integer limit.That I understood.But for values less
than that I am getting some strange values.Why is so?
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and.. remember
u can't do this.
final double d = 4.4d;
long l = d; //( with in range and final) but error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic