Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

complie-time constants

 
Rachana Sharma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
This is one of the questions in danchisholm's paper.

class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}

What is the result of attempting to compile and run the program?

a. Prints: 13
b. Prints: 4
c. Compile-time error at 1
d. Compile-time error at 2
e. Run-time error
f. None of the above

The answer to the above question is option (d).There is a detailed explaination given about the answer but iam not able to follow it.
could someone please explain the output.

Thanks in advance.
 
Kavitha Govindaraj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you carefully look at the code at

static byte m2(final char c2) {return c2;} // 2

Based on the method's return type, it will convert char to byte which leads to loss of precision because of the range it will accept. Java will not allow to downcast.

class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}
 
sridhar anna
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try compiling the code using the byte cast:

static byte m2(final char c2) {return (byte)c2;} // 2

This will compile the code, output 13 and clear your doubt

Regards,
Sridhara.
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can you please explain why the same error did not occur in the method m1()
in the below code.

[code]
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
[code]

Shyam Ramineni
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a variable is explicitly asigned a value at compile-time and is marked as final, this variable can be asigned to a variable which holds less bits, i.e char(16bits) to byte(8bits) (downcast), if and only if the final
variables value will fit into the destination container (the byte var in this case).

Having the above, the compiler sees that this value will NOT change and that the value DOES fit into a smaller container without any loss of precision. So no problem there.

But the compiler has no way of knowing what value an agrument will receive at runtime, and this is why the compiler will complain about a possible loss of precision.

I hope i explained myself properly, it's quit eraly where i am.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic