aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Primitives and literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Primitives and literals" Watch "Primitives and literals" New topic
Author

Primitives and literals

Dilbert Gilbert
Greenhorn

Joined: May 13, 2003
Posts: 4
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
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
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:
  • boolean
  • char
  • byte
  • short
  • int
  • long
  • float
  • double

  • 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.


    SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
    Dilbert Gilbert
    Greenhorn

    Joined: May 13, 2003
    Posts: 4
    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
    cyril vidal
    Ranch Hand

    Joined: Jul 02, 2003
    Posts: 247
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Primitives and literals