• 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

illegal start of expression -- arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the java language structured such that in regard to my second invocation of the foo method I get a compiler error:


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that your foo method requires an int array and that is not what you are passing it. Arrays in java are objects, so you need to create one to send. It looks to me like neither invocation will work. All objects, except Strings, need to be explicitly created with the new keyword.
public class IllegalStartOfExpression{
public static void main(String [] args){
int[] x = new int[]{1, 2};
foo(x);
foo(new int[]{1, 2});
}
public static void foo(int[] x){
System.out.println(x[0]);
}
}

------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I could add something: The confusing thing about anonymous arrays (which is what we're talking about), is that at first glance it appears that an initialization list is the anonymous array: The confusing part (for me) was that I have a book that says The construct (ie: the anonymous array) returns an array reference which can be assigned, and passed as a parameter. In particular, the following two examples are equivalent:
int[] intArray = {2,3,4,5};
int[] intArray = new int[] {2,3,4,5};

I thought that {2,3,4,5} was an anonymous array, but of course it's not, it's simply an initialization list, and not the anonymous array itself. So this list by itself cannot be passed to a method which expects an array.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic