Java array declaration syntax difference? [duplicate] - java

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
Is there any difference in declaring array argument in java like:
public void method(Type[] arg) { ... }
and in that way:
public void method(Type arg[]) { ... }
Just curiosity...

It is better practice to place them after the type, it is for understanding purposes. Also note that:
String[] firstArray[], secondArray;
Here you have firstArray variable referring to a two dimensional array and secondArray variable refering to an one dimension array;

No difference.
In SCJP Sun Certified Programmer for Java 6:
When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, key is a reference to an int array object, and not an int primitive.

Related

If Java does not supports pointers then how does it keep record of memory locations where data is saved? [duplicate]

This question already has answers here:
Does Java have pointers?
(12 answers)
Closed 5 years ago.
Programming languages like C and C++ use pointers but Java does not use them for security reasons, then how does it keep record of dynamically allocated memory which in C is maintained using pointers?
Java does indeed have pointers. Let's say that you have a class A with an attribute x with setter and getter void set_x(int) and int get_x() and consider this code:
public static void foo(A a)
{
a.setx(13);
}
public static void main()
{
A a = new A();
a.setx(12);
foo(a);
System.out.println(a.getx());
}
This will print 13 so that's indeed a pointer. However, you cannot declare a pointer and make it point as freely as you can in C. Nor can you do pointer arithmetics.
And there's lots of pointers inside the Java Virtual Machine, but they are not exposed to the programmer.

what is the difference between passing array to a method with array parameter and var-args in java? [duplicate]

This question already has answers here:
varargs and the '...' argument
(3 answers)
Closed 6 years ago.
I have gone through a note on var-args in java. I wondered what is the difference it make with the array parameter while calling a method.
public void doSomething(int[] a){
// some logic here
}
public void doSometing(int... a){
// some logic here
}
the above two methods were called by
int[] x={1,2,3,4,5};
doSomething(x);
is both of them are same or some difference exists?
and is it possible to overload these two methods?
The two method signatures are the same, and they do not allow overloading.
The only difference is that calling doSomething(1, 2, 3) with the vararg signature is allowed, while calling the same with the array signature results in an error.

What is the best way to send a method a dynamic number of variables? [duplicate]

This question already has answers here:
Can I pass an array as arguments to a method with variable arguments in Java?
(5 answers)
Closed 6 years ago.
I have a java program with a method that will consistently receive a different number of int values. What is the best way to send it the ints?
A construct called varargs (or an arbitrary number of arguments) helps you.
method(int... ints) { ... }
Then, varargs will be turned into int[] by the compiler.
OK, how to call those methods?
method(1, 2, 3);
method(new int[]{1, 2, 3});
About the question in the comments:
method(Arrays.stream(yourStringArray).mapToInt(Integer::parseInt).toArray());
You may convert String[] to int[] firstly, and then pass the result to the method.
Something like below you can do, making use of dot notation for method declaration with multiple arguments
public TestClass
{
public void process(int... variables)
{
// process your variables,
// Iterate through variables
}
}
Plus refer this, Java multiple arguments dot notation - Varargs

What does (String... arg) mean in a function definition? [duplicate]

This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
public static void main (String... arg)
I have never encountered the ... part in the function definition. Could anyone give some insight into this notation?
The ... indicates that you are passing 0 or more arguments of the type and the method will access them as an array of objects of the type. You may pass them as an array or as a sequence of objects of the declared type.
I.e.:
In your main method use
String firstArg = arg[0];
to access the first argument.
Look at the documentation of varargs for more info.

How to find int[].length on Java Documentation ? [duplicate]

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 7 years ago.
I am just curious ! When I use an int table, I can get to .length, which returns the length of the current table. For exemple :
int b[] = {0,1,2};
int l = b.length; // which is equal to 3
What I want is to get to ".length" in the Java documentation. In order, to figure out if ".length" is a static method or instance variable , and things like that.
From the JLS
10.7. Array Members
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

Categories