java如果语句错误 [英] java if statement error

查看:134
本文介绍了java如果语句错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,这个如果语句不工作。

It seems to me that this if statement is not working.

我在Java的新的,但是我知道C#和C ++ pretty好,但我从来没有见过这样的事:

I'm new in java, but i know C# and C++ pretty well, but I've never seen such a thing before:

today=edit[0].substring(0,10);
if (today == edit[0].substring(0,10))
{
    pars_prog.addView(name_prog[i]);
}

和它不进入如果功能?

如果是在语句的Java(Android版)?

Are if statements different in Java (Android)?

推荐答案

当您使用 == 的对象引用(无论是字符串或任何其他非基本类型),它只是比较引用是否相等 - 即他们是否指的是完全相同的对象,或者他们是否俩都是空

When you use == for any object references (whether strings or any other non-primitive type) it simply compares whether the references are equal - i.e. whether they refer to the exact same object, or whether they're both null.

在这种情况下,你要确定字符串是否等于的 - 即他们是否重新present相同的字符序列。您应该使用等于方法是:

In this case, you want to determine whether the strings are equal - i.e. whether they represent the same sequence of characters. You should use the equals method for that:

if (today.equals(edit[0].substring(0,10)))

然而,一般来说这样做的时候,你就要小心了目标等于调用非空,否则你会得到一个 NullPointerException异常

However, in general when doing this you should be careful that the target of the equals call is non-null, or you'll get a NullPointerException.

注意,C#是的类似的 - 除了 == 操作符可以被重载,而的重载字符串。如果操作数的编译时类型不同时字符串,你仍然可以获得比较基准:

Note that C# is similar - except that the == operator can be overloaded, and is overloaded for string. If the compile-time types of the operands aren't both string, you'll still get reference comparison:

object text1 = new StringBuilder("hello").ToString();
object text2 = new StringBuilder("hello").ToString();
Console.WriteLine(text1 == text2); // False

这篇关于java如果语句错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆