简体   繁体   English

Java,创建新对象的意义是什么?

[英]Java, what is the point of creating new objects?

I am fairly new to Java and was wondering what the difference between the two is. 我对Java相当陌生,想知道两者之间的区别是什么。 For this example I used arrays: 对于此示例,我使用了数组:

class testpile {

public static void main(String[] args)
{
int[] a = {1,2,3,4,5,6}; //First array
int[] b = new int[5];  //Second Array
b[0] = 7;
b[1] = 8;
b[2] = 9;
b[3] = 10;
b[4] = 11;

print(a); 
print(b); 

} 

public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println(); 
} 


} 

I understand that using "new" creates a unique object but what are the advantages of using one over the other? 我知道使用“新”会创建一个唯一的对象,但是使用一个相对于另一个有什么好处?

In your example there's no real difference between the two. 在您的示例中,两者之间没有真正的区别。 The first is mostly just "syntactic sugar" for the latter. 第一个主要是后者的“语法糖”。 In both cases the array is allocated on the heap. 在这两种情况下,数组都是在堆上分配的。

Both of the code creating a int array of size 5/6 这两段代码都创建了一个大小为5/6的int数组

In the first case the array is initialized with vale at the time of creation 在第一种情况下,数组在创建时用vale初始化

In second case the value is assigned latter 在第二种情况下,将值分配给后者

that's the difference 那是不同的

In this example there is no difference b/w these two. 在这个例子中,这两个b / w没有区别。

In the first case the array is initialized at the time of creation
 In second case the value is assigned latter whenever you want...  

But in both cases it will get m/m into heap.. 但在两种情况下,它都会使m / m进入堆。

I understand that using "new" creates a unique object but what are the advantages of using one over the other? 我知道使用“新”会创建一个唯一的对象,但是使用一个相对于另一个有什么好处?

Both constructs do exactly the same thing (with different data, though): Creating an array and filling it with data. 两种构造都做完全相同的事情(但是使用不同的数据):创建一个数组并用数据填充它。 In particular, both these arrays are "unique objects". 特别地,这两个数组都是“唯一对象”。

You'd use the "less literal" one when you do not know the size and the initial values for the element at compile-time. 当您在编译时不知道元素的大小和初始值时,可以使用“较少文字”。

int[] a = {1,2,3,4,5,6}; //First array
int[] b = new int[5];  //Second Array

They are just two different ways of creating an array. 它们只是创建数组的两种不同方式。 There isn't really any OOP involved here. 这里实际上并没有涉及任何OOP。

The first one is better when you know the values before hand, otherwise the second is better. 当您事先知道值时,第一个更好,否则第二个更好。

The first statement is called array initialization where six int variables are created and each variable is assigned. 第一条语句称为数组initialization ,其中创建六个int变量并分配每个变量。 In second statement, the new keyword create 5 int variables whose initial value is zero. 在第二条语句中, new关键字创建5个int变量,其初始值为零。

Using new keyword, you may instantiate an array whenever you require. 使用new关键字,您可以在需要时实例化数组。

int []a=new int[5];

for(int i:a)
  System.out.println(i);

a=new int[]{11,22,33};

for(int i:a)
  System.out.println(i);

I think the result is same. 我认为结果是一样的。

But when you create a array with "new" clause, You should assign a specify length of the array. 但是,当您使用“ new”子句创建一个数组时,应该分配一个指定的数组长度。

e.g  int[] b = new int[**5**];

And in this sample, you can also assign the value for b[5]. 并且在此示例中,您还可以为b [5]分配值。 there shouldn't produce compilation error.But the error should occur in the runtime. 应该不会产生编译错误,但是该错误应该在运行时发生。

In regard to the another method, the length of the array don't need specify. 关于另一种方法,不需要指定数组的长度。 It depend on the element count of array. 它取决于数组的元素数。

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

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