Win a copy of Rust Web Development this week in the Other Languages 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

Anonymous Array

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading first chapter of K&B book .
In K&B book it is mention that :
We call this Anonymous Array creation because you don't even need to assign the new array to anything .

And when I am trying this line in main method it is giving compile time error :

new int[]{2,3};

please solve my doubt .

thanks .
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is, you try to new an array object but then the syntax is not a Java statement, because you cannot start a Java statement with "new" keyword.

If you create an anonymous array like:
methodA(new int[]{2,3});

It should work.

Nick
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most expressions have to be used somehow, they can't be the whole statement. The big exception is a method invocation expression like "myMethod(x);". "new int[]{2,3};" is not a method invocation expression.

The examples in K&B were:
int[] testScores;
testScores = new int[]{4,7,2};

and:
f.takesAnArray(new int[] {7,7,8,2,5});

These cases include an array creation expression and use that expression in a statement.

[ December 27, 2004: Message edited by: Mike Gershman ]
[ December 27, 2004: Message edited by: Mike Gershman ]
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waw ! what a memory Mike ...
But you have missed one element at the time of intialization ..

Thanks Mike . I got the answer .
Thanks Nicholus . Nicholus please see SCWCD forum .. One question is waiting for you ...
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

I don't understand what "anonymous" refers to in the following example code from the B&K book:



The B&K book continues to state the following:


We call this anonymous array creation because with this syntax you don't even need to assign the new array ot anything.



I don't understand what they mean with this - since the array IS in fact assigned to the testScores variable. Can anybody help me to understand?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an explaination or arrays and it has a small section on anonymous arrays. Sometimes it is just the way the person is trying to explain it. This might be easier to understand.

http://ora-java.h1.ru/exp/ch04_06.htm

Regards

Sal
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading 2nd chapter of k&B . From the previous discussion I got this :

In general anonymous means which has no name .

So anonymous array means the array object which has no reference .

new Array[]{1,3,4};

this line is logically right but giving error because we can't start any sentence in java with new keyword .

So we can see the example of this type of array by passing it into the method like K&B book .
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mag Hoehme:
Hi folks,

I don't understand what "anonymous" refers to in the following example code from the B&K book:



The B&K book continues to state the following:



I don't understand what they mean with this - since the array IS in fact assigned to the testScores variable. Can anybody help me to understand?



Yes, you don't have to but you can
{4, 7, 2} is an anonymous array which is assigned to the reference variable testScores.
 
Did you just should on me? You should read this 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