About Question enthuware.ocajp.i.v7.2.1052 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1052 :

Post by Sweetpin2 »

I am not able to understand the explanation of above question.

An interesting observation is that if you do javap on the following class, you will see the same signature for method m1 and m2. This shows that a var-arg parameter of type T is same as an array of type T.
class TestClass {
String m1(int[] i) { return ""+i.length; }
String m2(int... i) { return ""+i.length; }
}


javap TestClass produces this:

java.lang.String m1(int[]);
java.lang.String m2(int[]);


Conversely, the following code will not compile:

class TestClass {
String m1(int[] i) { return ""+i.length; }
String m1(int... i) { return ""+i.length; } // Compiler determines that this is a duplicate method.
}

Can you help me by giving detail explanation.

admin
Site Admin
Posts: 10115
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by admin »

There is nothing much going on here. The ... notation is translated by the compiler into an array argument. So int ... and int[] are the same thing.
If you like our products and services, please help us by posting your review here.

terence_j_coffey
Posts: 2
Joined: Wed Jun 05, 2013 6:01 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by terence_j_coffey »

I ran the following using Eclipse and it worked fine.
Have I misunderstood your explanation, as this appears to contradict your conclusion ?

public class TestClass {

String m1 (int[] i) { System.out.println(i.length); return ""+i.length ;}
String m2 (int...i) { System.out.println(i.length); return ""+i.length ;}
public static void main(String[] args) {
TestClass tc = new TestClass();
int[] a = { 1,2,3,4,5};
tc.m1(a);
tc.m2(1,2,3,4);

}

}

Compiled from "TestClass.java"
public class TestClass {
public TestClass();
java.lang.String m1(int[]);
java.lang.String m2(int...);
public static void main(java.lang.String[]);
}

admin
Site Admin
Posts: 10115
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by admin »

Why do you think it should not compile? I don't see any contradiction with the explanation.

-Paul.
If you like our products and services, please help us by posting your review here.

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by Marthinus »

javap TestClass produces this when I run it:

class app.TestClass {
app.TestClass();
java.lang.String m1(int[]);
java.lang.String m2(int...);
}

I just put it in an app package. Don't believe that should change anything?

admin
Site Admin
Posts: 10115
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by admin »

Not sure what you mean. Please specify exactly what is your doubt.
If you like our products and services, please help us by posting your review here.

danko82
Posts: 5
Joined: Fri Aug 28, 2015 7:24 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by danko82 »

I really don't understand why this is wrong:
public void test1(int[] i){}

If I understood well int[] i, int i[] and int... i are the same.
Can you explain me better the question?
Thanks

admin
Site Admin
Posts: 10115
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by admin »

This is a rule of the Java language. Even though a var-arg parameter of type int is very similar to int[], they are not interchangeable. Therefore, int[] cannot be substituted for int... in the code.

Also, there is a fundamental difference - int... can take one int argument, while int[] i or int i[] must take an int array. You can't just pass one int to a method that takes int array.
If you like our products and services, please help us by posting your review here.

lukaszet
Posts: 1
Joined: Thu Sep 17, 2015 6:27 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by lukaszet »

admin wrote:This is a rule of the Java language. Even though a var-arg parameter of type int is very similar to int[], they are not interchangeable. Therefore, int[] cannot be substituted for int... in the code.

Also, there is a fundamental difference - int... can take one int argument, while int[] i or int i[] must take an int array. You can't just pass one int to a method that takes int array.
Sorry, I still don't understand it. Why below option is invalid?

Code: Select all

public void test1(int[] i){}
test1(10); //1
test1(10, 20); //2

In some questions I've encountered main method that takes one String as an argument which is converted to array of lenght 1. Isn't (int[] i) similar to (String[] args)?

admin
Site Admin
Posts: 10115
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1052 :

Post by admin »

1. Because i is an array of ints, but you are passing either a single int.
2. Because method test1 takes a parameter that is an array of ints, but you are passing two individual ints.

main method does not take one or two or any number of Strings as an argument. main method takes a String array as an argument. What you do is, you specify one string or multiple strings on the command line. The JVM puts all of them into a String array and then passes that array to the main method.

As mentioned above the ... operator and [] are not interchangeable. i.e. ... and [] are not exactly same although in some aspects they do operate similarly.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 17 guests