Example of Array Declaration in Different Data Types

Views 15

Answer:

Integer Array:


int[] intArray = new int[10]; // Array of 10 integers

String Array:


String[] strArray = {"Java", "Python", "C++"}; // Array of 3 strings

Double Array:


double[] dblArray = new double[4]; // Array of 4 double elements

Float Array:


float[] floatArray = new float[5]; // Array of 5 float elements

Character Array:


char[] charArray = {'A', 'B', 'C', 'D'}; // Array of characters

Boolean Array:


boolean[] boolArray = {true, false, true}; // Array of boolean values

Long Array:


long[] longArray = new long[6]; // Array of 6 long elements

Byte Array:


byte[] byteArray = new byte[4]; // Array of 4 byte elements

Short Array:


short[] shortArray = new short[3]; // Array of 3 short elements

Description:

  • int: Stores integers (whole numbers), e.g., int[] intArray = new int[10];
  • String: Stores sequences of characters, e.g., String[] strArray = {"Java", "Python", "C++"};
  • double: Stores double-precision floating-point numbers, e.g., double[] dblArray = new double[4];
  • float: Stores single-precision floating-point numbers, e.g., float[] floatArray = new float[5];
  • char: Stores a single character, e.g., char[] charArray = {'A', 'B', 'C', 'D'};
  • boolean: Stores true/false values, e.g., boolean[] boolArray = {true, false, true};
  • long: Stores long integers, e.g., long[] longArray = new long[6];
  • byte: Stores byte-sized integers, e.g., byte[] byteArray = new byte[4];
  • short: Stores small integers, e.g., short[] shortArray = new short[3];

This covers the fundamental data types in Java along with array declarations for each one.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.