| |

VerySource

 Forgot password?
 Register
Search
View: 700|Reply: 2

Ask why StringBuffer not Not Qualified

[Copy link]

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-20 11:40:01
| Show all posts |Read mode
class D
{

    public static void main (String [] args)
    {

String s1 = new String ("hello");

String s2 = new String ("hello");

if (s1.equals (s2))

System.out.println ("equal");

else

System.out.println ("not equal");

    }

}

class E
{

    public static void main (String [] args)
    {

StringBuffer sb1 = new StringBuffer ("hello");

StringBuffer sb2 = new StringBuffer ("hello");

if (sb1.equals (sb2))

System.out.println ("equal");

else

System.out.println ("not equal");

    }

}

Class D outputs equal, this is understandable
But why is Class E not equal? What is the mechanism of StringBuffer?
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-29 16:09:01
| Show all posts
StringBuffer calls the equals () method of Object, which compares whether it points to the same object, which is the same as ==.

for non-null reference values ​​x and y, this method returns true, if and only if x and y refer to the same object
Reply

Use magic Report

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-30 11:36:01
| Show all posts
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values ​​x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Returns true if and only if x and y refer to the same object, understand, thank you44964869.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list