• 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

Upcasting problem

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic