wood burning stoves
The moose likes Beginning Java and the fly likes Reagrding literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reagrding literals" Watch "Reagrding literals" New topic

Reagrding literals

sumit bajaj

Joined: Jun 29, 2007
Posts: 10
Qn. Is the double and long primitive types are also known as literals.?

I feel the ans is yes. But one site say it is wrong.

please clarify.

Thanks in advance.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585

Yes they are.

Regards ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
sumit bajaj

Joined: Jun 29, 2007
Posts: 10
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Welcome to the Ranch.

What a literal is, is when you write the actual value of the variable in the code.

There "Campbell" is a String literal, because I wrote the actual value of the String, and 123 is an int literal because I wrote the exact value of the int.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Well, I'd say double and int are primitive types that can be assigned a literal value, but I wouldn't say double and int are literals.

String s = "Cameron";

That would be assigning a literal value to the String s, because "Cameron" is hardcoded.

double d = 10.0;
int eresting = 50;

Both of those lines of code assign literal values to the variables d and eresting. You see, 10 and 50 are hardcoded into the program.

double trouble = d * eresting;

Well, trouble wouldn't really be assigned a literal value, because the value trouble takes on can vary depending upon how d and eresting are initialized.

From the Sun tutorials on what a litera is:

A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it's possible to assign a literal to a variable of a primitive type:

boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;

Sun Tutorial on Primitive Types and the Definition of a Literal

One of the things that I found interesting is that true, false and null are actually considered literal values, and not necessarily keywords in Java. It's just that they're used so frequently, that most people consider them to be true Java keywords, but in fact, they're just literal values.

-Cameron McKenzie
I agree. Here's the link: http://aspose.com/file-tools
subject: Reagrding literals
It's not a secret anymore!