This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes complie-time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "complie-time constants" Watch "complie-time constants" New topic
Author

complie-time constants

Rachana Sharma
Greenhorn

Joined: Jun 21, 2005
Posts: 15
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

Joined: May 27, 2004
Posts: 28
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
}}


Thanks <br />Kavitha G <br />SCJP
sridhar anna
Greenhorn

Joined: May 19, 2005
Posts: 23
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

Joined: Jul 08, 2005
Posts: 43
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


<a href="http://hyderabaditeducation.com" target="_blank" rel="nofollow">http://hyderabaditeducation.com</a>
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: complie-time constants