Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes difference between final variable in JAVA and constant in C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between final variable in JAVA and constant in C" Watch "difference between final variable in JAVA and constant in C" New topic
Author

difference between final variable in JAVA and constant in C

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
What are difference between final variable in JAVA and constant in C? or variable use define keyword in C?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19528
    
  42

abalfazl hossein wrote:What are difference between final variable in JAVA and constant in C? or variable use define keyword in C?


By "constant in C", I am assuming through the use of the "const" keyword?


In Java, variables that are defined as final, can't be modified. In this regard, it is very similar to variables in C, that are declared as const. Furthermore, the java compiler has the concept of compile time constants, and can treat variables as "constant variables" (variables that are compile time constants). In this regard, it is probably more similar to preprocesor constants in C (using the #define macro).

So... depending on how the final variable is declared in Java, it can be closer to "const" variables or "#define" constants, when comparing to C.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Have you tried LearnNowOnline? http://www.learnnowonline.com/
 
subject: difference between final variable in JAVA and constant in C