Hi,

Im new to JAVA and i have a question. The following code compiles fine and executes fine. But if you assign c = '\29' , in the following code, the compilation fails.

1. How does JAVA interpret char c = '\21'; and char c = '\29'; , as escape sequence character?
2. Also char c = '3'; is valid and why not char c = '33';

class A1{
		public static void main(String arg[]){
				//char c = '\29';
                                char c = '\21';
				System.out.println("value of c is: " + c);
		}
}

Could some one explain me why?

thanks,
katharnakh.

char should have length 1. Always 1.
This is correct char c=' '; or char c='2'; Not this: char c=''; or this char c='12';
So I believe that the compiler could convert the '\21' to its equivalent character but not '\29' because there isn't any. Is like writing: char c='\n'. n is a special character for newline. But char c='/n' is not correct.

It's because those are octal escape codes, and there are no 8s or 9s in octal numbers.

Edit:

Also a char is a single character. So '3' is okay, but '33' is two characters, so it is not okay.

...
So I believe that the compiler could convert the '\21' to its equivalent character but not '\29' because there isn't any. Is like writing: char c='\n'. n is a special character for newline. But char c='/n' is not correct.

Hi, thanks for the reply.
How the does the compiler converting it to equivalent char? is it finding some matching char for that or? I did look into ASCII table, but the character i find was different character from the one which above program prints.

How does java interpreting '\21' and '\29'(for eg.) there are some other examples which compiles and some not. How to recognize given an example whether char c='\21'; or char c = '\29'; that java recognizes?.

katharnakh.

Hi, thanks for the reply.
How the does the compiler converting it to equivalent char? is it finding some matching char for that or? I did look into ASCII table, but the character i find was different character from the one which above program prints.

How does java interpreting '\21' and '\29'(for eg.) there are some other examples which compiles and some not. How to recognize given an example whether char c='\21'; or char c = '\29'; that java recognizes?.

katharnakh.

See my post. Those are the octal and not decimal values. (I know, we probably "crossed" our replies).

It's because those are octal escape codes, and there are no 8s or 9s in octal numbers.

Ok, but how do you say they are octal escape codes? it cannot be hexa escape codes?
(Sorry, i dont know whether there is some thing like hexa escape codes or hexadecimal numbers are represented in 4 digits?).

katharnakh.

If you want to use the decimal values, then there is no need for either a \ or ' characters, as the decimal ascii "escape code" is the same as the integer value for the char. So, simply do

char c = (char) 29;

Hi Masijade, JavaAddict,

Thanks for your help. I found this in the internet. Now i understood why it was taking '\21' and '\29' as octal escape char. It is not hexa escape char, because it should start with \x, for eg. '\x29'.

katharnakh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.