一行java代码,它的作用是什么?

发布于 2024-08-06 11:56:22 字数 758 浏览 3 评论 0原文

所以我购买了《Java for Dummies》第 4 版这本书,我必须说这可能是我花 30 美元买过的最划算的一本书。我对编码并不陌生,如果我自己这么说的话,我实际上相当擅长。

然而,我遇到了一行让我有点困惑的代码:

public void setName(String n)
{
     if(!n.equals(""))
     {
          name = n;
     }
}

我的问题出现在第三行, if(!n.equals("")) 部分......我知道 if 循环是如何工作的(即: if(this == that){do stuff}),但我之前没有见过 !n.equals("") 设置。谁能向我解释一下吗?

PS:只是猜测一下。是否相同:

public void setName(String n)
{
     if(n != "")
     {
          name = n
     }
}

我认为这只是一种确保如果用户不输入名称(即 myAccount.setName = ""; )的方法,它不会返回错误并像正常一样运行,但我不确定。

预先感谢您的帮助!

编辑:更改了我的“myAccount.name =”“;”到“myAccount.setName =”“;”,对于造成的混乱表示抱歉。

谢谢:去亚萨,感谢答复!和Lucas Aardvark一样,他也回答了,但是Asaph先在他自己的回答中回答了我的验证评论,谢谢大家!

So I have purchased the book "Java for Dummies" 4th Edition, and I must say it is probably the best 30 dollars I have ever spent on a book. I'm not new to coding, am actually fairly decent at at it if I say so myself.

However, I've come across a line of code that has me a touch confused:

public void setName(String n)
{
     if(!n.equals(""))
     {
          name = n;
     }
}

My question comes up on the third line, the if(!n.equals("")) part...I know how if loops work (ie: if(this == that){do stuff}), but I've not seen the !n.equals("") set up before. Can anyone please explain it to me?

PS: Just to throw in a guess. Is it the same as:

public void setName(String n)
{
     if(n != "")
     {
          name = n
     }
}

I think it's just a way to make sure that if the user doesn't type in a name (ie. myAccount.setName = ""; ) it doesn't kick back an error and runs like normal, but I wasn't sure.

Thanks in advance for the help!

EDIT: changed my "myAccount.name = "";" to "myAccount.setName = "";", sorry about the confusion.

THANK YOU: Goes to Asaph, appreciate the answer! Same to Lucas Aardvark, he answered as well, but Asaph answered my verification comment in his own answer first, thanks to everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神回复 2024-08-13 11:56:22

在java中,字符串是不可变的,但不是实习的,所以
if(""==n) 对于另一个字符串可能为真,也可能不为真
其中 "".equals(n) 为真。

只是为了让你更加困惑,这是糟糕的代码,它会得到一个 NullPointerException
如果以 null 作为参数调用。应该写成“”.equals(n)

In java, strings are immutable but not interned, so
if(""==n) might or might not be true for another string
for which "".equals(n) is true.

Just to confuse you more, this is bad code, it will get a NullPointerException
if called with null as the argument. It should be written as "".equals(n)

上课铃就是安魂曲 2024-08-13 11:56:22
if(!n.equals(""))
{
     name = n;
}

表示如果 n 不是空字符串,则将其值赋给 name。

在 Java 中,每个对象都有一个 equals(Object o) 方法来测试与另一个对象是否相等。 == 运算符通常用于比较基元。它还可以用于比较对象的“相同性”。 IE。这两个对象实际上是同一个实例。这对于不可变类型(例如字符串)和基本类型(例如整数和长整型)的所有对象包装器非常有用。

if(!n.equals(""))
{
     name = n;
}

means if n is not an empty String, assign its value to name.

In Java, every Object has an equals(Object o) method to test for equality with another Object. The == operator is typically used to compare primitives. It can also be used to compare Objects for "sameness". ie. the two Objects are in fact the same instance. This comes in handy for immutable types such as Strings and all the Object wrappers for the primitive types such as Integer and Long.

葬花如无物 2024-08-13 11:56:22

equals() 方法比较两个字符串的内容。 == 和 != 运算符告诉您两个 String 对象是否是同一个对象。具有相同内容的两个不同字符串,因此彼此 equals() ,仍然可能彼此 != 。尽管字符串不可变,但通过想象字符串是可变的来帮助理解差异,然后 equals() 意味着“当前相同”,== 意味着“将始终相同”。

安全规则是始终使用 equals() ,除非您确定两个字符串都是 实习。引用:

因此对于任意两个字符串 s
并且 t, s.intern() == t.intern() 是
true 当且仅当 s.equals(t) 为
正确。

所有文字字符串和字符串值
常量表达式被保留。

The equals() method compares contents of the two strings. The == and != operators tell you whether the two String objects are the same object or not. Two different strings with the same contents, and hence equals() to each other, may still be != to each other. Even though Strings aren't mutable, it helps to understand the difference by imagining that Strings are mutable, and then equals() means "are currently the same" and == means "will always be the same".

The safe rule is to always use equals() unless you're sure both strings are interned. Quoting:

It follows that for any two strings s
and t, s.intern() == t.intern() is
true if and only if s.equals(t) is
true.

All literal strings and string-valued
constant expressions are interned.

吲‖鸣 2024-08-13 11:56:22

考虑一下:

String a="";
String b="";

a 和 b 都是字符串对象,每个对象都有自己的内存分配,因此有唯一的地址。 a 和 b 位于不同的地址。当您编写布尔表达式时,

a == b

您正在比较对象的地址,而不是它们的内容。为了比较
内容必须使用 String 对象的 equals() 方法。

对象在内存中具有一个物理位置,该位置对于每个对象来说都是唯一的(两个不同的对象不能具有相同的内存地址)及其内容或值。 == 运算符比较对象的地址;当您编写 a==b 代码时,您是在询问 a 和 b 是否是同一对象的别名 - a 和 b 是否引用相同的物理位置。 a.equals(b) 询问两个对象(无论它们在哪里)是否具有相同的值。

由于编译器“驻留”,这有点复杂,编译器可能在编译时检测到两个常量具有相同的值并重用相同的对象,但对于运行时创建的值而言,情况并非如此。

Consider this:

String a="";
String b="";

Both a and b are String OBJECTS, each with its own memory allocation and thus a unique address. a and b are at different addresses. When you code the boolean expression

a == b

you are comparing the addresses of the objects, not their contents. To compare the
contents you must use the String object's equals() method.

An object has a physical location in memory, which is unique for each object -- no two distinct objects can have the same memory address -- and its contents or value. The == operator compares the addresses of the objects; when you code a==b you are asking if a and b are aliased names for the same object -- do a and b refer to the same physical location. a.equals(b) asks if the two objects, wherever they may be, have the same value.

This is complicated somewhat by compiler "interning", where the compiler may detect at compile time that two constants have the same value and reuse the same object, but this won't be true for values created at runtime.

请别遗忘我 2024-08-13 11:56:22

equals() 方法将返回一个 boolean 值,该值表明传入的对象“等于”进行调用的对象。可以在类中重写此“等于”方法以进行自己的测试。对于String,测试的是原始字符串的值是否与传入对象的字符串表示形式的值相同。

由于它返回一个布尔值,因此您可以对值带有 !,因此测试是“方法参数不是空字符串吗?”是的?然后将其分配给我们的 name 变量。

== 在比较引用时,将始终测试左侧的对象是否与右侧的对象相同。

the method equals() will return a boolean value which states that the Object being passed in is 'equal to' the Object making the call. This 'equals to' method can be overridden in classes to make their own test. In the case of String, the test is whether the value of the original String is the same as the value of the String representation of the Object being passed in.

As it returns a boolean, you can negate the value with a !, so the test is 'is the method argument not an empty String?' Yes? then assign it to our name variable.

== will always test whether the Object on the left is the same as the Object on the right, as it compares references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文