Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upcasting problem

 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain why Equal is part of output
public class EqualsTest {
public static void main (String[] args) {
char A= '\u0005';
long L= 0x0005;
Io.prn("L = "+L); //1
Io.prn("A = "+A); //2
if (A == L)
System.out.println("Equal");
else System.out.println("Not Equal");
}
}
I understand char can upcast to long, but from the output of //1 and //2, I have problem
understand this. (A is not a number, L is 5).
thanks
chi-chih
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Io.prn("L = "+L); //1
Io.prn("A = "+A); //2
on //1 and //2 should be
System.out.println("L = "+L); //1
System.out.println("A = "+A); //2
Originally posted by chichih Lin:
Could someone explain why Equal is part of output
public class EqualsTest {
public static void main (String[] args) {
char A= '\u0005';
long L= 0x0005;
Io.prn("L = "+L); //1
Io.prn("A = "+A); //2
if (A == L)
System.out.println("Equal");
else System.out.println("Not Equal");
}
}
I understand char can upcast to long, but from the output of //1 and //2, I have problem
understand this. (A is not a number, L is 5).
thanks
chi-chih
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chichih:
The integral value of char A (i.e. 5) will be
used in the if statement because it being compared with a long. Therefore, you will get
equal printed.
Thanks
Barkat
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main (String[] args) {
char A = '\u0005'; // line1
System.out.println(A); //line2
long L= 0x0006;
System.out.println(A == L);
}
By looking at line1 how can we say that value of A is 5. line2 gave me output as ♣. Is there any list which will tell this value belongs to this like that?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh:
The \u in char value says that it is unicode value. What is printed is equivalent char character. so \u0005 means unicode integral value
5. You will have to consult the unicode table to
find what unicode will print what?
Hope this helps.
Thanks
Barkat
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Knowing the ASCII/Unicode equivalent of '\u005' isn't required to be able to answer the question. All you need to know is that both '\u0005' and 0x0005 have the integer equivalent of 5, and therefore are equal.
Hope that helps,
Paul
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When perfoming, if (A == L), a binary numeric promotion is applied implicitly and therefore the operands of the numerical equality operators (==) are promoted to boadest numeric type that is long. A is then promoted to long and its value is 5. L stays as it is 5.
Hope this helps more.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic