• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding var-args

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

While studying for the certification, i came across this " Two methods with the same name, return type, access modifier but one contain int var-args and an other contain int[] and THEY ARE NOT DIFFERENT" so i tried to write a simple program for this which is,


The error on Line 15 is "The method arrays(int[]) in the type VarargsArray is not applicable for the arguments (int)"...
Q1) So does it mean i cannot call a method with argument int.
Q2) if i change the second method to private int arrays(int... var)
then will the program execute or will the method be overloading?
Please advise and Thanks in advance!

Regards,
Deepa
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, please UseCodeTags.

Now, this code wouldn't even compile because two methods have the same name and signature. For the purpose of signatures, yes, int ... values, and int [] values are the same. However, there's a difference in how you call those two methods. In the first case, you can call it with a comma separated list of int values, where a single int value can be thought of as a list with just one value. When you define the parameter as a integer array though, then you must call it with an integer array argument. In either case, you would treat the parameter values as an int array within the method. So varargs are the same as arrays of args in some ways, and different in others.
 
Deepa Chandrashekar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer!
My question is if i change the access modifier and return type of second method will it be overloading?
Since var-args are similar to arrays but they cannot be interchangeable.

Please advise

Regards,
Deepa.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the rule of a valid overload? You MUST change the argument list, you may change return type, access modifier,...

So just changing access modifier and/or access modifier will not be a valid overload. That's something you can easily confirm using Notepad and javac (or using an editor).

Have a look at this thread.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic