Strings odd one out

J

jk

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
 
B

Bob

K

Karsten Baumgarten

jk said:
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.
 
J

John C. Bollinger

jk said:
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?
 
T

Tilman Bohn

In message <[email protected]>,
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...
 
O

Oscar kind

Tilman Bohn said:
In message <[email protected]>,
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[].
 
T

Tilman Bohn

Tilman Bohn said:
In message <[email protected]>,
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top