File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile time constant

 
Micheal John
Ranch Hand
Posts: 344
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even Line 7 and 8 assignments are wider than byte??
But its compiling fine ??
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s2 and c2 are compile-time constants.
 
Greg L Tonn
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final int x;
x=1;

Now is x a compile time constant?
 
Greg L Tonn
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish, no x would be assigned at runtime not compile time. Assignments at the point of declaration are compile time.
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic