This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primatives and Literals

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between primatives and literals. (if that is a fair question) and how are both treated?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Junilu Lacar
Bartender
Pie
Posts: 7268
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers. Makes things much clearer.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I do realize this. After someone stated that they are values and not variables, it made everything quite clear.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just have a small question:
int n = 23 ;
here, 'int', 'n', '=', and '23' are all literals. aren't they?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel I can choose the correct one out of 4 choices in the exam....
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot!
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are we going to need this much detail for any of the tests (tokens etc)
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree Jim. After I understood the answer, I realized that they really are quite different.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic