Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

compile time constants and string literals

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • 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: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
    Bookmark Topic Watch Topic
  • New Topic