• 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

what are literals exactly?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are literals just values of variables...

such as:
int myNum 100; (100 is the literal?)
String myLine = ("is this the literal?"); - is the string the string literal

does this only apply in a specific case? are these not just values?

i got a bit confused as i read that literals and constants are the same thing...i thought constants were int MYNUMBER (isnt MYNUMBER a constant)

can somebody please clear this up for me...
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A literal is a self defining value, not the contents of a variable.  For example: 123 and "String"

Before asking these very generic questions,
try asking a Search Engine and read what is returned.  Then if you have questions about what you have read, copy the text and paste it here with your questions.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your post is a prime example of the type of problems us "green horns" encounter norm.

i am reading a book on beginning java and do think the language was a bit complex to understand on a beginner level.

so i googled it and found similar responses

so i posted here on code ranch and you replied "A literal is a self defining value, not the contents of a variable"
im really sorry if im being stupid norm...but i dont understand what this means

what can a self defining value mean? i thought values were always assigned to variables,or the other way round.
i have never seen a self defining value,or maybe i have and have not identified it.

thanks for your reply norm,thanks
 
Saloon Keeper
Posts: 15487
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables hold values. Literals represent the values.

If you do a = b; you assign the value of variable b to variable a. If you do a = 3, you assign the value of the literal 3 to the variable a.

A program can have values without using any literals. Here's an example:

This program just prints out the first command line argument. We didn't use any literals, but we still have a value.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We didn't use any literals,

Except for the array index of 0
 
Stephan van Hulst
Saloon Keeper
Posts: 15487
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Err... yes.

Okay, here's another one:
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm... I kinda see the problems here.  The confusion might be in the terminology and the examples doesn't exactly help.

In simple term a literal is any value that you assign to a variable.



The value entered into a variable as it intent to be understood is the literal.  
This value can be changed throughout the program.

A constant is a variable the remain constant throughout the program and it cannot be change.
For example:  



Hope this clarifies it.
 
Stephan van Hulst
Saloon Keeper
Posts: 15487
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that is wrong. A literal is not any value assigned to a variable. You can assign values to variables that are not literals. For instance, you can assign one variable to another, or the result of an operation or method call.

A literal is just a piece of code representing a value. After compilation, literals don't matter anymore. For instance, 0b101 and 5 both represent the same value at runtime. They're both just pieces of text in a .java file though.
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, that is wrong. A literal is not any value assigned to a variable. You can assign values to variables that are not literals. For instance, you can assign one variable to another, or the result of an operation or method call.

A literal is just a piece of code representing a value. After compilation, literals don't matter anymore. For instance, 0b101 and 5 both represent the same value at runtime. They're both just pieces of text in a .java file though.



Then my friend, Google is also wrong.  ^^

Taken straight out of Google.
Java Literals are syntactic representations of boolean, character, numeric, or string data. Literals provide a means of expressing specific values in your program.

Besides, I used examples to show exactly what I meant.  

Cheers
 
Stephan van Hulst
Saloon Keeper
Posts: 15487
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgetting for a moment that the internet is not always a reliable source of information, what you quoted actually completely agrees with what I said.

A syntactic representation of data is really just a piece of code. Being able to express a value does not mean that it is a value.

The word "tree" is a word, not a tree.
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Forgetting for a moment that the internet is not always a reliable source of information, what you quoted actually completely agrees with what I said.

A syntactic representation of data is really just a piece of code. Being able to express a value does not mean that it is a value.

The word "tree" is a word, not a tree.



I didn't say I didn't agree what you say.  I don't think you are hearing what that green horn was asking.
He wasn't asking for more technicality, he wants some example that he can relate to.

But then again, I am a green horn too, what do I know right? ;)
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks...now im totally confused...is this just Symantecs? i mean is it going to cause me major problems down the line...if i dont understand this right away?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Frankie Law wrote:. . .
Then my friend, Google is also wrong.  ^^

Taken straight out of Google.
Java Literals are syntactic representations of boolean, character, numeric, or string data. Literals provide a means of expressing specific values in your program.
. . .

Please always say where such quotes come from, to avoidcopyright problems and to allow everybody to assess them. It is quite possible for things to be put on websites which are simply wrong. The Java® Language Specification gives the official, definitive, definition of literals, which, as Stephan has said, seems similar to what you showed.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully, this doesn't muddy the waters even more.

There are three terms that need to be teased apart here:

1. Variable. I like this definition: http://www.cs.utah.edu/~germain/PPS/Topics/variables.html
It says that a variable is a symbolic name for (or reference to) information. Examples:

Here, x and str are symbolic names. They represent some information that you will use in your program. They are also variables because the values that they represent can change. EDIT: striking that out because it's a bit ambiguous and open to different interpretations.  I think it's better to say that they are variables because they can be assigned different (or various/varying) values to represent during the execution of the program.  That is, at some point, x might represent the value 7, then at another point in time, it will represent the value 13, and at yet another point, represent the value -26. So, its name, x, is the same but the value it represents can be different depending on what's happening in the program.

2. Literal. I like this definition: http://www.webopedia.com/TERM/L/literal.html
It says that a literal is "a value written exactly as it's meant to be interpreted." Examples:

Here, 5 and "Hello" are literals. 5 is a numeric literal. "Hello" is a String literal. In Java, String literals need to be surrounded by double quotes and all characters in between the quotes are part of the literal value. The quotes are not part of the literal value but they are required so that the Java compiler can tell them apart from symbolic names.

Here, the name of the class declared starting on line 1 is Hello. On line 2, the instance variable message is assigned a value of "Hello" which, again, is a String literal.  Note that because it is surrounded by double quotes, Java interprets the consecutive characters 'H', 'e', 'l', 'l', 'o' as being part of a String literal. On line 1, the same series of characters are interpreted as the name of a class.  On line 4, newMessage, is a symbolic name. It is also referred to as the "formal parameter name" or simply, "the newMessage parameter". On line 5, the instance variable message (declared on line 2) is assigned whatever value the newMessage parameter refers to/represents. The only literal in this code is "Hello" on line 2.

3. Constant. It's confusing to say "constant variable" because those two words are opposites so I'll just stick with "constant". A constant is something that doesn't change its value. By convention, constants in Java are given symbolic names that are in all capitals. Underscores in constant names are used to separate words. However, a name that follows this convention is not necessarily a constant. The convention, when followed, is only for consistency so that it's easy to tell variables and constants apart by sight. Java neither enforces nor recognizes this convention although most modern Java IDEs can and will warn you when you are NOT following it properly. In other words, just because a name follows the convention used for constants, it doesn't necessarily make it one.

Here, STILL_A_VARIABLE is a symbolic name that follows the convention for naming constants in Java. However, it is NOT a constant because its value can change. So, STILL_A_VARIABLE is just, well, still a variable. To make it a constant, you'd have to qualify the declaration with the final keyword.

Here, the symbolic name LUCKY_NUMBER represents a constant value because it's declared as final. This means that once it is assigned a value, which in this case is 7 (a literal right?), no other code can attempt to assign a new value to it. Any code that attempts to do so would result in a compile-time error. Note that because "it's a symbolic name that represents a constant value" is a mouthful, we normally use a more pithy form and simply say that "LUCKY_NUMBER is a constant" to mean the same thing.

Constants are usually declared with the public and static keywords as well. That is, this is how most constants are declared:
The public qualifier makes the symbolic name LUCKY_NUMBER globally visible.  The static qualifier makes LUCKY_NUMBER a class member. Without static, it would be an instance member, which doesn't make much sense semantically.

So, if you had this:

then, the proper way to reference the constant declared on line 2 is LuckyNumber.SEVEN. In this code, LuckyNumber is the class name, SEVEN is a symbolic name and a constant, and 7 is the literal value assigned to it. Specifically, SEVEN is a symbolic name of a constant that is a public class member of LuckyNumber. The value of the LuckyNumber.SEVEN constant was initialized on line 2 by assigning it the literal value 7.

On line 4, pick is a symbolic name (it stands for something) for a variable (because its value can change over time). Specifically, pick is an instance variable because each instance of the class LuckyNumber will have its own value of the variable pick. The value of the pick instance variable is initialized to whatever value the symbolic name SEVEN represents (7 right?). Because it is a variable, however, pick may be assigned different values to represent later on. In this code, only the value 7 is a literal.

Let's assume that the following code is in the same package as the LuckyNumber class:

On line 3, myNum is a symbolic name. It is also a variable because it can be changed. On line 4, first is a variable (and a symbolic name). It is assigned the global constant value that the symbolic name LuckyNumber.SEVEN represents. Because that's a mouthful to say and write, we simply say that "first is assigned the constant LuckyNumber.SEVEN."  Again, SEVEN here is a symbolic name that represents a global constant value and it evaluates to the integer value of 7. That is also the value that gets assigned to the variable first. SEVEN is NOT a literal value though, right?

On line 5, the pick variable of the LuckyNumber instance referenced by the variable myNum is assigned the literal value of 2.

On line 6, another LuckyNumber instance is created and a reference to it assigned to the variable named strange.

On line 7, the pick variable of the LuckyNumber instance referenced by the variable strange is assigned a value that's calculated by adding the value of the constant LuckyNumber.SEVEN and the literal value 6. The resulting value, 13, is what strange.pick represents at that point. The value of the myNum.pick instance variable would still be 2.

One last thing, Line 9 is commented out because it is illegal and will not compile. SEVEN is declared as final in the LuckyNumber class so it cannot be assigned another value after it is initialized. Java will recognize this and throw a compile-time error if you uncomment line 9.  If you remove the final keyword from the declaration of SEVEN, line 9 can be uncommented and it will compile.

In this code (ignoring line 9), only the values 2 and 6 are literals.

Clear as mud now?

Ok, one final note, as I can anticipate that the term "symbolic name" may add to your confusion.  This kind of goes back to what Stephan wrote:

The word "tree" is a word, not a tree.



I suppose you could just say "name" instead of "symbolic name" but it's really just a technical term that computer scientists like to use because it reflects the significance of the name in the program. That is, these names represent or "symbolize" something in your program, so that when you use the name, it is understood that you are really referring to the thing that it stands for or symbolizes, just as your user name symbolizes your online identity, or how a flag symbolizes your country, or how a particular combination of shapes and lines can symbolize a brilliant but eccentric musician.

I hope this helps.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't remember where I read, but, literals are ones, which are known at compile time. In different words - are compile time constants. Non literals are the ones which needs some calculations in order to evaluate their values, and that happens at program's run time.

Two examples demonstrates that. First example won't compile, because variable 'b' couldn't be reached as loop would run forever and it can be defined during compile time as there is supplied literal value:

Next example, quite similar, but would compile. As in loop condition there isn't a literal:

Next example wouldn't compile too:

This would compile:

There is one special case - null. null you could think is a compile time constant, but it isn't, and that is because Java Language Specification states that. And the below code demonstrates that (compiles):

Literals are either primitive type variables actual values, or String's actual values.

JLS wrote:15.28. Constant Expressions


ConstantExpression:
   Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
...

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's very simple: Literals are values which are literally in the source code - values that are directly in the source code itself, and that can for example be assigned to a variable, or passed to a method, or used in any other place where a value is expected.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:It's very simple: Literals are values which are literally in the source code - values that are directly in the source code itself,



Highly agree. If you look at the Java Language Specification for literals, you will find it in the Lexical Structure section -- meaning it is a language definition. It is simply a value in the source code.

Henry
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . Non literals are the ones which needs some calculations in order to evaluate their values, and that happens at program's run time.. . .

If all the operands of an expression are compile‑time constants, then the calculations can be completed at compile time.No sign of 1 + 2 * 3; only 7 appears.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If all the operands of an expression are compile‑time constants, then the calculations can be completed at compile time.


That part is covered in the 15.28. Constant Expressions of JLS if I understand it correctly (at the very bottom). There is mentioned 'final' keyword too.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Jon Ninpoja !!

This question has been selected for the September Journal

Cow!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:i mean is it going to cause me major problems down the line...if i dont understand this right away?



No, it isn't going to cause you problems in the future if you don't understand now what a literal is. But... if you read a few articles and blog posts or whatever about Java you should before long notice that the word "literal" always refers to something like "Hello World" (a string literal) or 23 (an int literal) or something like that. Just like the way you find out the meanings of words in English (or your native language) which you don't initially understand.

So yes, if you don't find yourself able to learn the meaning of elementary Java terminology without having to ask other people, then you are going to have "major problems down the line" when you have to learn things which aren't simple. I don't mean to be harsh or negative but that's how it is.
reply
    Bookmark Topic Watch Topic
  • New Topic