• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assignment problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final int a=1;
final int b ;
b=2;
int x=0;
switch (x) {
case a: //ok
case b: //compiler error
------------------------------------------------------
it is portion of a code,please don't be angry because i didn't write a complete program.
i don't want to ask why is the error..because i found at least 3 threads here dedicated on the run-time vs compile-time constants more or less same to this piece of code.

What confuses me is that why 'b' is run time constant. The same assignment, when i break it down in two lines, the nature changes.

can you please tell me the steps of memory allocation in both cases?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hritik, welcome to javaranch.

can you please tell me the steps of memory allocation in both cases?



It is not about memory allocation. b is not a compile time constant because the JLS says so. You can see here for details. Basically when you create a final variable and assign it a value right there, then the compiler can deduce the value at compile time and replace the value of the variable (or I should say constant) at every usage of the constant. So this code



becomes

 
Hritik Roy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit for your reply.As i have mentioned earlier,that i went through the other threads in this forum on this particular topic,eventually following the links, i went to JLS as well. However, Mr. Wong wonderfully summarized it in these few points-
1. declared as final
2. have a primative or String type
3. initialized (on the same line as the declaration)
4. assigned to a compile time constant expression

I understand that part,that is not my question. If i was passing the value through a method call to the variable, or if i used any expressions other than the ones mentioned in JLS,i would have agreed that it is run-time constant.

But my question is how " final int i=1" and "final int i; i=1;" these are different in terms of assigning the values? How come they are assigning the same value differently? i just want to know what happens when i compile and run these two lines,at what phase variables are initialized. Because unless that doubt is not cleared, i am failing to understand why the second expression does not qualify for the first bullet point of criteria of constant expression mentioned in JLS(Literals of primitive type and literals of type String )
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expressions using for switch cases must be COMPILE TIME CONSTANTS.

COMPILE TIME CONSTANT is a literal or a final variable which is initialized during creation:
final int i = 10; //compile time constant
12 //compile time constant (integer literal)
"funny" // string literal (but it can not be used as case value)

This is NOT compile time constant:
final int i; //blank final
i=10; // compiler does not know the exact value
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3. initialized (on the same line as the declaration)



Hmmmm.... Maybe it should have been worded as "initialized during declaration". Which brings us to...

But my question is how " final int i=1" and "final int i; i=1;" these are different in terms of assigning the values? How come they are assigning the same value differently? i just want to know what happens when i compile and run these two lines,at what phase variables are initialized.



In the second case, the variable i is not initialized as part of the declaration -- and hence, isn't a compile time constant.

Now, you can argue that that the compiler should be smart enough to figure out the constant value, as the initialization is on the next line... but that is not how it is defined in the JLS. So, the variable i is not a compile time constant.

Because unless that doubt is not cleared, i am failing to understand why the second expression does not qualify for the first bullet point of criteria of constant expression mentioned in JLS(Literals of primitive type and literals of type String )



The value that is assigned to i (in both cases) *IS* a compile time constant. However, that doesn't mean that the variable i is a compile time constant -- as explained in the previous paragraph.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, here is the topic that hritik roy was refering to...

https://coderanch.com/t/454384/Beginning-Java/java/What-compile-time-constant

Henry
 
Hritik Roy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, you can argue that that the compiler should be smart enough to figure out the constant value, as the initialization is on the next line



Henry, exactly that was my problem, thanks for understanding it .Whenever i tried to think logically,i stumbled everytime.I should have wrote exactly these words of yours in my first post to make my question more clear.I apologize for that.

... but that is not how it is defined in the JLS. So, the variable i is not a compile time constant.



now that i know there is no particular reason behind it, its only because of what JLS says,so i will be ok with it, no more problems

thanks to all of you who replied.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic