• 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

Does value, constant and literals means same ?

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x = 10; // Here "value" of x is 10. "Constant" is 10 and "literal" is int 10. am I correct ? Did I use those words in correct manner ?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of x is 10, and 10 is a literal which holds the constant 10 but x is not a constant.
Value constant and literal do not mean the same thing.
A literal is where you write the value in the code e.g. 10, "Ganish Patil", rather than a formula 5 × 2.
A constant is a value which never changes.
There is a specific meaning for the term constant expression (often called compile‑time constant) in the Java® Language Specification, but that may be difficult to read.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These three words do not always mean the same thing, and they are used in different contexts.

Looking at the statement: int x = 10;

After executing this statement, the value of the variable x will be 10.

The 10 in the statement is an integer literal. "Literal" means that the value is literally in the source code; you write the value itself directly in the source code.

"Constant" (or: compile-time constant expression) means that the value is always the same, and the compiler has slightly different rules for constant expressions and expressions that are not constant. Literals in the source code are always constants.
 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Literal - is a value assigned to a variable.
Ex.


Java by default already have some constants:
Constants Java 7


-Mauro.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh got it, thank you Mauro, Jesper de Jong and Campbell
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mauro Trevigno wrote:Literal - is a value assigned to a variable.
. . .

That is not quite right, I am afraid. Although all the values to the right of the = signs in your post were literals, it is quite possible for expressions on the right of a = sign not to be literals.
i = j + k;
… where j and k are not literals and may not be constants.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Campbell

int x = 10;

Value: value of x is 10.
Constant: In above example 10 is constant. More Example: x = 10 * 2 ; // Here 10 and 2 are constants.
Literal: In above example 10 is integer literal.

Is it correct now ?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. But remember that x is not a constant.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie ya sure. I never thought I would ever get such quick replies. Now I feel like I can complete core and Adv. Java in 40-50 days, not thoroughly but at least I can develop desktop applications and web. Thank you so much
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

Please don't worry too much about learning too quickly. Be concerned to learn it correctly.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely..
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Teach Yourself Programming in Ten Years was just splendid.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic