Difference between these two array statements in java [duplicate] - java

This question already has answers here:
How to initialize an array in Java?
(11 answers)
Closed 2 years ago.
what is the difference between these 2 array staments in java. In Statement 1 we dont use new keyword so my question is there object is created or not.
Statement 1: int[] arr = {10,20,30};
Statement 2: int[] arr = new int[]{10,20,30};

Both are the same, just a different way of writing:
In the first statement, the type is derived by the compiler in the array creation from the types / the declaration of arr.
In the second statement you have an explicit declaration of the instance. It contains more overhead.

Related

"double[] array = {1,2,3,4};" vs "double[] array = new double[]{1,2,3,4,};" [duplicate]

This question already has answers here:
Declaring arrays without using the 'new' keyword in Java
(5 answers)
Closed 4 years ago.
I have been programming for quite sometime and I came across something I never noticed before.
What is the difference between these two?
double[] nums = {1,2,3,4};
double[] nums2 = new double[]{1,2,3,4,};
They both compile and have the same properties. At first I thought that nums2 could accept a new int[] and have integer values in it since its lower down in the hierarchy. But It actually didn't work.
The first way is just a shortcut syntax to create and initialize an array of the second way.
This is the only difference.
Take a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

How to declare an array in java [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 6 years ago.
What does the below line of code mean?
is it a legal way to declare or define an array.
I googled it but couldn't find any information regarding this.
int[] []x[]
This is a valid line of code. It references a 3 dimensional integer array. Following codes are equivalent:
int[][][]x
int[][]x[]
int[]x[][]
int x[][][]
All of them are reference to a 3 dimensional integer array. You can declare a 10x10x10 size array like the following:
x = new int[10][10][10];

Java new Object[]{...} syntax [duplicate]

This question already has answers here:
Creating an array of objects in Java
(9 answers)
Closed 7 years ago.
I don't recognize this kind of form Java syntax for constructor - granted I do very little Java now (taken from there):
new PersistentArrayMap(new Object[]{formKey, form});
I was expecting something along the lines of new Object(...) as parameter. Could someone decompose the above line ?
You are creating new Object array Object[] initialised to {obj1, obj2}
It is a way of declaring and initializing an array in java. A simpler example is the following:
int[] myIntArray = new int[]{1,2,3};

What is the correct way to create a object array in java? [duplicate]

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I've seen examples such as:
Type arrayname[] = new Type[];
also as written as:
Type[] arrayname = new Type[]
I am quite confused about such expressions!
Where exactly should I put the []?
Any of the above are allowed. All produce the same bytecode. JLS-10.2 Array Variables says (in part)
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

Array is initializing more elements than told too [duplicate]

This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
Java:
int [] list = new int [7];
System.out.print(list);
In the console it prints: [I#1f96302
Not only is it giving values other than ints, but it is giving more than I asked it too.
[l#1f96302 is the default way an Object is printed (that's what Object's toString() method returns for arrays). Try System.out.print(Arrays.toString(list)) instead, which will display the elements of the array.

Categories