Why can t two strings be compared?

Updated on technology 2024-02-08
16 answers
  1. Anonymous users2024-02-05

    string s1 = "a";

    string s2 = s1;

    In this case, s1 and s2 point to the same memory address, so s1==s2 returns true.

    And for: string s1 ="a";

    string s2 = "a";

    In this case, s1 and s2 point to different memory addresses, and although the value stored in both addresses is "a", s1==s2 returns false.

    If you want to compare whether the values in the addresses that S1 and S2 each point to are the same, the string class overrides the equals() method of the object class, which can make this comparison, for example:

    string s1 = "a";

    string s2 = "a";

    Returns true.

    However, if it is retrieved from the database, you should first determine whether it is null to avoid null pointer errors.

  2. Anonymous users2024-02-04

    The length of the string extracted from the library table is fixed, so there will be some spaces at the end, as long as the space at the end of the string extracted from the library is removed, I don't need to say the specifics!

  3. Anonymous users2024-02-03

    eques is used to compare the values of strings. , if it's a base type, and if it's an object type, it's a memory address.

  4. Anonymous users2024-02-02

    == is to see if the memory address of the two strings is the same, equals is to see if their characters are the same.

  5. Anonymous users2024-02-01

    1) equals is the method of object, all classes inherit object directly or indirectly, then they also inherit equals.

    object. In fact, there is no difference with ==.

    public boolean equals(object obj), but string rewrites equals

    public boolean equals(object anobject)

    if (anobject instanceof string)return true;}}

    return false;

    First, determine whether the address is different, then determine whether it is a string type, then convert it to a char array, and then compare.

    All classes have equals methods, no overrides, and the default is to compare addresses.

    It is recommended to study the source code and the principle more.

  6. Anonymous users2024-01-31

    When a class is compared with ==, it will only return true if the two classes are identical, including the same memory address, the same value, and so on.

    In your example, x with"abc"Although the values of the two strings are the same, they are two separate strings in memory, so if you use == will return false, and the value of the class should be compared with its equals

  7. Anonymous users2024-01-30

    Like ==, != These runners can be bai.

    du is used for basic types, and non-basic zhi dao types like string can't be directly compared with this comparison.

    stringtype: If you want to determine whether two strings are equal or unequal, use equals().

    if(!password is not equal to d

  8. Anonymous users2024-01-29

    It looks like this, string s2=new string("hello");In this way, the memory address of the new object will be different, and it will return false

  9. Anonymous users2024-01-28

    If the pointers of a and b only want to be the same, return true, otherwise false

  10. Anonymous users2024-01-27

    Direct comparison.

    For example, string a, b, c;

    c=a+b;Connect.

    if(a>b) cout<<"a>b";

  11. Anonymous users2024-01-26

    As soon as it is new, a new string object will be created.

    string s2="abc";

    string s3="abc";

    S2 and S3 refer to a string, because S3 will look in the string pool to see if there is "abc"."to quote it directly to s3.

    It's as simple as that.

  12. Anonymous users2024-01-25

    There are three "abcs" in the memory space, s1 points to one, s4 points to one, and s2 and s3 point to the same, that is to say, s2 and s3 refer to the same object, "is to determine whether they point to the same object, or to determine whether they refer to the same object.

  13. Anonymous users2024-01-24

    string s2 = "abc"Indicates that s2 corresponds to a "string constant", which is determined at compile time, so as long as it corresponds to the same string constant, it is equal to the variables, which is why your s2==s3 is true.

    string s1=new string("abc"The strings created with new string() here are not constants and cannot be determined at compile time, so the strings created by new string() are not put into the constant pool, they have their own address space.

    For a more detailed explanation, please refer to.

  14. Anonymous users2024-01-23

    * "abc"--- storage place is called a constant string pool, which is a shared string;

    new string("abc"Objects --- stored in the heap;

    s1, s2, s3, s4 --- referenced in stack memory;

    string class is created to check the string"abc"Whether it already exists in the pool, if it exists, it will be shared.

    string s1=new string("abc");The following actions were made:

    1 Define an S1 reference to the object New String("abc");

    2 and the object new string("abc"), and the"abc", which can also be said to point;

    string s2="abc";

    Define an S2 reference that points to a constant string pool"abc";[Share what exists].

    string s3="abc";

    Define an S3 reference that points to a constant string pool"abc";[Share what exists].

    string s4=new string("abc");

    1 Define an S4 reference that points to the object new string("abc");This opens another object in the heap memory, which is also in the aspiration constant pool"abc";However, each object is different, and their heap memory addresses can be different.

    To be clear, == compares memory addresses, and the content of the reference in the stack memory is the heap memory address of the object it is interested in, i.e. s1,2,3,4 [so you can understand it].

    In this way; s2==s3 needless to say; The memory addresses of s1 and s4 are also different;

    Look at it!

  15. Anonymous users2024-01-22

    C language does not have a string type, only a string, and you can use strcmp to compare string to a C++ STL class.

    If this is the case, it can be directly compared with < >==.

    The rules for both comparisons are dictionary order.

  16. Anonymous users2024-01-21

    For reference types, == determines whether the same instance is referenced.

    To determine whether the content of two instances is the same, use the custom equals method.

Related questions
6 answers2024-02-08

This little poem is concise and full of meaning, and the writing style is very distinctive. The poet skillfully handles the relationship between narrative and lyricism. The first three sentences of the narrative describe the environment, using layers of in-depth and repeated rendering techniques to create an atmosphere, pave the way for the fourth lyricism, highlight the status of the lyrical sentence, and make the lyrical sentence appear particularly vigilant and powerful. >>>More

8 answers2024-02-08

Zodiac signs are all deceptive, just half-believe.

9 answers2024-02-08

Actually, I think you should know if B is sincere, and the current boys have played with a lot. But when you meet a girl you really like, you will still treat her wholeheartedly. If B is sincere, I think you should be able to feel it. >>>More

4 answers2024-02-08

for(i=3;i<=ihuffsize[n-1];i++)uint_8s[0]=0;

uint_8s[1]=uint_8[p]; >>>More

5 answers2024-02-08

The following is **Self-love without limits.

Confucius traveled east and saw two children arguing. Ask why. >>>More