• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does value, constant and literals means same ?  RSS feed

 
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh got it, thank you Mauro, Jesper de Jong and Campbell
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But remember that x is not a constant.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely..
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teach Yourself Programming in Ten Years was just splendid.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!