wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Upcasting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Upcasting problem" Watch "Upcasting problem" New topic
Author

Upcasting problem

chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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


not so smart guy still curious to learn new stuff every now and then
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jul 30, 2001
Posts: 149
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 04, 2002
Posts: 133
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

Joined: Aug 29, 2002
Posts: 223
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.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Upcasting problem
 
Similar Threads
regarding scjp 1.5
shift operator
Char
doubt in equals method....
Shift Operator