Anonymous Array in Java - GeeksforGeeks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

3/3/22, 9:58 PM Anonymous Array in Java - GeeksforGeeks

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python

Anonymous Array in Java


Difficulty Level :
Easy ● Last Updated :
02 Dec, 2021

An array in Java without any name is known as an anonymous array. It is an array

just for creating and using instantly. Using an anonymous array, we can pass an array

with user values without the referenced variable.

Proper ties of Anonymous Arrays :

We can create an array without a name. Such types of nameless arrays are called

anonymous arrays.

The main purpose of an anonymous array is just for instant use (just for one-time

usage).

An anonymous array is passed as an argument of a method.

Note : For Anonymous array creation, do not mention size in []. The number of

values passing inside {} will become the size.

Syntax : 

new <data type>[]{<list of values with comma separator>};

Examples :

// anonymous int array

new int[] { 1, 2, 3, 4};

//useanonymous
We char
cookies to ensure array
you have
browsing experience on our website. By using our site, you
acknowledge
the best
that you have read and understood our
Cookie Policy &
Privacy Policy
new char[] {'x', 'y', 'z');

Got It !

https://www.geeksforgeeks.org/anonymous-array-java/ 1/5
3/3/22, 9:58 PM Anonymous Array in Java - GeeksforGeeks

// anonymous String array

new String[] {"Geeks", "for", "Geeks"};

Start Your Coding Journey Now! Login Register


// anonymous multidimensional array

new int[][] { {10, 20}, {30, 40, 50} };

Implementation:

Java

// Java program to illustrate the


// concept of anonymous array
 
class Test {
    public static void main(String[] args)
    {
          // anonymous array
          sum(new int[]{ 1, 2, 3 });
    }
   
    public static void sum(int[] a)
    {
        int total = 0;
 
        // using for-each loop
        for (int i : a)
            total = total + i;
         
        System.out.println("The sum is: " + total);
    }
}

Output

The sum is: 6

Explanation: In the above example, just to call the sum method, we required an

array, but af ter implementing the sum method, we are not using the array anymore.

Hence for this one-time requirement anonymous array is the best choice. Based on

our requirement, we can later give the name to the anonymous array, and then it will

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
no longer be anonymous. 
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/anonymous-array-java/ 2/5
3/3/22, 9:58 PM Anonymous Array in Java - GeeksforGeeks

This ar ticle is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and

Start Your Coding Journey Now!


would like to contribute, you can also write an ar ticle using write.geeksforgeeks.org

Login Register
or mail your ar ticle to review-team@geeksforgeeks.org. See your ar ticle appearing

on the GeeksforGeeks main page and help other Geeks. Please write comments if

you find anything incorrect, or you want to share more information about the topic

discussed above.

Like 8

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

Diamond operator for Anonymous Difference between Anonymous


01 05
Inner Class with Examples in Java Inner Class and Lambda
15, Jan 20
Expression
17, Jun 19

Java Program to Check if a Given Difference Between java.sql.Time,


02
Class is an Anonymous Class 06
java.sql.Timestamp and
16, Nov 20
java.sql.Date in Java
We use cookies to ensure you have the best browsing experience on our07,
website.
Apr 21
By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Anonymous Inner Class in JavaGot It ! How to Convert java.sql.Date to
03 19, Dec 16

https://www.geeksforgeeks.org/anonymous-array-java/ 3/5
3/3/22, 9:58 PM Anonymous Array in Java - GeeksforGeeks

java.util.Date in Java?
07 02, Feb 21

Start Inner
YourClass
Coding JourneyInner
And Anonymous Now! Login Register
04 Class that Implements Runnable | Different Ways to Convert
08
Concurrent Programming java.util.Date to
Approach 3 java.time.LocalDate in Java
08, Apr 19 05, Jan 21

Ar ticle Contributed By :

GeeksforGeeks

Vote for difficulty

Current difficulty :
Easy

Easy Normal Medium Hard Expert

Improved By : nishkarshgandhi

Article Tags : Java-Array-Programs, Java-Arrays, Java

Practice Tags : Java

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
Load Comments
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/anonymous-array-java/ 4/5
3/3/22, 9:58 PM Anonymous Array in Java - GeeksforGeeks

5th Floor, A-118,

Start Your Coding Journey Now!Uttar PradeshLogin


Sector-136, Noida, - 201305 Register
feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Machine learning
Contact Us CS Subjects
Copyright Policy Video Tutorials

News Languages
Technology Python
Work & Career Java
Business CPP
Finance Golang
Lifestyle C#

Web Development Contribute


Web Tutorials Write an Article
HTML Pick Topics to Write
CSS Write Interview Experience
JavaScript Internships
Bootstrap Video Internship

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/anonymous-array-java/ 5/5

You might also like