This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Char

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from Bill Brogden's Exam cram book.
public class EqualTest {
public static void main(String args []) {
char A = '\u0005';
if (A == 0x0005L)
System.out.println("Equal");
else
System.out.println(" Not equal");
}
}
1) Program compiles and prints "Not Equal"
2) Program compiles and prints "Equal"
The given answer is 2). This program compiles fine and gives "Equal" as output. Can you append L to a value when comparing with char variable? I don't understand what is going on here.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0x0005L is a literal (hexadecimal because of leading 0x long because of L)
then you have comparison char and long, therefore arithmetic promotion to long is done automatically
CIAO
Christian
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian,
I know about "0x0005L is a literal (hexadecimal because of leading 0x long because of L)"
But my confusion is for comparison between char and long, and also for arithmetic promotion to long from char.
I don't know about this stuff. Would you explain me in detail?
Thanks a lot.
 
Christian Strohmaier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all primitive data types except for boolean are arithmetical promoted, whith the following algorithm:
* double, if there is at least one double, break
* float, if there is at least one float, break
* long, if there is at least one long, break
* default is int
Hope this helps
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char c = 67; this is a valid assignment. Basically char is an unsigned integer type. and char to int conversion is natural and accepted. What happens is that char gets assigned the unicode char. then when it is being compared to the long, the int value is taken and then promoted to long for the comparison. chars can have simple arithmetic performed on them. eg. char c = 'A'; c++; this works basically because char is then used as an int, added to and if printed will print B
 
Did you miss me? Did you miss this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!