Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: why is the most simple thing not working?

  1. #1
    Majora94 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default why is the most simple thing not working?

    This may be stupid but why does the if-block not execute? Both of the strings should be the same. Either I am seriously missing something or something has gone fatally wrong.

    Java Code:
    String test = "blah552";
    String words = test.replaceAll("[0-9]","");
    System.out.print(words);
    if(words == "blah")
        System.out.print("It worked");

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: why is the most simple thing not working?

    Don't compare strings with ==, instead use the .equals() method.

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    Because words and "blah" are two different references of two different objects.
    Last edited by diamonddragon; 01-29-2012 at 08:20 AM.

  4. #4
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: why is the most simple thing not working?

    because you used == instead of equals.

    use if(words.equals("blah")) instead and it should work.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    why does the if-block not execute?
    (I think this is the correct way of posing the question. It is precisely the simplicity that would suggest the code is working correctly.)

    When you compare two expressions with == what is compared is the value of the expressions. In the case of words and "blah" these values are reference values and quite often we can have different reference values (references to different objects) but we still want to consider the objects they reference as the "same" in some sense. In this case words and "blah" have different values (refer to different things), but we would expect that the two different things the values refer to are made up of the same characters in the same order.

    The solution in such cases is to use the equals() method. Every class has an equals() method and its job is to report when the (possible distinct) instances can be considered equal. Each class defines equality by its own lights. In the case of String, equality is defined to be "the same characters in the same order".

    So try

    Java Code:
    if(words.equals("blah"))
    This point applies to any class, not just String.
    Last edited by pbrockway2; 01-29-2012 at 03:25 AM.

  6. #6
    Majora94 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: why is the most simple thing not working?

    Thank you so much. I have spent about a year programing in c++ and never had this problem.

    I need to stop running into new languages without relearning the basics first.

  7. #7
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by pbrockway2 View Post
    (I think this is the correct way of posing the question. It is precisely the simplicity that would suggest the code is working correctly.)

    When you compare two expressions with == what is compared is the value of the expressions. In the case of words and "blah" these values are reference values
    Isn't "blah" anonimous object of String type?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by diamonddragon View Post
    Isn't "blah" anonimous object of String type?
    I have heard of anonymous classes but not anonymous objects. No, blah is nothing but a String object pure and simple.

  9. #9
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by Fubarable View Post
    I have heard of anonymous classes but not anonymous objects. No, blah is nothing but a String object pure and simple.
    I'v read somewhere that instance of class that is not referenced to any variable is called anonymous object.

    Say:
    Java Code:
    new Object();
    Last edited by diamonddragon; 01-29-2012 at 05:21 AM.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by diamonddragon View Post
    Isn't "blah" anonimous object of String type?
    <jargon>

    "blah" is a string literal. As Fubarable points out there is no such thing as an anonymous object defined in the Java language.

    In the context of the OP's question (using "blah" in connection with ==), "blah" is also an expression as is words. More particularly "blah" is a constant expression.

    As I said before == compares the values of these expressions. In this context == and != are said to be reference equality operators. The behaviour of reference equality operators is described in a way which anticipates what the OP observed: "While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t)".

    </jargon>

    I have spent about a year programing in c++ and never had this problem.
    You can think of reference values (the values of the expressions "blah" and words) as pointers. There is, however, no pointer arithmetic: all you can do with these pointers is dereference them with dot - eg, "blah".length() or words.length() - or compare them with == or !=. Much the same thing would occur in C/C++ if you compared two char* for equality. The pointers may well compare as different even if the memory pointed to consisted of the same sequence of char values.
    Last edited by pbrockway2; 01-29-2012 at 04:49 AM.
    diamonddragon likes this.

  11. #11
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    So, does it means "blah" is String instance and, at the same time, reference to itself?

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by diamonddragon View Post
    So, does it means "blah" is String instance and, at the same time, reference to itself?
    You, me, and everybody else can think of and talk about "blah" as a String instance, or as a String object. But that's not how the JLS defines things. (the fact that the JLS does not follow the casual speech of the rest of us is one reason why my post marked its "wisdom" as <jargon>).

    The way things are actually defined is that those six characters - quote-b-l-a-h-quote - constitute a constant expression. This expression has a value which is used with the reference equality operators. The value of the expression is a reference to a String object or instance.

    So, no, "blah" is not a String instance: it is a String literal, a constant expression, whose value is a reference to a String instance.

    The JLS guarantees that that quote-b-l-a-h-quote will always refer to the same String instance. (see my link before to what a String literal is) And that's a good thing. Because it means we can say "blah" is "a String instance" instead of the more ponderous "a constant expression whose value is a reference to a String instance" with out getting into trouble. There is just one unique String instance associated with the 6 characters "blah" appearing like that in some source code, even if there may be lots of String instances with exactly that sequence of 4 characters.

    -----

    Programming in Java is rather being like the prisoners in Plato's cave: primitive values aside, we are always dealing with references to things and never with the things themselves. We behave, as Socrates suggested we might, by speaking of the shadowy references as if they were actual instances. It turns out that Plato was wrong - at least in this context - we don't need to be concerned with the instances themselves (what he called "forms") it is enough to know the behaviour of the references.
    Last edited by pbrockway2; 01-29-2012 at 05:57 AM.

  13. #13
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    I love You man.
    Last edited by diamonddragon; 01-29-2012 at 06:32 AM.

  14. #14
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    Just one more question.

    Is ok if say:
    "blah" is a String literal of the String reference type?
    "blah" is a constant expression which reference value is reference to String instance?
    Last edited by diamonddragon; 01-29-2012 at 07:04 AM.

  15. #15
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by pbrockway2 View Post
    Programming in Java is rather being like the prisoners in Plato's cave: primitive values aside, we are always dealing with references to things and never with the things themselves. We behave, as Socrates suggested we might, by speaking of the shadowy references as if they were actual instances. It turns out that Plato was wrong - at least in this context - we don't need to be concerned with the instances themselves (what he called "forms") it is enough to know the behaviour of the references.
    If instances are "forms", does it mean references are "essences"?

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    What is difference between reference type and reference value in "blah" case?
    The reference type of "blah" is String. (again that is documented at JLS 3.10.5 which I linked to before.)

    The value of the expression/literal "blah" is a pointer to an object which is an instance of String. This is quite general, not just for String 4.3.1 Objects: "An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object".

    In summary "blah" is of type String and has a value which is a pointer to an instance of String. We know *lots* about the reference value: we know what will happen if we send it to System.out.println(), we know what we will get back if we call length() with it, etc. But we know precious little about the instance itself, only what we can determine with the reference equality operators (eg the OP learnt that it is not the same as the instance pointed to by words).

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by diamonddragon View Post
    If instances are "forms", does it mean references are "essences"?
    No, references are the "appearance" of things in a Java program.

    It might have been a distraction to drag forms into it. For Plato there was a reality standing behind the appearances (and he thought it had to do with forms), just as you can think of instances as the real things to which references refer.

    (Essences are a whole other kettle of fish. Opinion was, and remains, divided as to whether the essence of a thing comes from its relationship to forms or whether it is a real, physical, aspect of the thing's substance. Nothing to do with Java. And, quite possibly, nothing to do with anything.)

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    Quote Originally Posted by diamonddragon View Post
    Just one more question.

    Is ok if say:
    "blah" is a String literal of the String reference type?
    "blah" is a constant expression which reference value is reference to String instance?
    This is the edited version of your last question... :)

    Yes. Perfectly OK. (... a constant expression whose reference value is a reference ...).

    Indeed, among friends - and we are all friends here - it's quite OK to say that "blah" is a String, or an instance of String, or a String object (as Fubarable did).

  19. #19
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: why is the most simple thing not working?

    So, if we have class instance, and no reference variable, we still have reference(pointer) to that object?

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: why is the most simple thing not working?

    Yes, you don't need a variable to have a pointer. "blah".length() is an example.

    Another would be "blah".split("a")[0]. Not only is "blah" acting as a reference (*), but so is "blah".split("a"). And not a variable in sight!

    (*) Of course I mean "is an expression whose value is a reference..."

    -----

    I'm not quite sure what you mean by a "class instance". But, yet another example would be System.out which is a reference (pointer) to an instance of PrintStream.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to do a simple class+object thing
    By ilop12 in forum New To Java
    Replies: 8
    Last Post: 06-03-2010, 05:08 PM
  2. please i need help with a simple thing
    By zohdi in forum New To Java
    Replies: 28
    Last Post: 05-30-2010, 02:41 AM
  3. [SOLVED] Simple Trig Thing
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 05-16-2009, 07:45 AM
  4. Replies: 5
    Last Post: 02-25-2009, 08:14 PM
  5. simple validations not working
    By rahaman.athiq in forum Web Frameworks
    Replies: 0
    Last Post: 11-06-2008, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •