Strings odd one out

Discussion in 'Java' started by jk, Feb 20, 2005.

  1. jk

    jk Guest

    Hi:

    My understanding of classes is that a class serves as a wrapper for
    a data, and the operations that can be performed on the given data.
    This is not a very specific definition, but is the motivation for
    my question. The instances of classes are called by their handle.

    For instance: adding a string to a vector
    Vector v = new Vector();
    v.add("ABC");

    Strings are different in that they can be called by
    a handle, or they can be called by a literal String.

    eg:
    String s = "ABC";
    int len1 = s.length();
    int len2 = "ABC".length()

    So, this seems 'inside out' ...
    The data that should be wrapped by the class
    is used to invoke methods upon itself from
    outside the class.

    Can someone explain how this is done...

    Thanks in Advance
    John
    jk, Feb 20, 2005
    #1
    1. Advertising

  2. jk

    Bob Guest

    jk wrote:

    > Strings are different in that they can be called by
    > a handle, or they can be called by a literal String.
    >
    > eg:
    > String s = "ABC";
    > int len1 = s.length();
    > int len2 = "ABC".length()
    >
    > So, this seems 'inside out' ...
    > The data that should be wrapped by the class
    > is used to invoke methods upon itself from
    > outside the class.


    http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#26992

    The literal is a reference to an instance of class String.
    --
    Bob
    Bob, Feb 20, 2005
    #2
    1. Advertising

  3. jk wrote:
    > Hi:
    >
    > My understanding of classes is that a class serves as a wrapper for
    > a data, and the operations that can be performed on the given data.
    > This is not a very specific definition, but is the motivation for
    > my question. The instances of classes are called by their handle.
    >
    > For instance: adding a string to a vector
    > Vector v = new Vector();
    > v.add("ABC");
    >
    > Strings are different in that they can be called by
    > a handle, or they can be called by a literal String.
    >
    > eg:
    > String s = "ABC";
    > int len1 = s.length();
    > int len2 = "ABC".length()
    >
    > So, this seems 'inside out' ...
    > The data that should be wrapped by the class
    > is used to invoke methods upon itself from
    > outside the class.
    >
    > Can someone explain how this is done...


    The compiler creates a temporary instance of String containing "ABC",
    then invokes its length() method to assign the value to len2 and removes
    the temporary object when he's done.

    --
    Regards,
    Karsten
    Karsten Baumgarten, Feb 20, 2005
    #3
  4. jk wrote:

    > Hi:
    >
    > My understanding of classes is that a class serves as a wrapper for
    > a data, and the operations that can be performed on the given data.
    > This is not a very specific definition, but is the motivation for
    > my question. The instances of classes are called by their handle.
    >
    > For instance: adding a string to a vector
    > Vector v = new Vector();
    > v.add("ABC");
    >
    > Strings are different in that they can be called by
    > a handle, or they can be called by a literal String.
    >
    > eg:
    > String s = "ABC";
    > int len1 = s.length();
    > int len2 = "ABC".length()
    >
    > So, this seems 'inside out' ...
    > The data that should be wrapped by the class
    > is used to invoke methods upon itself from
    > outside the class.


    You are confusing the program with its source code. A Java String
    literal is a representation of a full-fledged String instance that works
    just like any other String instance when the program runs. It is
    impossible for a program to determine at runtime from a String reference
    whether or not the referent corresponds to a String literal in the Java
    source.

    As for why Java does it this way, consider the question of practicality:
    how would you specify the literal contents of a String at compile-time
    if you could not use a double-quoted character sequence? If you don't
    want to give up the double-quoted character sequence then what could it
    represent *other* than a String object?

    --
    John Bollinger
    John C. Bollinger, Feb 21, 2005
    #4
  5. jk

    Tilman Bohn Guest

    In message <cvd1dc$pji$>,
    John C. Bollinger wrote on Mon, 21 Feb 2005 11:14:36 -0500:

    [...]
    > As for why Java does it this way, consider the question of practicality:
    > how would you specify the literal contents of a String at compile-time
    > if you could not use a double-quoted character sequence? If you don't
    > want to give up the double-quoted character sequence then what could it
    > represent *other* than a String object?


    A string primitive? ;-) String is after all the only non-primitive
    value type that has its own literal syntax (autoboxing for the others
    notwithstanding), if I'm not mistaken. I suppose anything else would
    have made managing literal strings in Java almost intolerable...

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 21, 2005
    #5
  6. jk

    Oscar kind Guest

    Tilman Bohn <> wrote:
    > In message <cvd1dc$pji$>,
    > John C. Bollinger wrote on Mon, 21 Feb 2005 11:14:36 -0500:
    >
    > [...]
    >> As for why Java does it this way, consider the question of practicality:
    >> how would you specify the literal contents of a String at compile-time
    >> if you could not use a double-quoted character sequence? If you don't
    >> want to give up the double-quoted character sequence then what could it
    >> represent *other* than a String object?

    >
    > A string primitive? ;-) String is after all the only non-primitive
    > value type that has its own literal syntax (autoboxing for the others
    > notwithstanding), if I'm not mistaken. I suppose anything else would
    > have made managing literal strings in Java almost intolerable...


    Of course String is not a primitive: a String is a list of characters.
    Therefore, it cannot be a primitive. At most is is a char[].


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Feb 21, 2005
    #6
  7. jk

    Tilman Bohn Guest

    In message <>,
    Oscar kind wrote on Mon, 21 Feb 2005 19:36:12 +0100:

    > Tilman Bohn <> wrote:
    >> In message <cvd1dc$pji$>,
    >> John C. Bollinger wrote on Mon, 21 Feb 2005 11:14:36 -0500:

    [...]
    >>> then what could it represent *other* than a String object?

    >>
    >> A string primitive? ;-) String is after all the only non-primitive
    >> value type that has its own literal syntax (autoboxing for the others
    >> notwithstanding), if I'm not mistaken. I suppose anything else would
    >> have made managing literal strings in Java almost intolerable...

    >
    > Of course String is not a primitive:


    How did what I wrote give you the idea I thought otherwise? My tongue
    was firmly in my cheek. I thought that was obvious.

    > a String is a list of characters.
    > Therefore, it cannot be a primitive.


    In the context of above remark (i.e., hypothetically speaking), this
    argument doesn't hold any more water than claiming an integer or even a
    byte could not be primitive because it is a list of bits.

    > At most is is a char[].


    Internally, that's of course what it is. However, there is nothing in
    principle that would prevent Java from having a primitive string type
    just like it has primitive numeric and boolean types. (At least I don't
    see it.) So in reply to John's rhetorical question regarding
    practicality, one could very well imagine that the double-quoted
    literals that one `wouldn't want to give up' represented primitive
    strings. Not that it would be sensible, of course. But Jesus, it was a
    joke, ok?

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 21, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. newsgroups.comcast.net

    Odd error from one ip address only

    newsgroups.comcast.net, Apr 5, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    418
    Mahhek
    Apr 21, 2006
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    724
    Malcolm
    Jun 24, 2006
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,050
    Richard Heathfield
    Feb 18, 2007
  4. Plateriot

    2 connection strings, one SQL one network MDB

    Plateriot, Jun 5, 2008, in forum: ASP .Net Security
    Replies:
    3
    Views:
    263
    Misbah Arefin
    Jun 9, 2008
  5. Charlie Ca
    Replies:
    4
    Views:
    108
    Robert Klemme
    Mar 25, 2010
Loading...

Share This Page