Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Java - Comparing Strings

  • 01-04-2006 02:23PM
    #1
    Registered Users, Registered Users 2 Posts: 3,265 ✭✭✭


    /*
    Below is what im stuck on.
    ******
    String semiColon = ";";

    if (tempString2 == semiColon )
    {
    System.out.print("test");
    tempString2 = " ";
    }
    ******

    In my mind it should compare the tempString2 with a ;. If it finds
    that it is the same as a ; then it should set tempString to a blank
    space. I want it to do this so that it knows if its a double figure
    or if its a single figure.

    EDITED: Solved.


Comments

  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma




  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    EDIT : What he said


  • Registered Users, Registered Users 2 Posts: 45,160 ✭✭✭✭Basq


    I always use String.equalsIgnoreCase myself!


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Have you tried the following?
    if (tempString2.equals(semiColon) )
    

    Edit: or any of the other above suggestions...


  • Registered Users, Registered Users 2 Posts: 3,265 ✭✭✭BlackWizard


    Oh bugger your right.

    I just copied that compare statement from sudo code we made up a few
    days ago and totally messed myself up.

    heh! Thanks guys! I hadnt got much time to look into it either thats why I
    posted it here. :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    basquille wrote:
    I always use String.equalsIgnoreCase myself!

    WASTEFUL, in this case. Tsk. :)


  • Registered Users, Registered Users 2 Posts: 3,225 ✭✭✭JackKelly


    Why is it that some types can only use "==" and others .equals(...)?


  • Registered Users, Registered Users 2 Posts: 1,999 ✭✭✭The_Bullman


    TimAy wrote:
    Why is it that some types can only use "==" and others .equals(...)?

    I think you can only use the "==" for comparing primitave types. For other classes the equals method should be used

    I think..


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I think you can only use the "==" for comparing primitave types. For other classes the equals method should be used

    I think..

    Correct. A string is an object not a primative.


  • Closed Accounts Posts: 51 ✭✭garrethg


    TimAy wrote:
    Why is it that some types can only use "==" and others .equals(...)?
    The comparison operator is used to test for equality between both primatives and references to objects.

    e.g.

    String x= new String("some text");
    String y= new String("some text");
    String z= x;

    /* a is false, it's a comparison between an object and a primative. */
    boolean a= x == "some text";
    /* b is true, it's a comparison between the primative value of an object field, returned by the method "equals", and a primative. */
    boolean b= x.equals("some text");
    /* c is false, it's a comparison between references to different objects.*/
    boolean c= x == y;
    /* d is true, it's a comparison between references to the same object. */
    boolean d= x == z;


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    garrethg wrote:
    /* b is true, it's a comparison between the primative value of an object field, returned by the method "equals", and a primative. */
    boolean b= x.equals("some text");

    Eh? Equals returns a boolean, obtained by comparing the contents of x to the string literal "some text", through a mechanism which, I believe, is implementation-defined. (But in practice almost certainly simply involves comparing two arrays of characters).


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    garrethg wrote:
    /* c is false, it's a comparison between references to different objects.*/
    boolean c= x == y;

    Thats guaranteed in this case because you have created two new objects but if you did something like

    String x= "some text";
    String y= "some text";

    then x == y could return true, because of how Java handles strings it sees both strings as one object.

    [edit]
    Just to add. You shouldn't be creating your strings like...

    String x = new String("some text");

    As this is wasteful because you are creating two objects.


  • Closed Accounts Posts: 51 ✭✭garrethg


    rsynnott wrote:
    Eh? Equals returns a boolean, obtained by comparing the contents of x to the string literal "some text", through a mechanism which, I believe, is implementation-defined. (But in practice almost certainly simply involves comparing two arrays of characters).
    Thanks, I don't know what I (wasn't) thinking at the time :o


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    Hobbes wrote:
    Just to add. You shouldn't be creating your strings like...

    String x = new String("some text");

    As this is wasteful because you are creating two objects.

    AFAIR, modern Javas optimise this, treating String x as a reference.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    rsynnott wrote:
    AFAIR, modern Javas optimise this, treating String x as a reference.

    well it would be a reference but it would also create a reference for "some text" within the method.

    As you said they may do away with this, but its still messy imho.


Advertisement