What is the syntax to declare and initialize an array in java

+1 vote
I am unable to declare and initialize array in java. Can you please help?
Apr 12, 2018 in Java by Akrati
• 3,190 points

edited Apr 12, 2018 by Akrati 3,607 views

3 answers to this question.

0 votes
You can refer the following syntax for primitive data types:


int[] IntArr = new int[2];
int[] IntArr = {1,2};
int[] IntArr = new int[]{1,2};

You can refer the following syntax for classes, say string:

String[] StrArr = new String[3];
String[] StrArr = {"x","y","z"};
String[] StrArr = new String[]{"x","y","z"};

You can also declare the array first and then initialize it:



String[] StrArr;
StrArr = new String[]{"x","y","z"};










answered Apr 12, 2018 by nsv999
• 5,500 points
0 votes

One Dimensional Array

Syntax for default values:

int[] num = new int[5];
int num[] = new int[5];

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};
int num[] = {1, 2, 3, 4, 5};

Multidimensional array

Declaration

int[][] num = new int[5][2];
int num[][] = new int[5][2];
answered Jul 16, 2018 by Daisy
• 8,140 points
0 votes

You can use this method:

String[] strs = IntStream.range(0, 15)  // 15 is the size
    .mapToObj(i -> Integer.toString(i))
    .toArray(String[]::new);
answered Jul 25, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
1,029 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
13,094 views
0 votes
1 answer

What is the need to override the equals and hashCode methods in Java?

You must override hashCode() in every class ...READ MORE

answered Jan 2, 2019 in Java by Daisy
• 8,140 points
3,370 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an ...READ MORE

Feb 8, 2022 in Java by Edureka
• 120 points
407 views
0 votes
1 answer

How do I declare and initialize an array in Java?

To declare and initialize an array in ...READ MORE

answered Jun 22, 2023 in Java by Khan Sarfaraz
• 700 points
458 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,337 views
0 votes
2 answers

How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE

answered Jul 19, 2018 in Java by Sushmita
• 6,920 points
2,896 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
898 views
0 votes
2 answers

Array of Objects

You can also do : A[] a = ...READ MORE

answered Aug 3, 2018 in Java by sharth
• 3,370 points
835 views
0 votes
1 answer

Why do we use threads in Java?

Reasons for using Multithreading in Java: When a ...READ MORE

answered Nov 2, 2018 in Java by nsv999
• 5,500 points
1,264 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP