aspose file tools*
The moose likes Beginning Java and the fly likes Literals in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Literals in Java" Watch "Literals in Java" New topic

Literals in Java

Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
Iam sorry for this simple being under the learning stage ihave seen many books referring to this topic but nobody explains what is literals.
What does exactly literals stands for in java language.where is it exactly used by java(i.e compilers..).What is the exact use of the literals in java
thanks in advance............

Frank Carver

Joined: Jan 07, 1999
Posts: 6920
I'm not entirely sure what you are asking, but...
In java a literal is any value which isn't in a variable. For example, in the statement:
the "hello" is a literal String. Similarly for:
double d = Math.floor(3.14 * r);
the 3.14 is a literal double.
Most people consider it good program design for each literal to appear only once, in an assignment statement, so that it can be referred to by name everywhere it is used:
public static final int MAX_ENTRIES = 35;
This makes it easier and safer to change the value if the program requirements change.
I hope this helps.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
thanks frank but
if i say int i = 34
will 34 become a literal
then the question is can i say that the "value" of i which is 34 is a "literal" of type integer why cant i say it directly as value of i instead of literal
Iam confused with the value of i and the literal part of it.
or is literal just like macros in case of c++ just like you said
Please could you just breifly describe what literal is or you can give me any resource where i can find detail explaination of what literals r and where java exactly uses it....?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
If you say:
int i = 34;
the literal integer 34 is assigned to the integer variable i.
i is a variable which contains a value. If I say:
I am printing the value of a variable.
If I say:
I am printing the value of a literal.
Perhaps it will help if you think of literals as only existing in the source code for your program. In order to do anything with a literal it must be assigned to variable, used in an expression or passed as a parameter.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
According to Khalid's book, "A literal denotes a constant value. This value can be numerical (integer or floating-point), character, boolean or a string.
Integer 2000 0 -7
Floating-point 3.14 -3.14 .5 0.5
Character 'a' 'A' '0' '*' ')'
Boolean true false
String "abba" "3.14" "for" "a piece of the action"
hope this helps
Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
now let me talk about one more problem.Following are the given ranges Integral Types
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807
NOW The point is that whether the number say "50" what type of literal will it be whether a BYTE, SHORT , INTEGER, OR LONG. If you see that the number "50" is comming between all the ranges of the above Integral types.You see Iam Confused Here
Or there are no literal for byte short and long
Similarly will be the case for the Floating point types
Thanx in advance
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Unless otherwise modified, any literal number without a fractional part is considered to be an int, and any literal number with a fractional part is considered to be a double.
I agree. Here's the link:
subject: Literals in Java