This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constant expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile time constant expressions" Watch "compile time constant expressions" New topic
Author

compile time constant expressions

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Dear all,
Is the following knowledge about compile time constants correct?
1�) A compile time constant is a literal (String or primtive type) declared with modifer final.
Examples:
final byte b = 6;
final String s = "abc";
Declaring a variable as final isn't sufficient to become a compile time constant. Hence,
final byte b;
b = 6;
would'nt make b a compile time constant.
2�) The trick cases where is it useful to know that the variable is a compile time constant one:
a) Assignement conversion:

but

b) String reference:

OUtput: false
Explanation: (Dan's mock exam)

The expression (a+b) is evaluated at runtime and produces a new instance of a String Object. Even thought the expression (a+b) produces the same result each time it is evaluated, each evaluation of the expression produces a new instance of the String

but


Outputs: true
Explanation: (Dan's mock exam)
The key to understanding this program is understanding the impact of declaring variables "a" and "b" with the "final" modifier. Since "a" and "b" are final, all of the String expressions become compile-time constant expressions that evaluate to "AB". As a result, all are represented by a single String Object at runtime. Therefore, the equality operator finds that the references are equal and returns the value true.

Are these cases the only ones concerning compile time constants or are there others to be known?
Thanks in advance for your response,
Cyril.
PS: In Dan's mock exam, sometimes, method intern() from String class is used. Have we to learn about it for the exam?


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yeah, it looks like you've got it down pretty well. As far as the intern method goes, I don't believe that will be on the exam.


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constant expressions
 
Similar Threads
int -> float -> int and loss of precision
expression involving int or smaller
Dan's Q doubt again?
Dan's Question : Fundamental Classes
Doubts in Dan's Exam: