英文:
Code Example: String comparison with .equals(args[0])
问题
我正在借助一本书学习Java,但不理解以下示例/语句:
对于 args[0].equals("Schrödinger")
,您需要确保 args[0]
不为null,而 "Schrödinger" .equals(args[0])
不需要这种检查,因为 "Schrödinger"
不能为null。
代码:
if("Schrödinger".equals(args[0])) {
System.out.println("Hallo");
}
但是,如果 args
中没有参数,我会收到错误,这似乎也是合乎逻辑的,因为您需要在某个时候获取 args[0]
处的值进行比较,即使字符串 "Schrödinger"
不为null。我感到困惑,这本书中的示例是错误的,还是我没有理解其中的某些内容?
英文:
I'm learning Java with the help of a book and don't understand the following example/statement:
For args[0].equals("Schrödinger")
you need to make sure the args[0]
is not null, whereas "Schrödinger".equals(args[0])
doesn't need this kind of check because "Schrödinger"
can't be null.
Code:
if("Schrödinger".equals(args[0])) {
System.out.println("Hallo");
}
However I do get an error if there is no parameter in args
, which also seems logical as you need to grab the value at args[0]
at some point for the comparison, even if the string "Schrödinger"
isn't null. I'm confused, is the example from the book wrong or am I not understanding something?
答案1
得分: 1
提供的代码仅处理 args[0]
为 null
的情况,而不处理 args
的长度为 0
的情况。假设您的程序应该传递一些命令行参数,并且在执行时没有传递任何参数,那么以下数组 args
的长度将保持为 0
。
public static void main(String[] args)
因此,试图访问第一个位置的元素,该位置由索引 0
指定,将导致 ArrayIndexOutOfBoundsException
。由于数组的索引从 0
开始,最大索引可以达到数组大小减去 1
。
在访问最大索引内的元素之前,您可以通过首先检查数组的大小来避免面临的问题,例如:
if (args.length >= 1 && "Schrödinger".equals(args[0])) {
//...
}
类似地,如果要访问索引 1
处的元素,应检查大小是否为 2
或更多,例如:
if (args.length >= 2 && "Schrödinger".equals(args[1])) {
//...
}
顺便说一下,像 "Schrödinger".equals(args[0])
这样的条件被称为 尤达条件。
英文:
The code provided in the book only handles args[0]
being null
, not args
having a length of 0
. Assuming your program is supposed to pass some command-line arguments to it, and you do not pass any argument while executing it, the length of the following array, args
will remain 0
.
public static void main(String[] args)
As a result, an attempt to access an element from the first position, which is specified with the index, 0
will result in ArrayIndexOutOfBoundsException
. Since the index of an array starts with 0
, the maximum index can go up to the size of the array minus 1
.
You can avoid the problem you have faced by checking the size of the array first before accessing an element within the maximum index e.g.
if(args.length >= 1 && "Schrödinger".equals(args[0])) {
//...
}
Similarly, if you want to access an element up the index, 1
, you should check if the size is 2
or more e.g.
if(args.length >= 2 && "Schrödinger".equals(args[1])) {
//...
}
By the way, a condition like "Schrödinger".equals(args[0])
is known as Yoda conditions.
答案2
得分: 0
你的假设是正确的。对于主方法,如果未提供数组,则无法访问数组的第一个元素。这将引发ArrayIndexOutOfBoundsException
,因为没有元素。
英文:
Your assumption is correct. For the main method If the array is not provided then you can not access the first element of the array. It will throw an ArrayIndexOutOfBoundsException
since there are no elements.
答案3
得分: 0
>对于args[0].equals("Schrödinger")
,您需要确保args[0]
不为空。
正确,因为您在对象上调用了.equals("Schrödinger")
,这个对象预期在args
数组的索引0处。
>而"Schrödinger".equals(args[0])
不需要这种检查,因为"Schrödinger"
不能为null。
正确,因为"Schrödinger"
是一个字符串文字,根据定义是一个对象,所以您明确地拥有它,甚至没有假设它会为null的可能性。
>但是,如果args中没有参数,我会收到错误消息,这也似乎是合理的,因为您需要在某个时候获取args[0]
处的值进行比较,即使字符串"Schrödinger"不为空。
正确,但要准确的话,错误不会发生在比较阶段,因为您不能将某物与无物进行比较,而是您的错误发生在这里,因为您没有传递任何内容,因此args
数组为空,并且args[0]
会引发ArrayIndexOutOfBoundsException
异常。
我希望我已经为您阐述了每个步骤。
英文:
>For args[0].equals("Schrödinger")
you need to make sure the args[0]
is not null.
True, since you're calling .equals("Schrödinger")
on the object, which is expected to be at index 0 in the args
array.
>whereas "Schrödinger".equals(args[0])
doesn't need this kind of check because "Schrödinger"
can't be null.
True, as "Schrödinger"
is a String literal, which by definition is an Object, so you explicitly have it, and there's no even hypothetical chance it'll be null.
> However I do get an error if there is no parameter in args, which also seems logical as you need to grab the value at args[0]
at some point for the comparison, even if the string "Schrödinger" isn't null.
True, but to be precise here, error doesn't happen at the comparison stage, because of you can't compare something to nothing, rather, your error happens here, because you're not passing anything, hence the args
array is empty, and args[0]
will throw the ArrayIndexOutOfBoundsException
exception.
I hope I made each and every step clear for you.
答案4
得分: 0
运行任何针对空对象的方法都是不允许的,你会收到一个异常。在进行任何操作之前,你必须检查args[0]
是否为空(或长度为0)。
英文:
Running any method for a null object is not allowed, and you will get an exception. You have to check if args[0]
is null (or of length 0) before you do anything with it.
答案5
得分: 0
用于用户定义的参数数组
情况1: args[0].equals("Schrödinger")
当 args[0] 为 null(未传递任何值)时,您不能在 null 上调用 equals 方法,因此会抛出 NullPointerException
。
情况2: "Schrödinger".equals(args[0])
在这种情况下,尽管 args[0] 为 null,但在字符串文字上调用 equals,并且不会抛出异常,同时条件计算为 false。因此,这种方法是推荐的方法,或者使用 Optionals。
用于 public static void main 方法的参数
在 public static void main 方法的参数数组中,如果没有命令行参数,它将抛出 ArrayIndexOutOfBoundsException。如果 JVM 没有找到命令行参数,它将初始化 args 为 String[] args = new String[0],即长度为 0。因此,在访问时会引发异常。
英文:
For User defined args array
Case 1: args[0].equals("Schrödinger")
<br/>
When args[0] is null(no value is passed), you cannot call equals method on
null hence NullPointerException
is thrown
<br/>
Case 2: "Schrödinger".equals(args[0])
<br/>
In this case despite args[0] being null equals is called on string literal and
no exception is thrown + the condition evaluates to false. Hence this approach
is the recommended one or use Optionals.
For args of public static void main
In case of args array of public static void main it will throw ArrayIndexOutOfBoundException. If JVM finds no command line arguments its initializes the args as String[] args = new String[0] i.e of length 0. Hence while accessing it throws exception
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论