简体   繁体   English

用Java减去2个字符串等于null

[英]Subtract 2 strings in Java equals null

In C, if I subtract a string from the same string , it will result in NULL . 在C,如果我减去一个string从同一个string ,它会导致NULL So, if str1 = str2 , str1 = str2 - str1 = NULL . 因此,如果str1 = str2 ,则str1 = str2 - str1 = NULL How do I achieve the same null operation/result in Java? 如何在Java中实现相同的空操作/结果?

Thank you. 谢谢。

You can't subtract strings in C or Java. 您不能在C或Java中减去字符串。

In C, you can subtract two addresses of strings. 在C中,您可以减去两个字符串地址 If they're the same address, you'll get zero. 如果它们是相同的地址,您将得到零。 If they're not the same address, you'll get a meaningful result only if they're in the same block of allocated memory. 如果它们不是相同的地址,则只有当它们在分配的内存的同一块中时,您才会获得有意义的结果。

You can get a nonzero result from the subtraction of the addresses of two equal strings stored in different memory locations. 通过将存储在不同存储位置的两个相等字符串的地址相减可以得到非零结果。 You can test C string equality with strcmp or strncmp() . 您可以使用strcmpstrncmp()测试C字符串是否相等。 (The latter should be preferred.) (应优先选择后者。)

In Java, if you want to determine if two strings are the same object , you can use the equality operator. 在Java中,如果要确定两个字符串是否是同一对象 ,则可以使用相等运算符。 For example, str1 == str2 will return true if the two are the same object. 例如,如果两个str1 == str2是同一对象,则它们将返回true。

If you want to determine if the two strings are equal , you can use the equals() method. 如果要确定两个字符串是否相等 ,则可以使用equals()方法。 For example, str1.equals( str2 ) will return true if the two strings are equal. 例如,如果两个字符串相等,则str1.equals( str2 )将返回true。 If either might be null, you can use instead Objects.equals( str1, str2 ) . 如果任何一个都可能为null,则可以改用Objects.equals( str1, str2 )

There is not a way of doing that in java, because you cannot use "-" operator on objects. 在Java中,没有办法做到这一点,因为您不能在对象上使用“-”运算符。 Only if your objective is put null when both objects are equals, you can achieve the null result as follows: 只有当两个对象都相等时将目标设为null时,才能实现null结果,如下所示:

    String str1 = "hola";
    String str2 = "hola";
    String str3 = "";

    if(str1.equals(str2)){
        str3 = null;
    }

Actually, no. 其实没有 Subtracting same pointers should not necessarily result in NULL . 减去相同的指针不一定会导致NULL What it will surely be is zero, of type ptrdiff_t . 它肯定是零,类型为ptrdiff_t NULL being zero is an assumption not guaranteed to be valid. NULL为零是一个不能保证有效的假设。

But this doesn't work in Java at all since it doesn't have pointers (not as primitive types) and, subsequently, pointer arithmetic; 但是,这在Java中根本不起作用,因为它没有指针(不是原始类型),并且没有指针运算。 strings are known by their references. 字符串通过其引用已知。 You can't do too much with Java's references apart from dereferencing them (and a reference is guaranteed to succeed unless it's null ), assigning and comparing them for equality. 除了取消引用之外,您不能对Java的引用做太多的事情(并且引用必须保证成功,除非null ),然后为它们进行分配和比较是否相等。 That's all you need. 这就是您所需要的。

In C your string variables are pointers, which are numeric, and you can perform subtraction on them. 在C语言中,字符串变量是指针,该指针是数字的,您可以对其进行减法。

In Java your string variables are references to String objects and attempting subtraction is a type error. 在Java中,您的字符串变量是对String对象的引用,尝试减法是类型错误。 Specifically - is limited to use on specific primitive numeric types: byte , short , int , long , float , and double . 特别地-仅限于在特定的原始数字类型上使用: byteshortintlongfloatdouble

To determine if two String variables reference the same object in memory you would use str1 == str2 (double-equals) which determines reference equality. 要确定两个String变量是否引用内存中的同一对象,可以使用str1 == str2 (双等于)来确定引用相等性。 To determine if two String variables have the same value (but may be distinct references) you would use str1.equals(str2) assuming str1 != null . 要确定两个String变量是否具有相同的值(但可能是不同的引用),可以使用str1.equals(str2)并假设str1!= null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM