Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Strange 'var arg' method.

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have discovered today really strange (for me) thing in Java. On one hand it makes sense, but on the other, I just want to scream 'What the heal?'. I put it here, because it might be useful for people who are learning for SCJP.

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this is totally normal, if we pay attention to closely line 6-8 we'll see the behavior clearly.

Line6 create an anonymous String array and assigned it to ref variable "list" type Object.
Line 7 passing a copy of "list" into foo() method that takes zero to many Objects.
Line 8 a new an anonymous array declared, initialized, and created in the foo() method.

Do you see the different between line 6 & 8?? That's where it get interesting....
 
Tom Kowalski
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to be rough, but you have just 'read' what I have put in code. Nothing more, nothing less.
 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Kowalski wrote:I have discovered today really strange (for me) thing in Java. On one hand it makes sense, but on the other, I just want to scream 'What the heal?'. I put it here, because it might be useful for people who are learning for SCJP.



Good point ! When I tried compiling it, the compiler throws up this informative warning:

warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning
foo(new String[] {"j","a","v","a"});

So, in essence when you pass a String[] to foo, the call is treated as a non-varargs call.
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nothing strange here. Signature:

means, that you can pass as an array of Objects or just one Object. If you pass one object it will be converted to one-element Object array, so in the code

you pass 'list' variable which is an Object type - this one is converted to one element Object array
However when you call:

compiler first needs to cast String[] array either to Object or to Object[] (both are accepted for foo() method signature). It does it trying to cast to more specific parameter first (Object[]). If you don't wan't to let it choose which casting is better you can explicitely do your favourite casting before:

which results in 2 different arrays (first is one element array containing String[] array as the only element, second is 4 element Object[] array containing objects of type String)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a very interesting example. You've got to be careful with Object... arguments, because of they way they can match anything.

The relevant part of the Java Language Specification says:

The process of evaluating of the argument list differs, depending on whether the method being invoked is a fixed arity method or a variable arity method (§8.4.1).

If the method being invoked is a variable arity method (§8.4.1) m, it necessarily has n>0 formal parameters. The final formal parameter of m necessarily has type T[] for some T, and m is necessarily being invoked with k>=0 actual argument expressions.

If m is being invoked with k!=n actual argument expressions, or, if m is being invoked with k=n actual argument expressions and the type of the kth argument expression is not assignment compatible with T[], then the argument list (e1, ... , en-1, en, ...ek) is evaluated as if it were written as (e1, ..., en-1, new T[]{en, ..., ek}).



In this case, a String[] can be assigned to an Object[], but an Object (the reference type of the variable) can't be without an explicit cast.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic