I am new to Java, and I have just started reading some books on it, and wud like to know 2 things: 1. what xactly(in simple words) is 'primitive' and 'literal'? 2. What is 'Heap' and 'Stack'? Please elaborate
1. what exactly(in simple words) is 'primitive' and 'literal'?
Primitive: Java has a very specif kind of data type that isn't a class (or object) one. Java's primitive data types are:
1�) byte, short, int and long are known as integral data types and are signed. The char type is integral but unsigned (range from 0 through 2^16-1). float and double are known as floating-point types
1�)They are some rules that govern the process of conversion (explicit by cast or implicit) from one type to an other one, except for boolean that can't be converted to any other one primitive type 2�) You can't (except by using Wrapper classes) convert a primitive type to a class type. 3�) There is another data type, Array, that isn't really a primltive data type nor a class one, but an ordered collection of primitives, object references, or other arrays. There are some rules intended to convert an array to a class (also here implicit or explicit by cast). Literal: A literal is a value specified in the program source, as opposed to one determined at runtime. Literals can represent primitive or string variables, and may appear on the right side of assignements or in method calls. examples: boolean literals:
Some cases it is useful to know that we deal with literals: 1�)Assignement:
Java relaxes its assignement conversion rule when a literal int value is assigned to a narrower type (byte, short, or char), provided the literal value falls within the legal range of primitive type
2�) Equals method:
When a string literal is compiled, the compiler adds an appropriate string to a pool of literal strings. However, if the same literal already appeared as a literal elsewhere in the class, then it is already represented in the pool: the compiler doesn't create another copy; instead, it uses the existing one from the pool
Output: true but
output: false Hope this helps, Cyril.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Joined: May 13, 2003
Thanks a lot Cyril, it was very helpful. I tried the codes that u gave me, but then i had some problem: String s1 = new String("Compare me"); String s2 = "Compare me"; if (s1==s2) System.out.println("true"); else System.out.println("false"); This code returns FALSE....i thot since the literal is already in the pool, it shud have returned TRUE, pls explain....... Also, can u pls tell me, what are the default(if i donot initialize them) values of these primitives.. Thanks
Joined: Jul 02, 2003
This code returns FALSE....i thot since the literal is already in the pool, it shud have returned TRUE, pls explain.......
The result true only appears when you compare two literals. What happens:
when this line compiles, the String literal "Compare me" is added to the pool of literal strings;
Here no new literal "Compare me" is added to the pool. But at runtime, you create an new instance of String, and thus a reference to the new String is assigned to s1 "Compare me" in the pool of literal string and "compare me" as new instance don't more design the same object, so returns false.