Download Java short Array in Formats
QR Code for this Page
Java short Array

Java short Array

short Array in Java

Java short array is used to store short data type values only. The default value of the elements in a short array is 0.

With the following Java short array examples you can learn

  • how to declare Java short array
  • how to assign values to Java short array
  • how to get values from Java short array

Initializing a short Array in Java

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

// declares an array of shorts
short[] shortArray;

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collection framework or simply use Vector.

Syntax :
                        < data type > < variable >[];
                        < data type >[] < variable >;

Java short Array Example

/*  Java short Array Example
    Save with file name ShortArray.java	*/

public class ShortArray
{
	public static void main(String args[])
	{
		//JAVA SHORT ARRAY DECLARATION
		short s[];
		//MEMORY ALLOCATION FOR JAVA SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO JAVA SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		//JAVA SHORT ARRAY OUTPUT
		System.out.println("Java short Array Example");
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}

Following Java short array example you can learn how to assign values to Java short array at the time of Java short array declaration.

How to assign values to Java short array at the time of Java short array declaration

/*  How to assign values to Java short array at the time of Java short array declaration Example
    Save with file name ShortArray2.java    */

public class ShortArray2
{
	public static void main(String args[])
	{
		//JAVA SHORT ARRAY DECLARATION AND ASSIGNMENT
		short s[] = {10,30,40,7};
		//JAVA SHORT ARRAY OUTPUT
		System.out.println("Java short Array Example");
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}

Following Java short array example you can learn how to declare Java short array with other Java short array variables.

How to declare Java short array with other Java short array variables

/*  How to declare Java short array with other Java short array variables Example
    Save with file name ShortArray3.java    */

public class ShortArray3
{
	public static void main(String args[])
	{
		//JAVA SHORT ARRAY DECLARATION
		short s[], s2; //s IS AN ARRAY s2 IS NOT AN ARRAY
		//MEMORY ALLOCATION FOR JAVA SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO JAVA SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		s2 = 101;
		//JAVA SHORT ARRAY OUTPUT
		System.out.println("Java short Array Example");
		System.out.println("value of s2 : "+s2);
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}

Following Java short array example you can learn how to assign Java short array to other Java short array.

How to assign Java short array to other Java short array

/*  How to assign Java short array to other Java short array Example
    Save with file name ShortArray4.java    */

public class ShortArray4
{
	public static void main(String args[])
	{
		//JAVA SHORT ARRAY DECLARATION
		//s AND s2 ARE ARRAY VARIABLES
		short[] s, s2;
		//MEMORY ALLOCATION FOR JAVA SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO JAVA SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		//ASSIGNING s ARRAY TO s2 ARRAY VARIABLE
		s2 = s;
		//JAVA SHORT ARRAY OUTPUT
		System.out.println("Java short Array Example");
		System.out.println("s array values");
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
		System.out.println("s2 array values");
		for(int i=0;i<s2.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s2[i]);
		}
	}
}
Download Java short Array in Formats