Hi Dilbert,
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:
booleancharbyteshortintlongfloatdouble 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:
char literals:
Integral literals:
String 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
but
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
Consequence:
Output: true
but
output: false
Hope this helps,
Cyril.