• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Primitives and literals

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Dilbert Gilbert
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic