• 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

compile time constants and string literals

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know, for COMPILE TIME CONSTANTS, memory is allocated at runtime only right ? But the compiler keeps track of these compile time constant variable's values right ?

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand your question. All memory is allocated at runtime, no?
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya ya it is .

But or example :



line 11 shows an error, but if final keyword is added to this line it compiles. so it is clear that the compile time constants are being considered here at compile time. so thats what i was asking actually.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error that you are speaking of @shambhavi sham does not have to do with the use final.
When I try to compile the code as is I get the following:


The problem with this code:


is that the compiler cannot say for sure that the variables b, c or d have values. However you are sure that you want to use them not matter what. If you change the code to this:

You get the expected results and there is not a final in sight.

If you do change:

to

then you do not need to initialize because the compiler knows that no matter what a will be greater then 0 and the other variables will get values.

You could also change your code to something like this:

or this:

And you would still get the results that you expected.
I think that the topic to look into here variable initialization.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shambhavi sham wrote:
line 11 shows an error, but if final keyword is added to this line it compiles. so it is clear that the compile time constants are being considered here at compile time. so thats what i was asking actually.



Hate to point this out, but isn't that obvious?

Of course, compile time constants are used at compile time. That's why they are called compile time constants...

Henry
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic