int x int y 和 int x,y 之间有什么区别?

huangapple go评论80阅读模式
英文:

What's the difference between int x int y and int x,y

问题

在:

int x;
int y;

和:

int x, y;

例如,在下面的代码中:

public static boolean sda4(int[] arr) {
    for(int i=0, j=arr.length/2; i<arr.length/2 && j<arr.length; i++, j++)
        if(arr[i]!=arr[j]) return false;
    return true;
}

如果我在for循环内部输入(int i=0, int j=arr.length/2),它会显示“语法错误”。

英文:

Between:

int x;
int y;

and:

int x, y;

For example, in the next code:

public static boolean sda4(int[] arr) {
	for(int i=0, j=arr.length/2; i&lt;arr.length/2 &amp;&amp; j&lt;arr.length; i++, j++)
		if(arr[i]!=arr[j]) return false;
	return true;
}

If I type inside the for loop (int i=0, int j=arr.length/2) it says "syntax error".

答案1

得分: 2

没有字节码方面的差异。

  • 使用多变量声明可以节省一些行数,并且更容易阅读,因为它们都属于相同的数据类型。在这两种情况下,保留的内存空间是相同的。

  • 使用变量声明和多重初始化虽然可以节省行数并使代码更紧凑,但阅读起来可能更复杂或更烦人。

因此,有时候代码更紧凑并不意味着它的质量更高。

祝好!

英文:

There are no differences in bytecode terms.

  • With the multiple variable declaration you save some lines and it is easier to read since they all belong to the same data type. The reserved memory space is the same in both cases.

  • With the declaration and multiple initialization of variables, although you save lines and the code is more compact, it could be more complex or annoying to read.

Therefore, sometimes the code is more compact does not imply that it is of higher quality.

GL


答案2

得分: 1

让我们进行一些实验,好吗?我们仔细看看三个不同的程序。

Test1.java

public class Test1 {
    public static void main(String[] args) {
        int x;
        x = 1;
        int y;
        y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

Test2.java

public class Test2 {
    public static void main(String[] args) {
        int x = 1, y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

Test3.java

public class Test3 {
    public static void main(String[] args) {
        int x;
        int y;
        x = 1;
        y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

当我们现在编译这三个程序(javac Test1.java Test2.java Test3.java),并查看生成的字节码(javap -C Test[123]),我们看到以下内容:

javap -c Test1

Compiled from "Test1.java"
public class Test1 {
  public Test1();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

javap -c Test2

Compiled from "Test2.java"
public class Test2 {
  public Test2();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

javap -c Test3

Compiled from "Test3.java"
public class Test3 {
  public Test3();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

由于这三个类的字节码是相同的,问题归结为个人喜好。在Java中,通常每个变量都在自己的一行上声明。Braian Coronel在他的答案中讨论了关于代码可读性的一些优缺点

英文:

Let us do a little bit of experimentation, shall we? We take a closer look at three different programs.

Test1.java:

public class Test1 {
    public static void main(String[] args) {
        int x;
        x = 1;
        int y;
        y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

Test2.java:

public class Test2 {
    public static void main(String[] args) {
        int x = 1, y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

Test3.java:

public class Test3 {
    public static void main(String[] args) {
        int x;
        int y;
        x = 1;
        y = 2;
        System.out.println(x);
        System.out.println(y);
    }
}

When we now compile those three programms (javac Test1.java Test2.java Test3.java) and take a look at the produced bytecode (javap -C Test[123]), we see the following:

javap -c Test1:

Compiled from &quot;Test1.java&quot;
public class Test1 {
  public Test1();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

javap -c Test2:

Compiled from &quot;Test2.java&quot;
public class Test2 {
  public Test2();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

javap -c Test3:

Compiled from &quot;Test3.java&quot;
public class Test3 {
  public Test3();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      11: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_2
      15: invokevirtual #13                 // Method java/io/PrintStream.println:(I)V
      18: return
}

Since the bytecode for all three classes is identical, the question is one of personal preference. In Java, normally each variable is delcared on its own line. Braian Coronel discussed some pros and cons wrt. code readability in his answer

答案3

得分: 0

> 修复你的代码如下
> 移除 'int' 在 j=arr.length/2 前面
因为你不能在for循环条件中声明多个变量类型因为 int x,y 是唯一可以在循环条件内部创建变量的方式

    public static boolean sda4(int[] arr) {
            for(int i=0, j=arr.length/2; i<arr.length/2 && j<arr.length; i++, j++)
                if(arr[i]!=arr[j]) return false;
            return true;
        }
英文:

> Fix your code like this,
> Remove 'int' before j=arr.length/2
Because you can't declare more than one variable type inside for loop condition . Because int x,y is the only one thing you can create variables inside loop condition

public static boolean sda4(int[] arr) {
	for(int i=0, j=arr.length/2; i&lt;arr.length/2 &amp;&amp; j&lt;arr.length; i++, j++)
		if(arr[i]!=arr[j]) return false;
	return true;
}

huangapple
  • 本文由 发表于 2020年8月23日 03:10:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63540137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定