+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
Coding Thread, How to compare strings in Java in Coding and Web Development; JFI if (string1 == string2) { ... } doesn't work in java You have to use other techniques such as ...
  1. #1

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    5,829
    Thank Post
    1,476
    Thanked 595 Times in 446 Posts
    Rep Power
    170

    How to compare strings in Java

    JFI

    if (string1 == string2) {
    ...
    }

    doesn't work in java

    You have to use other techniques such as

    if (string1.equals(string2))
    {
    ...
    }

    taken me days to work why it wasn't working and eventually googledfro java string comparision to find out that it doesn't work like PHP or C or C++

    you live and learn

    regards

    Simon

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,983
    Thank Post
    3,531
    Thanked 1,107 Times in 1,014 Posts
    Rep Power
    374
    Java is a bit backwards ( or at least I thought so ) I only did a little at uni in comparison to C++ or the likes, same goes for data casting, done differently between c++ and java from what I remember.

    maybe that's just me ??

  3. #3

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    It's because a string in Java is essentially an object. The comparison operator would compare the unique objects and just wouldn't be the desired output. That's why they have the equals() method which will actually compare the value of the string to something else and return the proper result

  4. Thanks to webman from:

    mac_shinobi (17th June 2010)

  5. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by webman View Post
    It's because a string in Java is essentially an object.
    So it is in other sane languages, but that's what we have operator overloads for. Java apparently doesn't.

  6. #5

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    it doesn't work like PHP or C or C++
    Interesting but if it's strict ANSI C you use strcmp() and so on, but "or JavaScript/JScript" works.

  7. #6

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    Yeah. I'm not particularly fond of Java, but that's just the way it works. *shrugs*

  8. #7

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Of course then you have fun like the Bourne shell families, where we have test operators like "-eq", "-ne", "-lt", "-le", "-z", "-n" and so on, since it is not object-based nor has overloads...

  9. #8


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,489
    Thank Post
    868
    Thanked 855 Times in 675 Posts
    Rep Power
    197
    Java feels backwards becuase you're using a method from one object, but you're comparing two.

    Eg.

    Perl
    if ($foo eq $bar)

    C
    if (strcmp(foo,bar) == 0)

    Java
    if (foo.equals(bar))

    So this one's the only one which "looks" uneven - counterintuitive, even.
    Not done java for ages, but it might be possible to do something like:

    if (string::equals(foo,bar))

  10. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,983
    Thank Post
    3,531
    Thanked 1,107 Times in 1,014 Posts
    Rep Power
    374
    Quote Originally Posted by webman View Post
    It's because a string in Java is essentially an object. The comparison operator would compare the unique objects and just wouldn't be the desired output. That's why they have the equals() method which will actually compare the value of the string to something else and return the proper result
    I need to save up about £1000 and get a bunch of books / cbt nuggets etc - tons and tons to learn !!

    Trying to learn from tutorials is tough as I dont know what code snippets are correct or not etc and what do you mean by object exactly when referring to programming ?

    In java then how come they dont do

    if (foo.equals(bar.value()))

    seeing as they are using the .equals
    Last edited by mac_shinobi; 17th June 2010 at 11:44 PM.

  11. #10

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by mac_shinobi View Post
    what do you mean by object exactly when referring to programming ?
    Object-oriented programming - Wikipedia, the free encyclopedia

  12. Thanks to powdarrmonkey from:

    mac_shinobi (17th June 2010)

  13. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,983
    Thank Post
    3,531
    Thanked 1,107 Times in 1,014 Posts
    Rep Power
    374
    so quoting from wikipedia

    An object is a discrete bundle of functions and procedures, often relating to a particular real-world concept such as a bank account holder or hockey player. Other pieces of software can access the object only by calling its functions and procedures that have been allowed to be called by outsiders.
    Would that mean if I had a script ( i know you might use byref or byval in a function, I think in a fully blown language such as c++, java or w/e )

    Just as a simple example without over complicating it

    Code:
    Sub add(x,y)
    add = x + y
    End Sub
    Then the object would be add sub??

  14. #12

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    Not exactly

    Take a bank account as an example. An example of a class would be a customer. The properties of this class would be their account number, name, address, and balance. Methods would be withdraw() and deposit(). An object is just an instantiation of a class. Pseudocode example:

    Code:
    Customer webman = new Customer(12345678, "Craig", "101 Front Street", 4000);
    webman.withdraw(200);
    Then you get on to inheritance - the generalisation-specialisation part of object-oriented programming. A regular customer contains the stuff as mentioned above; and then you could have a specialised "Gold" customer who may have an overdraft attribute added on - but it also inherits all of the customer stuff too.

    The concept is a bit different from procedural programming, but once you get your head around it it becomes a lot easier.

  15. #13

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,983
    Thank Post
    3,531
    Thanked 1,107 Times in 1,014 Posts
    Rep Power
    374
    Quote Originally Posted by webman View Post
    An object is just an instantiation of a class. Pseudocode example:
    That's where I get lost - english please

    so the class contains all the properties that define the class but its not an object until its created
    Last edited by mac_shinobi; 18th June 2010 at 12:02 AM.

  16. #14

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Well not really, since subs can't return values... but I see what you mean.

    No. In a full OO language, add() would be a function of some object (like object.Add()) and it would take only one parameter, and add it to the value already possessed by the object:

    Code:
    Class myobject
    Dim mynumber As Integer
    
    Sub Add(othernum As Integer)
        mynumber = mynumber + othernum
    End Sub
    
    Function GetNumber() As Integer
        GetNumber = mynumber
    End Function
    
    End Class
    (my VB is rusty, but this is reasonable pseudo-code if nothing else...)

    This is the difference between procedural code, like your sub, and fully object-orientated methods like above.

  17. #15

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    A class is like a blueprint, and creating an instance of an object is bringing it to life so you can interact with it. I have some information that might be useful to you, will PM tomorrow



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 19th January 2010, 02:25 PM
  2. Compare Smoothwall with RM smart cache
    By nicholab in forum Internet Related/Filtering/Firewall
    Replies: 1
    Last Post: 13th January 2010, 06:20 PM
  3. [Video] Compare The Meerkat Babies
    By russdev in forum Jokes/Interweb Things
    Replies: 0
    Last Post: 20th August 2009, 12:27 PM
  4. [Website] You CAN compare the meerkat... dot com!
    By WithoutMotive in forum Jokes/Interweb Things
    Replies: 11
    Last Post: 21st January 2009, 10:19 PM
  5. Compare Values Above, Equal and Below
    By TwyloII in forum How do you do....it?
    Replies: 7
    Last Post: 16th January 2009, 01:20 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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