Re: Strings odd one out

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 02/21/05


Date: Mon, 21 Feb 2005 11:14:36 -0500

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
jobollin@indiana.edu


Relevant Pages

  • JNI C->java calls with multiple parameters
    ... I'm trying to design a JNI wrapper around a Java library (POI - excel ... case needs to be passed the row and column numbers plus the string ... All the examples I've seen set up an object array ...
    (comp.lang.java.programmer)
  • Re: Deadlock in Solaris 8
    ... Are you using stl strings? ... operator created a new string instance from the constant data. ... which killing performance due to the multiprocessor environment. ...
    (comp.unix.solaris)
  • Re: Fast string operations
    ... the char[] for each string cached with the string, or is a new one created ... the string is full of whitespace. ... >> ToLowergenerates a new string instance as does Trim. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fast string operations
    ... Regardless, even with your suggestion, the .Trim() still creates a new ... > My assumption here is that you already intend to trim the string before it ... >> ToLowergenerates a new string instance as does Trim. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: System.String: It doesnt seem to act like a reference type.
    ... First, because Strings are immutable, changing the value of a string ... doesn't really change the string _in situ_ in memory. ... creates a whole new string and points the String reference to it. ... the new String instance instead of the old ...
    (microsoft.public.dotnet.languages.csharp)

Loading