This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Primatives and Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Primatives and Literals" Watch "Primatives and Literals" New topic
Author

Primatives and Literals

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
What is the difference between primatives and literals. (if that is a fair question) and how are both treated?


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Dale DeMott:
What is the difference between primatives and literals. (if that is a fair question) and how are both treated?


My understanding is: literal concept includes primative concept.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
primitive data types are non-composite (read: non-Object) data types. int, char, byte, etc. are primitives.
Literals, on the other hand, are the opposite of variables. They are values that are specified, well, literally in the code.
example:

in this example 1, 'A', 15L, and "Hello, World!" are literals. aValue, aChar, aLong, and aString are variables.
1, 'A', 15L, anInt, aChar, and aLong are primitives. "Hello, World!" is a literal that is an Object; Strings are the only objects that can be literals (This was done becuase Strings are such commonly used critters).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

From the JLS :
A literal is the source code representation of a value of a primitive type (�4.2), the String type (�4.3.3), or the null type (�4.1):
A primitive is one of boolean, byte, short, char, int, long, float, double.
What do you mean by "how are they treated"? treated in what respect?

Don Liu:

My understanding is: literal concept includes primative concept.


Junilu - [How to Ask Questions] [How to Answer Questions]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Joel and Dale
According to Don said
My understanding is: literal concept includes primative concept.

The statement is true.
And some special case occur in primative data type such as character array char[]-->string which use the object concept although it is primative data type.
So in the Set theory
Primative data type is subset of literal
I hope that I can answer your question



Francis Siu
SCJP, MCDBA
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Thanks for your answers. Makes things much clearer.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Neither primitives not literals are subsets or supersets of each other. Examples:

Primitive literals:
1, 'a', false

Literal but not primitive:
"", "a", "This is a string literal", Object.class, ArrayList.class, int.class, int[].class

Primitive but not literal:
Any variable with primitive type, e.g. int

Not pimitive or literal:
Any variable with reference type, e.g. String, ArrayList, Object


"I'm not back." - Bill Harding, Twister
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Yes. I do realize this. After someone stated that they are values and not variables, it made everything quite clear.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Dale:
I forget to bring all my SCJP books with me. The first thing I would like to do is check all the definetions. I feel this question isn't very fair, Primatives and Literals are not in the same domain. Primatives is opposite to compositive. Literals is something broader. I agree with siu chung man.
HTH.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Jim
Sorry Jim,
I may be misunderstanding some concept in Java

Neither primitives not literals are subsets or supersets of each other. Examples:
Primitive literals:
1, 'a', false

Literal but not primitive:
"", "a", "This is a string literal", Object.class, ArrayList.class, int.class, int[].class

(*) Primitive but not literal:
Any variable with primitive type, e.g. int

I have question in
(*)Primitive but not literal:
Is (primitive type, e.g. int) including in literal?
Because I do not make sure integer primitive data type which is not literal.
thanks
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Say you had:

the anInt is a primitive but is not a literal; it is a variable.
Also, some primitive types do not have literals. (byte and short come to mind). When you say

the 5 is an integer literal which is cast down to a byte.
Thus, not all primitives are literals, and not all literals are primitives.
Hope this clears up any confusion.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I just have a small question:
int n = 23 ;
here, 'int', 'n', '=', and '23' are all literals. aren't they?
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
oo........
Please forget it, I do not want to discuss this little thing.
A little little definition differ between each other. :roll:
Anyway,thanks
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I feel I can choose the correct one out of 4 choices in the exam....
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Originally posted by Don Liu:
I just have a small question:
int n = 23 ;
here, 'int', 'n', '=', and '23' are all literals. aren't they?

They are all tokens in the languange, not literals. Only 23 is a literal. (for that matter, ';' is a token, as well)
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Joel McNary:

They are all tokens in the languange, not literals. Only 23 is a literal. (for that matter, ';' is a token, as well)

i guess i get it.
token is the literals reserved for the system. but isn't it true that token is a subset of literal. if 'int' is not reserved by system, i guess i can use it like '23' or 'moose'? am i right?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Urm... I'm not sure what definition of "token" Joel is using. According to the JLS though, literals are a subset of tokens. Everything in a valid .java file is (or is part of) either a comment, a token, or whitespace. (Note that spaces inside comments or String literals are not considered "whitespace" in this context.) Tokens can be identifiers, keywords, literals, separators, or operators - these five groups do not overlap.
if 'int' is not reserved by system, i guess i can use it like '23' or 'moose'? am i right?
int is a keyword, therefore it cannot be used as an identifier. It can't be a literal unless it's enclosed in double quotes (since it's clearly not a numeric, boolean, or class literal).
23 is not a keyword; it can't be an identifier since it doesn't start with a letter; it's a numeric (int) literal.
moose is not a keyword; it can be an identifier; it can't be a literal unless surrounded with double quotes.
To be fair, I don't think it really matters whether you know what the definition of a token is, unless you're writing a compiler. But understanding the differences between identifiers, keywords, literals, separators, and operators is useful.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Yup, that's the definition of "token" that I was using...
I do have a (small) background in compilers and interpreters, so "token" just rolls off the tounge.
But Jim is (as usual ) right:
int - keyword
n - identifier
= - operator
23 - literal
; - separator
all of which make up the larger "token" categorization.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
thanks a lot!
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Jim Yingst:
Urm... I'm not sure what definition of "token" Joel is using. According to the JLS though, literals are a subset of tokens. Everything in a valid .java file is (or is part of) either a comment, a token, or whitespace. (Note that spaces inside comments or String literals are not considered "whitespace" in this context.) Tokens can be identifiers, keywords, literals, separators, or operators - these five groups do not overlap.
if 'int' is not reserved by system, i guess i can use it like '23' or 'moose'? am i right?
int is a keyword, therefore it cannot be used as an identifier. It can't be a literal unless it's enclosed in double quotes (since it's clearly not a numeric, boolean, or class literal).
23 is not a keyword; it can't be an identifier since it doesn't start with a letter; it's a numeric (int) literal.
moose is not a keyword; it can be an identifier; it can't be a literal unless surrounded with double quotes.
To be fair, I don't think it really matters whether you know what the definition of a token is, unless you're writing a compiler. But understanding the differences between identifiers, keywords, literals, separators, and operators is useful.

Thanks for the help! now i have to start all over, again!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Joel]: Yup, that's the definition of "token" that I was using...
Thanks - from your phrasing I thought maybe "token" and "literal" were intended as separate (non-overlapping) categories. I should have known better...
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Tokens can be identifiers, keywords, literals, separators, or operators - these five groups do not overlap.

cool, i guess this is really what i am looking for. now i am looking forward to scjp 1.4
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Are we going to need this much detail for any of the tests (tokens etc)
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Dale DeMott:
Are we going to need this much detail for any of the tests (tokens etc)


By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)

it looks like we have better be prepared!
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Tokens can be identifiers, keywords, literals, separators, or operators - these five groups do not overlap.

What is the difference between primatives and literals. (if that is a fair question) and how are both treated?

in the original question, it mentioned 'primative', in my previous post, i said this two concepts are from different domains. and i still think so. otherwise, where does 'primative' fit in here?
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
For these type of questions, it is best to refer to the Java Language Specification. Nothing beats the JLS. Thanks Junilu Lacar for pointing out the relevent sections in the JLS.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
thanks!
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Dale DeMott:
What is the difference between primatives and literals. (if that is a fair question) and how are both treated?

i guess this isn't a fair question!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It's kind of like "what's the difference between apples and seeds?" You should know what seeds are, and you should know what apples are, but asking what's the "difference" is kind of a silly question. They're very different things. A slightly better question would be what do they have in common? The answer is, not much.
[ May 08, 2003: Message edited by: Jim Yingst ]
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I agree Jim. After I understood the answer, I realized that they really are quite different.
 
 
subject: Primatives and Literals
 
Similar Threads
JNI, setting/setting arrays
Doubt in Mock Question
Boxing
Are JAVA primitives valid RMI IIOP
deadlock