Hi,
I am having couple of doubts in Dan's exam, please clarify them.
1)
Question:
--------------------------------
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);
-----------------------------------------
My doubt:
Above program is working fine.
My concern is, What are the other ways of assigning the value to char variable, other than the below ones,
char c1='c';
char c2=34;
char c3=023;
char c4=0x45;
char c5='\u0001'
How is it possible, to assign the value to the char variable as shown in the line numbers 2 and 3.
And why the below one is not working,
char c6='\0x0001'; (It should work on the same lines of assignment in the line number 3. Only the difference between these two is,
one is in octal representation and other one is in Hexadecimal representation)
=============================================================================================================
2)
Question:
-----------------------------------------
class EBH202 {
static boolean a, b, c;
public static void main (
String[] args) {
boolean x = (a = true) || (b = true) && (c = true); // 1
System.out.print(a + "," + b + "," + c);
}}
-----------------------------------------
Output: true,false,false
My doubt:
In one of the materials i have read that, all the sub expressions of the expression
will be evaluated before applying any operators. Based on the same logic, prior to applying the short circuit 'OR' operator '||', second and third expressions(b=true,c=true) should be evaluated.
And one more doubt is, operator '&&' is having higher precedence priority than '||'. On this basis, second and third expressions, (b=true,c=true) should be evaluated.
Why is it not happening?
=============================================================================================================
3)
Question:
-----------------------------------------
class JSC201 {
static byte m1() {
final char c = '\u0001';
return c; // 1
}
static byte m3(final char c) {return c;} // 2
public static void main(String[] args) {
char c = '\u0003';
System.out.print(""+m1()+m3(c));
}}
-----------------------------------------
Output:compile time error will be thrown at line 2:
Explanation given:"There is a compile-time error at 2. The char type variable, c, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return c;", is a return statement with an expression, c. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is byte. The return statement attempts to return the value of the char type variable, c. If a char value is a compile-time constant, and if the value falls within the range of type byte, then the char value is assignable to type byte. In method m3, variable c is not a compile-time constant, so the value of variable c is not assignable to type byte. While the declaration of method m3 produces a compile-time error, the declaration of method m1 does not; because the variable is a compile-time constant with a value, \u0001, that is assignable to type byte. "
My doubt:
On what basis we can say whether it is compile time constant or not.
Any how in the method declaration on line number 2, it is stated that 'c' is final variable and we can't modify the same in the
method. So why is it not final variable.
Many Many Thanks in Advance Narasimha.