• 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

Int Primitive varargs overloading VS Int Array

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have two overloading method say

and we call method with int primitive variable and int array...
e.g



only overloaded(int... a) is called for both method invocation not the second one... As we all know array is object and int is not an object but primitive. theny why void overloaded(int... a) is accepting object array. Please clarify it. Thanks
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rana Asif wrote:



Hint: For compiler it would be
 
Rana Asif
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply...
so if for compiler it is like

then can you call for primitive value? like I dont think so...
 
Greenhorn
Posts: 10
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rana Asif wrote:Thanks for reply...
so if for compiler it is like

then can you call for primitive value? like I dont think so...



var args are 1 dim arrays for the compiler.the second one will be a 2 dim array.therefore it wont be used
 
Rana Asif
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulas Kainssoenn wrote:

Rana Asif wrote:Thanks for reply...
so if for compiler it is like

then can you call for primitive value? like I dont think so...



var args are 1 dim arrays for the compiler.the second one will be a 2 dim array.therefore it wont be used


My confusion is that even the compiler read it as 1dim array then why it is accepting the primitive value too? is it reading something like
 
Ulas Kainssoenn
Greenhorn
Posts: 10
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its like that.The first one expects 0 to many ints.This it self is an array.the second one is also an 1 dim array.So the first will be the right choice.Think that you can write at main public static void main(String [] args) OR public static void main(String... args).They are the same thing.The second one expects 0 to many
1 dim arrays,so arrays of arrays.
 
Rana Asif
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry but I dont get it...
can you call

but you can call

so its mean both are not equal...

Ulas Kainssoenn wrote:Its like that.The first one expects 0 to many ints.This it self is an array.the second one is also an 1 dim array.So the first will be the right choice.Think that you can write at main public static void main(String [] args) OR public static void main(String... args).They are the same thing.The second one expects 0 to many
1 dim arrays,so arrays of arrays.

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In effect, the compiler is massaging the code before compiling it. A method declared with (int...) gets converted to taking an array. You can confirm this by trying to declare overloaded(int... a) and overloaded(int[] a) in the same class - you'll get an error.

But the compiler is also able to convert a call to a method declared like this in a similar way. If you write overloaded(10), the compiler realises it can match this to overloaded(int... a), and converts the call to the equivalent of overloaded(new int[] { 10 }).

This sort of thing is known as "syntactic sugar", in that the compiler is allowing you to write things in a short-hand way rather than providing completely new functionality.
 
Rana Asif
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:In effect, the compiler is massaging the code before compiling it. A method declared with (int...) gets converted to taking an array. You can confirm this by trying to declare overloaded(int... a) and overloaded(int[] a) in the same class - you'll get an error.

But the compiler is also able to convert a call to a method declared like this in a similar way. If you write overloaded(10), the compiler realises it can match this to overloaded(int... a), and converts the call to the equivalent of overloaded(new int[] { 10 }).

This sort of thing is known as "syntactic sugar", in that the compiler is allowing you to write things in a short-hand way rather than providing completely new functionality.


Thanks for this explanation.. this makes perfect sense...
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic