aspose 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

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
What is compile time constant?
I want to know it, to answer the below question..


And also explain why at line 3 & 4 compile time error occured?


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi Michael,

Compile time constant means, it's value is determined at compile time itself.

Conside the following code,


Now in the above code, the value for jj is determined only at run time or in other words, only after the method myMethod runs which happens at run time. To the contrary, when you just say final int jj = 10;, it is a compile time constant.

Hope you got it.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

You get a compile time error at line 3 is because, you are trying to assign a short which is wider than a byte.

I'm not able to replicate why line 4 gives you a compiler error?? Anyone on this please??
[ December 05, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Greg L Tonn
Ranch Hand

Joined: Nov 27, 2006
Posts: 52
Line 4 fails for the same reason line 3 does. A char is wider than a byte. A char is 16 bits and a byte is only 8 bits. So you would need an explicit cast to get these two lines to compile. But, be aware that loss of data can incur since you would be "chopping off" the left most bits to make it fit.

Also just to let you know. They like it if you would specify the resource of the question you posted.
[ December 05, 2006: Message edited by: Greg L Tonn ]
Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
Even Line 7 and 8 assignments are wider than byte??
But its compiling fine ??
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
s2 and c2 are compile-time constants.
Greg L Tonn
Ranch Hand

Joined: Nov 27, 2006
Posts: 52
What Keith is saying, is that at compile time the compiler knows that 1 is small enough to fit into a byte so it doesn't complain.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
final int x;
x=1;

Now is x a compile time constant?


SCJP 5.0 77%
Greg L Tonn
Ranch Hand

Joined: Nov 27, 2006
Posts: 52
Satish, no x would be assigned at runtime not compile time. Assignments at the point of declaration are compile time.
Anuragk kushwaha
Ranch Hand

Joined: Aug 02, 2006
Posts: 51
line 3 and 4 gives compile time error due to following

----
All conversions between char and the two integer types byte and short are considered narrowing conversions: the reason being that the conversions between the unsigned type char and the signed types byte or short can result in loss of information.
---

and Almost narrowing will be done explicitely.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile time constant