How does .equals Java String Class method work?










1















In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?



String word = "hello"
word.equals("hello");


output is true










share|improve this question

















  • 3





    What do you mean by "a random variable of type string"?

    – Henry
    Nov 12 '18 at 13:23











  • In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

    – davidxxx
    Nov 12 '18 at 13:27















1















In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?



String word = "hello"
word.equals("hello");


output is true










share|improve this question

















  • 3





    What do you mean by "a random variable of type string"?

    – Henry
    Nov 12 '18 at 13:23











  • In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

    – davidxxx
    Nov 12 '18 at 13:27













1












1








1








In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?



String word = "hello"
word.equals("hello");


output is true










share|improve this question














In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?



String word = "hello"
word.equals("hello");


output is true







java class methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 13:19









The Great VisionaryThe Great Visionary

15117




15117







  • 3





    What do you mean by "a random variable of type string"?

    – Henry
    Nov 12 '18 at 13:23











  • In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

    – davidxxx
    Nov 12 '18 at 13:27












  • 3





    What do you mean by "a random variable of type string"?

    – Henry
    Nov 12 '18 at 13:23











  • In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

    – davidxxx
    Nov 12 '18 at 13:27







3




3





What do you mean by "a random variable of type string"?

– Henry
Nov 12 '18 at 13:23





What do you mean by "a random variable of type string"?

– Henry
Nov 12 '18 at 13:23













In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

– davidxxx
Nov 12 '18 at 13:27





In Java you can instantiate objects without assigning it to a variable. Here this is the case for "hello" : word.equals("hello");

– davidxxx
Nov 12 '18 at 13:27












4 Answers
4






active

oldest

votes


















5














The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().



Take a look:



public boolean equals(Object anObject) 
if (this == anObject)
return true;


if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);



return false;



As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared



I have taken it from here. Better if you have an idea about String pool also. So please refer the link






share|improve this answer

























  • @Pshemo True. Edited it. Thanks

    – Sand
    Nov 12 '18 at 13:47


















2














String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.



We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here



Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java






share|improve this answer
































    1














    If you are comparing String with primitives like int,long.
    The answer is
    String is not a primitive. It is a class. There are several ways to create an object from this class,
    like



    String word=new String("hello");
    String word="hello"; //special case for String class


    now word becomes an object and you can call the method equals(because every object you create is inherited from java.lang.Object) class.



    If you look at primitives like int.
    you cannot do something like below, beacause number is not an object.



    int number=12; 
    number.equals(12)





    share|improve this answer






























      1














      equals method compares the string on which the method is called with the string which is passed as a parameter.



      Take a look at these links:



      String Comparison



      Equals on String



      Overriding equals in String class






      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263058%2fhow-does-equals-java-string-class-method-work%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().



        Take a look:



        public boolean equals(Object anObject) 
        if (this == anObject)
        return true;


        if (anObject instanceof String)
        String aString = (String)anObject;
        if (coder() == aString.coder())
        return isLatin1() ? StringLatin1.equals(value, aString.value)
        : StringUTF16.equals(value, aString.value);



        return false;



        As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared



        I have taken it from here. Better if you have an idea about String pool also. So please refer the link






        share|improve this answer

























        • @Pshemo True. Edited it. Thanks

          – Sand
          Nov 12 '18 at 13:47















        5














        The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().



        Take a look:



        public boolean equals(Object anObject) 
        if (this == anObject)
        return true;


        if (anObject instanceof String)
        String aString = (String)anObject;
        if (coder() == aString.coder())
        return isLatin1() ? StringLatin1.equals(value, aString.value)
        : StringUTF16.equals(value, aString.value);



        return false;



        As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared



        I have taken it from here. Better if you have an idea about String pool also. So please refer the link






        share|improve this answer

























        • @Pshemo True. Edited it. Thanks

          – Sand
          Nov 12 '18 at 13:47













        5












        5








        5







        The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().



        Take a look:



        public boolean equals(Object anObject) 
        if (this == anObject)
        return true;


        if (anObject instanceof String)
        String aString = (String)anObject;
        if (coder() == aString.coder())
        return isLatin1() ? StringLatin1.equals(value, aString.value)
        : StringUTF16.equals(value, aString.value);



        return false;



        As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared



        I have taken it from here. Better if you have an idea about String pool also. So please refer the link






        share|improve this answer















        The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().



        Take a look:



        public boolean equals(Object anObject) 
        if (this == anObject)
        return true;


        if (anObject instanceof String)
        String aString = (String)anObject;
        if (coder() == aString.coder())
        return isLatin1() ? StringLatin1.equals(value, aString.value)
        : StringUTF16.equals(value, aString.value);



        return false;



        As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared



        I have taken it from here. Better if you have an idea about String pool also. So please refer the link







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 14:11









        suvojit_007

        1,3251517




        1,3251517










        answered Nov 12 '18 at 13:25









        SandSand

        1,417319




        1,417319












        • @Pshemo True. Edited it. Thanks

          – Sand
          Nov 12 '18 at 13:47

















        • @Pshemo True. Edited it. Thanks

          – Sand
          Nov 12 '18 at 13:47
















        @Pshemo True. Edited it. Thanks

        – Sand
        Nov 12 '18 at 13:47





        @Pshemo True. Edited it. Thanks

        – Sand
        Nov 12 '18 at 13:47













        2














        String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.



        We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here



        Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java






        share|improve this answer





























          2














          String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.



          We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here



          Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java






          share|improve this answer



























            2












            2








            2







            String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.



            We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here



            Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java






            share|improve this answer















            String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.



            We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here



            Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 '18 at 14:07

























            answered Nov 12 '18 at 14:01









            user2851729user2851729

            336




            336





















                1














                If you are comparing String with primitives like int,long.
                The answer is
                String is not a primitive. It is a class. There are several ways to create an object from this class,
                like



                String word=new String("hello");
                String word="hello"; //special case for String class


                now word becomes an object and you can call the method equals(because every object you create is inherited from java.lang.Object) class.



                If you look at primitives like int.
                you cannot do something like below, beacause number is not an object.



                int number=12; 
                number.equals(12)





                share|improve this answer



























                  1














                  If you are comparing String with primitives like int,long.
                  The answer is
                  String is not a primitive. It is a class. There are several ways to create an object from this class,
                  like



                  String word=new String("hello");
                  String word="hello"; //special case for String class


                  now word becomes an object and you can call the method equals(because every object you create is inherited from java.lang.Object) class.



                  If you look at primitives like int.
                  you cannot do something like below, beacause number is not an object.



                  int number=12; 
                  number.equals(12)





                  share|improve this answer

























                    1












                    1








                    1







                    If you are comparing String with primitives like int,long.
                    The answer is
                    String is not a primitive. It is a class. There are several ways to create an object from this class,
                    like



                    String word=new String("hello");
                    String word="hello"; //special case for String class


                    now word becomes an object and you can call the method equals(because every object you create is inherited from java.lang.Object) class.



                    If you look at primitives like int.
                    you cannot do something like below, beacause number is not an object.



                    int number=12; 
                    number.equals(12)





                    share|improve this answer













                    If you are comparing String with primitives like int,long.
                    The answer is
                    String is not a primitive. It is a class. There are several ways to create an object from this class,
                    like



                    String word=new String("hello");
                    String word="hello"; //special case for String class


                    now word becomes an object and you can call the method equals(because every object you create is inherited from java.lang.Object) class.



                    If you look at primitives like int.
                    you cannot do something like below, beacause number is not an object.



                    int number=12; 
                    number.equals(12)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 13:53









                    DushDush

                    713312




                    713312





















                        1














                        equals method compares the string on which the method is called with the string which is passed as a parameter.



                        Take a look at these links:



                        String Comparison



                        Equals on String



                        Overriding equals in String class






                        share|improve this answer





























                          1














                          equals method compares the string on which the method is called with the string which is passed as a parameter.



                          Take a look at these links:



                          String Comparison



                          Equals on String



                          Overriding equals in String class






                          share|improve this answer



























                            1












                            1








                            1







                            equals method compares the string on which the method is called with the string which is passed as a parameter.



                            Take a look at these links:



                            String Comparison



                            Equals on String



                            Overriding equals in String class






                            share|improve this answer















                            equals method compares the string on which the method is called with the string which is passed as a parameter.



                            Take a look at these links:



                            String Comparison



                            Equals on String



                            Overriding equals in String class







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 2 days ago

























                            answered Nov 12 '18 at 13:33









                            ArmineArmine

                            6622826




                            6622826



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263058%2fhow-does-equals-java-string-class-method-work%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Use pre created SQLite database for Android project in kotlin

                                Darth Vader #20

                                Ondo