How to create and initialize Anonymous array in Java Example

0
304
views
java_anonymous_array in Hindi, Core Java Guide, Core Java Tutorials
java_anonymous_array in Hindi, Core Java Guide, Core Java Tutorials

Java Anonymous Array in Hindi

Definition of Java Anonymous Array:- Some times we create an array without any name also, such type of nameless Arrays are called “Java Anonymous Array.

The main objective of Java anonymous Array is just for instant use

We can use it only once but not in future use.

We can create this array like this,

Syntax : 

new  int[]{10,20,30,40}   // valid

Remember:- the time of array creation we can’t specify the size.  Otherwise we will get compile time error.

Syntax:    new int[5]{1,2,3,4,5}   // not valid

Java Anonymous Array in Hindi, Core Java Tutorials in Hindi, Core Java Guide

Java Anonymous Array in Hindi, Core Java Tutorials in Hindi, Core Java Guide

This is Simple program  of Anonymous Array in Java

program name: demo_Anonymous_Array.java

package Core;

public class demo_Anonymous_Array {

       public static void main(String args[]) {

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

       }

       public static void add(int[] x) {

              int ans = 0;

              for (int y : x) {

                     ans = ans + y;

              }

              System.out.println(“The Java Guide’s sum:” + ans);

       }

}

OUTPUT:-

The Java Guide’s sum:10

You may like to check Java Array

Go4Vidhya: visit for more

LEAVE A REPLY

Please enter your comment!
Please enter your name here