Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. A stupid question?

A stupid question?

Scheduled Pinned Locked Moved Java
questionjavadatabaseannouncement
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkbc
    wrote on last edited by
    #1

    Hi all, Near one or two years (version Java at that time is 1.5x), I have a short code to compare 2 Strings in java like bellow: String a = "abc"; String b ="abc";//this is load from the database. CODE 1

    if(a==b){
    System.out.println("equal");
    }else {
    System.out.println("dif");
    }

    and I got the result is dif I remember because after I change the code to String a = "abc"; String b ="abc";//this is load from the database. CODE 2

    if(a.equals(b)){
    System.out.println("equal");
    }else {
    System.out.println("dif");
    }

    It give me the result is equal Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal

    B T J N 4 Replies Last reply
    0
    • S sharkbc

      Hi all, Near one or two years (version Java at that time is 1.5x), I have a short code to compare 2 Strings in java like bellow: String a = "abc"; String b ="abc";//this is load from the database. CODE 1

      if(a==b){
      System.out.println("equal");
      }else {
      System.out.println("dif");
      }

      and I got the result is dif I remember because after I change the code to String a = "abc"; String b ="abc";//this is load from the database. CODE 2

      if(a.equals(b)){
      System.out.println("equal");
      }else {
      System.out.println("dif");
      }

      It give me the result is equal Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      This is as expected, in Java there is no operator overloading and == always does a reference equality check. To do a value equality check, you need to use .equals (after doing a null check).

      1 Reply Last reply
      0
      • S sharkbc

        Hi all, Near one or two years (version Java at that time is 1.5x), I have a short code to compare 2 Strings in java like bellow: String a = "abc"; String b ="abc";//this is load from the database. CODE 1

        if(a==b){
        System.out.println("equal");
        }else {
        System.out.println("dif");
        }

        and I got the result is dif I remember because after I change the code to String a = "abc"; String b ="abc";//this is load from the database. CODE 2

        if(a.equals(b)){
        System.out.println("equal");
        }else {
        System.out.println("dif");
        }

        It give me the result is equal Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal

        T Offline
        T Offline
        TorstenH
        wrote on last edited by
        #3

        Code1 compares the objects and if they are equal, also both Objects have the same value they are still different objects. Code2 checks character wise for the value. It's a common failure.[^].

        regards Torsten When I'm not working

        1 Reply Last reply
        0
        • S sharkbc

          Hi all, Near one or two years (version Java at that time is 1.5x), I have a short code to compare 2 Strings in java like bellow: String a = "abc"; String b ="abc";//this is load from the database. CODE 1

          if(a==b){
          System.out.println("equal");
          }else {
          System.out.println("dif");
          }

          and I got the result is dif I remember because after I change the code to String a = "abc"; String b ="abc";//this is load from the database. CODE 2

          if(a.equals(b)){
          System.out.println("equal");
          }else {
          System.out.println("dif");
          }

          It give me the result is equal Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          sharkbc wrote:

          Now the CODE 1 always gives the result is equal
           
          Is it true or my eyes is wrong?

          Java, regardless of version, has always operated in that way (equals is required.) So something is wrong in how you are validating this. Perhaps because despite what your comment says you are not in fact loading it from the database. (An very odd corner case would involve how you are 'loading' it from the database and would indicate an error in the load process.)

          S 1 Reply Last reply
          0
          • J jschell

            sharkbc wrote:

            Now the CODE 1 always gives the result is equal
             
            Is it true or my eyes is wrong?

            Java, regardless of version, has always operated in that way (equals is required.) So something is wrong in how you are validating this. Perhaps because despite what your comment says you are not in fact loading it from the database. (An very odd corner case would involve how you are 'loading' it from the database and would indicate an error in the load process.)

            S Offline
            S Offline
            sharkbc
            wrote on last edited by
            #5

            hi Jschell, I've just modify my question, it must be ... Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal Im sure at that time, the database is not change and i do not change any other code in the programe, because after we got the mistake, we debug the code and get it. We just only change

            if(a==b)

            to

            if(a.equals(b))

            T J 2 Replies Last reply
            0
            • S sharkbc

              hi Jschell, I've just modify my question, it must be ... Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal Im sure at that time, the database is not change and i do not change any other code in the programe, because after we got the mistake, we debug the code and get it. We just only change

              if(a==b)

              to

              if(a.equals(b))

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              what JRE are you using now? is it the new Java 7? There might be an improvment as Strings are now also allowed in switch-conditions: http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html[^] But in general I would say that there must be something else wrong.

              regards Torsten When I'm not working

              1 Reply Last reply
              0
              • S sharkbc

                hi Jschell, I've just modify my question, it must be ... Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal Im sure at that time, the database is not change and i do not change any other code in the programe, because after we got the mistake, we debug the code and get it. We just only change

                if(a==b)

                to

                if(a.equals(b))

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                The real problem is that we do not know what you are actually doing. Again the following has always been true regardless of java version for the major java vendors... Equality compares references The method equals() compares value (content of string.) As I said there are ways which MIGHT present seemingly different results dependent on the specifics of what is going on. But you haven't presented the specifics. And based ONLY on the code you posted you are comparing references for what you refer to "CODE 1". And reference equality will be equal. So what are some possible causes for some other difference for some OTHER code? 1. You were not using a real version of java, it was some odd variant. 2. There was something odd about the way in which your database code worked. 3. There was something odd about your data 4. You misinterpreted what was happening before. 5. The real code would make it explicitly obvious why a difference was occurring. 6. The method intern() was being called somewhere (incorrectly called.)

                1 Reply Last reply
                0
                • S sharkbc

                  Hi all, Near one or two years (version Java at that time is 1.5x), I have a short code to compare 2 Strings in java like bellow: String a = "abc"; String b ="abc";//this is load from the database. CODE 1

                  if(a==b){
                  System.out.println("equal");
                  }else {
                  System.out.println("dif");
                  }

                  and I got the result is dif I remember because after I change the code to String a = "abc"; String b ="abc";//this is load from the database. CODE 2

                  if(a.equals(b)){
                  System.out.println("equal");
                  }else {
                  System.out.println("dif");
                  }

                  It give me the result is equal Is it true or my eyes is wrong? Now the CODE 1 always gives the result is equal

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #8

                  Where is a initialised from? It is possible that the two objects are the same and then == will return true. For future reference, if you want to compare two objects always use the equals() method.


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups