• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Doubts in Dan's Exam:

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static byte m3(final char c) {return c;} // 2
c is final but not a compile time constant:-
The following compiles:-
byte showit(){
final char c = 'c';
return c;
}
Feel free to correct me if I am wrong anyone.
With Dan's questions it is good to mess around with the code to explore further.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Narasimha,
I will try and give this a shot.
1. You are mixing syntax with your assignment. The '\0' signifies octal notation. Therefore, '\0x0001' will try and interpret x0001 as an octal value (which is not a valid octal value). To make is hexadecimal you leave off the single quotes (i.e., 0x0001).
2. According to the Java Spec:

15.7.1 Evaluate Left-Hand Operand First
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.


Using that logic we can easily see that the expression will work out to:
x = (a = true) || (b = true) && (c = true);
x = true || (b = true) && (c = true);
from this point on the result of x is given because ORing true against anything else will result as true. Therefore, we don't need to perform any more steps. Since we have evaluated the a = true assignment fully then a will be true while b and c will still remain false.
3. In the m1 method, the compiler actually replaces the return c line with return '\u0001'. Therefore the return type can be shown as byte because the unicode leteral can fit into a byte. In the method m3, you are trying to convert a character into a byte which can't be done without casting.
Regards,
Manfred
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic