This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Literals in Java

 
Sadashiv Borkar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam sorry for this simple question.as 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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
System.out.println("hello");
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.
 
Sadashiv Borkar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
System.out.println(i);
I am printing the value of a variable.
If I say:
System.out.println(26);
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Examples:
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic