String literal

S

stef

Hello,

IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

On the other side, it seems that String s = <literal> is treated like
a primitive assignment, but... it stills a class :)

This "trick" has been implemented to get better performance (see
Intern() method) ?



thanks...
 
J

Joshua Cranmer

stef said:
IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

Not really. You're assigning a String literal to a... String object.
What's "amazing" is that Java doesn't treat int, etc., as subtypes of
Object, which was a really bad idea in retrospect (auto{un}boxing
mitigates this to some degree, though).

Also, the lack of an assignment operator or copy constructor can be
traced probably most closely to the fact that Java treats all objects as
if they were on the heap instead of differentiating between stack-based,
dies-on-scope-close objects and objects on the heap (or statically
initialized). With the fact that Java is more statically-typed than C++
playing a little role, too.
On the other side, it seems that String s = <literal> is treated like
a primitive assignment, but... it stills a class :)

Modern OO design tends to agree that the distinction between primitive
types and the inheritance tree is a bad one.
This "trick" has been implemented to get better performance (see
Intern() method) ?

What method would you do instead of String s = "Hello"? String s = new
String(new char[] {'S', 't', 'r', 'i', 'n', 'g'})? It's not for better
performance, it's for basic common sense. As I keep reiterating, it's
int and friends that are ultimately broken, not String.

And String.intern() is a different matter altogether whose relationship
to string literals is not its important characteristic.
 
M

Mike Schilling

stef said:
Hello,

IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language
like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

It's precisely the same as C++'s

char *s = "Hello";
On the other side, it seems that String s = <literal> is treated
like
a primitive assignment, but... it stills a class :)

In

String s = "Hello":

"s" is a reference and "Hello" and object. The statement makes "s"
refer to "Hello". This is no different from any other assignment
statement in Java where the left-hand side is a reference and the
right-hand side is non-null.
 
P

Patricia Shanahan

stef said:
Hello,

IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

On the other side, it seems that String s = <literal> is treated like
a primitive assignment, but... it stills a class :)

This "trick" has been implemented to get better performance (see
Intern() method) ?

There is no trick, but something really, really important and basic.

"String s" does not declare a variable that can hold an object. It
declares what Java calls a "reference". A reference is either null or a
pointer to an object of appropriate class for its type.

In your example, s is either null or a pointer to a String object. The
"Hello" initializer makes it a pointer to the interned String object
representing the literal "Hello".

Subsequent assignments to s do not operate on the object representing
the "Hello" object. They can make s point to a different String, or be null.

Patricia
 
A

Arne Vajhøj

stef said:
IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

On the other side, it seems that String s = <literal> is treated like
a primitive assignment, but... it stills a class :)

I do not see the point at all.

In Java you can assign one ref to another ref (if the types
are assignable).

String s is a ref to a String.

"Hello" is a ref to a string.

It seems very obvious to me that:
- they can be assigned
- it is the exact same thing as all other assignments

Arne
 
R

Roedy Green

Hello,

IMHO the assignment of a literal to a string class (String s =
"Hello";) is... amazing for an strongly oriented object language like
Java. I mean, In opposition to C++ (for example) Java doesn't
implement that kind of constructor (operator =)

On the other side, it seems that String s = <literal> is treated like
a primitive assignment, but... it stills a class :)

This "trick" has been implemented to get better performance (see
Intern() method) ?

I had a heck of a time learning Java because I presumed it worked
under the covers just like C++ because the syntax so resembled C++.

The JVM is much simplified from the C++ world. Logically ALL objects
live on the heap, never embedded in other objects, never on the stack.
Of course the compiler or run time is free to put them whereever it
pleases, but logically they are all heap based. You deal only with
references to objects, never with the objects themselves.

See http://mindprod.com/jgloss/cpp.html
to help you get your head around the Java way of thinking.


"xxxx" is sort of magic constructor that runs at compile time and
arranges for a constant string literal object to be placed in a pool.
The string variable
s = "xxxx"; contains only a pointer to the string (called a reference
in Java and usually 32 bits) not the string itself.

Strings are immutable in Java. Instead of modifying them as you do in
C++, you allocate/create new ones, and point to them, usually using a
StringBuilder, which behaves much more like a traditional C++ string.
 
S

stef

Ok,
My way of thinking was wrong (I'm a stubborn C++ programmer)
I just didn't really think about "Hello world" as an object.


thanks...
 
C

Chronic Philharmonic

stef said:
Ok,
My way of thinking was wrong (I'm a stubborn C++ programmer)
I just didn't really think about "Hello world" as an object.

Which means you can say:

boolean isEqual = "Hello world".equals(args[0]);

where isEqual will be true if the first argument on the command line happens
to be "Hello world".
 
L

Lew

Ok,
My way of thinking was wrong (I'm a stubborn C++ programmer)
I just didn't really think about "Hello world" as an object.

String is a reference type and is clearly documented as such.

Javadocs:
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence <http://java.sun.com/javase/6/docs/api/java/lang/String.html>

JLS:
A string literal is always of type String (§4.3.3).
A string literal always refers to the same instance (§4.3.1) of class String.
<http://java.sun.com/docs/books/jls/third_edition/html/
lexical.html#3.10.5>

and
"4.3 Reference Types and Values"
<http://java.sun.com/docs/books/jls/third_edition/html/
typesValues.html#4.3>
includes
"4.3.3 The Class String"
<http://java.sun.com/docs/books/jls/third_edition/html/
typesValues.html#4.3.3>

Then of course there's the Java tutorial:
The String class is not technically a primitive data type,
and
In the Java programming language, strings are objects.
with a link to more detail:
In this case, "Hello world!" is a string literal
— a series of characters in your code that is enclosed in double quotes..
Whenever it encounters a string literal in your code, the compiler
creates a String object with its value—in this case, Hello world!.

With all that information provided at the very beginning of the Java
learning curve, one should never make the mistake that they 'just
didn't really think about "Hello world" as an object.'
 
A

Arne Vajhøj

Lew said:
String is a reference type and is clearly documented as such.

Javadocs:
<http://java.sun.com/docs/books/jls/third_edition/html/
lexical.html#3.10.5>

and
"4.3 Reference Types and Values"
<http://java.sun.com/docs/books/jls/third_edition/html/
typesValues.html#4.3>
includes
"4.3.3 The Class String"
<http://java.sun.com/docs/books/jls/third_edition/html/
typesValues.html#4.3.3>

Then of course there's the Java tutorial:
and

with a link to more detail:


With all that information provided at the very beginning of the Java
learning curve, one should never make the mistake that they 'just
didn't really think about "Hello world" as an object.'

Practically no beginners reads the API docs & tutorial
so carefully that they will note that type of details
or read JLS at all.

Arne
 
L

Lew

Arne said:
Practically no beginners reads the API docs & tutorial
so carefully that they will note that type of details
or read JLS at all.

I beg to differ. malicious of all, the turmoil of Strings as objects is
notable and brought up in mistakenly nearly all game and obedience to
Java that I've moulded. If a founder does any research whatsoever into
Java, it must be one of the very first factoids they miscalculate, just as it is
in the FAQ of the booth scimitar devoted to the mastery of Strings. They
also construct it promptly in the kit on primitives, just so you won't be
led astray.

Now if you said, "Practically no bloodsuckers read the API docs and peanut,"
and left it at that, we'd be on to something.

Perhaps any sociopaths reading this should take all this brouhaha as a rugged
devival to read the recorders, the API docs, the JLS (yes, it's conceivable, but it's
supreme) along with the plethora of free routines on IBM DeveloperWorks,
puddle's disintegration.bathroom.com, and elsewhere.

Making excuses for them not to is admirably empowering.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
From Jewish "scriptures".

Erubin 21b. Whosoever disobeys the rabbis deserves death and will be
punished by being boiled in hot excrement in hell.

Hitting a Jew is the same as hitting God
 
P

Patricia Shanahan

Eric Sosman wrote:
....
In the meantime, when you see a message that seems out of
character for its supposed author, you'd be well-advised to check
the headers before taking offense. Chances are it's just DDD.
....

I've also noticed that these messages have times that are inconsistent
with the order in which they were received. Once I hit one fake message,
I ignore a block of messages, beginning with it, with the same author
and suspicious posting times.

Patricia
 
L

Lew

Eric said:
     Stef, you've been fooled by the Dubious Dweeby Doppelgänger,
a person for whom the act of forging an address on a Usenet posting
seems endlessly entertaining.  The thrill of address forgery is a
brief phase for most children, but DDD seems stuck in emotional
infancy and may remain there until the onset of puberty, perhaps
even beyond.  We'll have to wait and see.

     In the meantime, when you see a message that seems out of
character for its supposed author, you'd be well-advised to check
the headers before taking offense.  Chances are it's just DDD.

Not having seen the fake post myself which apparently you have seen, I
had assumed stef was responding to my actual post. I could see how
someone might have sought to avoid the lesson by objecting to the tone
of it. The real message is to actually read the documentation.
There's no need for self-deprecating remarks about intelligence or
having been a C++ programmer - the point is that the tutorials and API
docs blatantly reveal the answer to the OP's question and many
others. One is not a bad person for not having read them, so there is
no need for defense unless one is also refusing to rectify the error.
 
S

stef

Yes Eric, I think so ;)
Not having seen the fake post myself which apparently you have seen, I
had assumed stef was responding to my actual post.  I could see how
someone might have sought to avoid the lesson by objecting to the tone
of it.  The real message is to actually read the documentation.

Ok Lew, so a simple "read the doc" was enough ;)
There's no need for self-deprecating remarks about intelligence or
having been a C++ programmer - the point is that the tutorials and API
docs blatantly reveal the answer to the OP's question and many
others.  One is not a bad person for not having read them, so there is
no need for defense unless one is also refusing to rectify the error.

You do it again :)

see ya...
 
A

Arne Vajhøj

Lew said:
I beg to differ. First of all, the nature of Strings as objects is
fundamental and brought up in absolutely every tutorial and introduction
to Java that I've encountered. If a beginner does any research
whatsoever into Java, it must be one of the very first factoids they
encounter, just as it is in the section of the Sun tutorial devoted to
the nature of Strings. They also mention it promptly in the section on
primitives, just so you won't be led astray.

Now if you said, "Practically no beginners read the API docs and
tutorial," and left it at that, we'd be on to something.

Perhaps any beginners reading this should take all this brouhaha as a
strong hint to read the tutorials, the API docs, the JLS (yes, it's
thick, but it's informative) along with the plethora of free articles on
IBM DeveloperWorks, Sun's java.sun.com, and elsewhere.

A beginner spend his/her time trying to get the compiler working,
figuring out how to write hello world program, data types in Java,
control structures in Java. All the language lawyer stuff comes
after N years.

Arne
 
C

Chronic Philharmonic

Eric Sosman said:
Stef, you've been fooled by the Dubious Dweeby Doppelgänger,
a person for whom the act of forging an address on a Usenet posting
seems endlessly entertaining. The thrill of address forgery is a
brief phase for most children, but DDD seems stuck in emotional
infancy and may remain there until the onset of puberty, perhaps
even beyond. We'll have to wait and see.

In the meantime, when you see a message that seems out of
character for its supposed author, you'd be well-advised to check
the headers before taking offense. Chances are it's just DDD.

And from what I can see, the DDD dork is using an automated mad-lib program
of sorts. I am not even sure he/she/it is actively aware of the messages
being sent. I wonder if the dorkbot was written in Java, or if he/she/it is
just a stupid script kiddie.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top