Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Dan's test on Char values

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class D {
public static void main (String[] args) {
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
char d = 0x0031; // 4
char e = '\u0031'; // 5
System.out.print(""+a+b+c+d+e);
}
}
Ans:Compiles & runs fine.
At line one octal integer is converted to char.
At line 4 hexadecimal integer is assigned.
At line 5 unicode character is assigned.
But I am not understanding what is happening at lines 2 & 3?What is the role of back slash there?
Thanks
Veena
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both lines 2 and 3 are octals also. The backslash allows you to place the octal digits there. I asked a similar question a couple weeks ago. If you do a search you should be able to find it. I think someone posted a link when they responded explaining it.
I wouldn't worry about it being on the exam though.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

'\001' '\01' '\1'
'\012' '\12'
"This is \u0061 string of text with escape sequences for some of the ch\141racters.\n"
Some ASCII characters we can type �A� �a� �0� �*�
Some ASCII characters we cannot type. So we use escape sequences instead. �\n� �\r� �\\� �\��
�0� is the decimal digit 0. How do we represent binary 0?
We can use octal escape sequences to type any ASCII character.
�\000� �\007� �\017� �\077� �\177� �\377�
What are octal escape sequences good for? Control characters for data communications (end-of-text, end-of-line) and talking to devices (ring-bell, form feed)
There are 65536 Unicode characters. Only the first 256 Unicode characters can be represented by octal escape sequences.
(Actually, I think there are at least 90,000 currently defined Unicode characters.)
[ July 18, 2003: Message edited by: Marlene Miller ]
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:

'\001' '\01' '\1'
'\012' '\12'
"This is \u0061 string of text with escape sequences for some of the ch\141racters.\n"
Some ASCII characters we can type �A� �a� �0� �*�
Some ASCII characters we cannot type. So we use escape sequences instead. �\n� �\r� �\\� �\��
�0� is the decimal digit 0. How do we represent binary 0?
We can use octal escape sequences to type any ASCII character.
�\000� �\007� �\017� �\077� �\177� �\377�
What are octal escape sequences good for? Control characters for data communications (end-of-text, end-of-line) and talking to devices (ring-bell, form feed)
There are 65536 Unicode characters. Only the first 256 Unicode characters can be represented by octal escape sequences.
(Actually, I think there are at least 90,000 currently defined Unicode characters.)
[ July 18, 2003: Message edited by: Marlene Miller ]


Does '\0some number' & '\some number' represent octal representation?& '\u0some number' represent hexadecimal?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic