Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Advanced-java] Odd question


  • From: Brian.Cain@xxxxxxxxxxxx (Cain Brian-BCAIN1)
  • Subject: [Advanced-java] Odd question
  • Date: Thu, 28 Mar 2002 17:12:06 -0600

> -----Original Message-----
> From: Alvin Wang [mailto:xwang@xxxxxxxxxxxxxxxxx]
> Importance: High
> 
> Hi! It is a little odd. Let us say:

Hi. Yes, it is odd, isn't it?

> String str = new String("abc");
> 
> We all know that (str == "abc") is false. My question is how 
> to manipulate
> str to make str point to "abc" (because str knows that its 
> value itself is
> "abc"), or in the other word, to make (str == "abc") true?

Perhaps you don't understand what string literals do when they show up in code.  That's not to say that I do, but in any case, it's hard to figure out exactly what you're trying to do.  I'm assuming you know that str.equals("abc") is what you would use for testing string equality.  

Do you really want to know if str and the literal "abc" have the same offset?  (I believe that's the comparison performed by the '==' operator on references).  What you're trying to do looks to me like (new Foo() == new Foo()), which should probably never be evaluated as true.  

In fact, your statement might even be something like (new Foo() == new Foo(new Foo())).  Literals enclosed in double quotes are of type String, and you pass a literal to String's constructor (which creates a copy of the same String passed in).  

Long story short -- you're naughty if you're trying to do any pointer arithmetic in Java in the first place, but I'd say the intern() method may have something to do with what you're trying to accomplish.  I hate to say RTFM, but RTFM.

> Just want to clarify, my question is, in another word, how to find the
> reference to "abc" from str? Thanks!

Well, here we have a problem with string literals again.  If you're looking for the address-of operator ('&'), you've come to the wrong language/paradigm.  Here's a comparison you'll like: ("Pointers" != "References").  

I believe that ("abc" != "abc") will (always?) evaluate to true (maybe it's compiler-dependent) because both literals can occupy different places in memory.  It would only be an optimization if it didn't.  The JLS probably clears this up.

-Brian