• 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

how many differnt ways can you initialize char ....

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. char a = '\u0000';
2. char a = 'a';
3. char a = 65;
4. ch'\u0000'r = 'a';
q1] is 4 valid and are there any more ways ?
q2] in 3 when we say char a = 65, suppose we use the == operator to compare this value with an int i = 65, will it give true ? and why ?
what does it mean to say char a = 65.... WHAT CHARACTED DOES A HAVE ?
q3] does the 'u' used above have to be lower case ?
q4] sometimes we compare '\u0065' with a hexadecimal representation of long l = 0x0065 with the == operator and the result is true... WHY? what are the mechanics governing that comparison ?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q1] - No it is not valid (as you would know if you tried to compile it). The reason is that to declare a variable you need to use a keyword, not something that will look like a keyword AFTER the compiler translates it. By the way you stuck a null in the middle of char, I think you meant to stick an 'a' like
ch'\u0061'r x = 'a'; (which won't work either).
q2] In 2 the char is getting a decimal code for a. I looked this up in my ooooold IBM extended architecture pamplet.
Dec 65 corresponds to hex 41 corresponds to ASCII 'A' corresponds to unicode \u0041.
Dec 97 = hex 61 - ASCII 'a' = unicode \u0061.
So the answer is NO, A !== 65.
q3] Yes it must be lower case. Otherwise the compiler takes the whole thing as a literal and can not squeeze it into a char.
q4] \u0065 is unicode for 'e'. 0x0065 is hex for 'e'. The compiler resolves them and sees them as equal.
Try it!!!
class Test{
public static void main(String args[])
{
//ch'u\0061'r p = 'u'; not a keyword
//char q = '\U0065'; not proper unicode
char a = 65; // uses Dec code assignment
System.out.println("a = " + a);
char x = 41;
System.out.println("x= " + x);
char y = '\u0065';
long z = 0x0065;
boolean b = (y == z);
System.out.println("y = " + y + ", z = " + z + " " + b );

}
}


[This message has been edited by Cindy Glass (edited December 20, 2000).]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q1] 4 isn't valid, but remove the single quotes and change the number to represent the character 'a' (#1 below) or use \u0000 in a variable name (#2, again without the quotes). Are there more ways to initialize char? How about using octal (#3) and hexadecimal numbers (#2).
q2] char is an integral type, so the comparison of a char value of 65 does "==" an int value of 65 (#5).
q2] (continued) OK, so what does it mean to set a char to 65? Your can use System.out.println() to see (See output below that shows the char value is 'A'). If you don't have a reference that shows what the values represent, it's easy and very helpful to write some really simple programs to print out such info.
q4] unicode uses hexdecimal digits.
<PRE>class Char {
public static void main(String args[]) {
char aa = '\u0000';
char ba = 'a';
char ca = 65;
char d\u0000 = '\u0000'; // #1
char ea = 0x0000; // #2
ch\u0061r fa = 0072; // #3
System.out.println(fa); // #4
int e = 65;
if (ca == e) System.out.println(ca + "==" + e); // #5
}
}
</PRE>
Output:
C:\java\jr>java Char
:
A==65
 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Cindy and Carol, that was just brilliant !
reply
    Bookmark Topic Watch Topic
  • New Topic