File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile-time constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile-time constant" Watch "compile-time constant" New topic
Author

compile-time constant

francis joseph
Greenhorn

Joined: Jul 31, 2004
Posts: 13
I was going through the questions at danchisholm.net and trying to understand the following question. Can someone help me understand the following question, what's the correct answer and why?

Answer is c.

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

Thanks
Joe

([C0DE][/C0DE] tags added)
[ August 05, 2004: Message edited by: Barry Gaunt ]
No�l Verdurmen
Ranch Hand

Joined: Jul 28, 2004
Posts: 33
You have to cast explicitly if you want to return a char ('c1', which is 2 byte) as a byte (which is 1 byte!).
If you don't, it might cause a loss of precision and that results in a compile-time error.


No�l
No�l Verdurmen
Ranch Hand

Joined: Jul 28, 2004
Posts: 33
You have to cast explicitly if you want to return a char ('c1', which is 2 byte) as a byte (which is 1 byte!).
If you don't, it might cause a loss of precision and that results in a compile-time error.

hmm... i just reconsidered, after i saw the function at line 2...
Are you sure 'c' is the right answer?
I would say 'd' does not compile, for the reason I stated in my previous answer.
'c' should compile, as it is defined at compile-time as a constant. The compiler implicitly casts constants to the smallest possible primitive.
'd' is also final, however it is not yet defined, so the compiler has to reserve 2 bytes.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Francis,

Thank you for using my exam.

No�l is correct. The answer provided by the exam is d. The explanation provided with that answer is as follows.


There is a compile-time error at 2. The char type variable c2 is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The method parameter c2 is declared final, so the value of c2 can not be changed within method m2. The value of method parameter c2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter c2 is not a compile-time constant. In method m2, the statement, "return c2;", is a return statement with an expression, c2. 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 method m2 is byte. The return statement attempts to return the value of the char type variable c2. 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 m2, variable c2 is not a compile-time constant, because the value of c2 is not known at compile time. Instead, the value of c2 is assigned at run time to the value of the argument. Since the char type variable c2 is not a compile-time constant, the value of variable c2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable c1 is declared final and the value is set at compile time; so c1 is a compile-time constant. The value \u0001 falls within the range of type byte; so the value of the compile-time constant c1 is assignable to the return type of method m1 without an explicit cast.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile-time constant