Interesting facts about Array assignment in Java Last Updated : 06 Jan, 2019 Comments Improve Suggest changes Like Article Like Report Prerequisite : Arrays in Java While working with arrays we have to do 3 tasks namely declaration, creation, initialization or Assignment. Declaration of array : int[] arr; Creation of array : // Here we create an array of size 3 int[] arr = new int[3]; Initialization of array : arr[0] = 1; arr[1] = 2; arr[3] = 3; int intArray[]; // declaring array intArray = new int[20]; // allocating memory to array Some important facts while assigning elements to the array: For primitive data types : In case of primitive type arrays, as array elements we can provide any type which can be implicitly promoted to the declared type array. Apart from that, if we are trying to use any other data-types then we will get compile-time error saying possible loss of precision. JAVA // Java program to illustrate the concept of array // element assignments on int type array public class Test { public static void main(String[] args) { int[] arr = new int[3]; arr[0] = 1; arr[1] = 'a'; byte b = 10; arr[2] = b; System.out.println(arr[0] + arr[1] + arr[2]); } } Output: 108 JAVA // Java program to illustrate the concept of array // element assignments on int type array public class Test { public static void main(String[] args) { int[] arr = new int[3]; // Assigning long value to int type. arr[0] = 10l; arr[1] = 'a'; byte b = 10; arr[2] = b; System.out.println(arr[0] + arr[1] + arr[2]); } } Output: possible loss of precision. JAVA // Java program to illustrate the concept of array // element assignments on char type array public class Test { public static void main(String[] args) { char[][] arr = new char[2][2]; // Assigning long value to int type. arr[0][0] = 10l; arr[0][1] = 'a'; char b = 10; arr[1][0] = b; // Assigning double value to char type arr[1][1] = 10.6; } } Output: error: incompatible types: possible lossy conversion from long to char error: incompatible types: possible lossy conversion from double to char Object type Arrays : If we are creating object type arrays then the elements of that arrays can be either declared type objects or it can be child class object. JAVA // Java program to illustrate the concept of array // element assignments on Number type array public class Test { public static void main(String[] args) { Number[] num = new Number[2]; num[0] = new Integer(10); num[1] = new Double(20.5); System.out.println(num[0]); System.out.println(num[1]); } } Output: 10 20.5 JAVA // Java program to illustrate the concept of array // element assignments on Number type array public class Test { public static void main(String[] args) { Number[] num = new Number[3]; num[0] = new Integer(10); num[1] = new Double(20.5); // Here String is not the child class of Number class. num[2] = new String(“GFG”); } } Output: Compile-time error(incompatible types) JAVA // Java program to illustrate the concept of array // element assignments on Number type array public class Test { public static void main(String[] args) { Number[][] arr = new Number[2][2]; arr[0][0] = 10l; // Assigning char to Number type array arr[0][1] = 'a'; byte b = 10; arr[1][0] = b; // Assigning String to Number type array arr[1][1] = "GEEKS"; } } Output: error: incompatible types: char cannot be converted to Number error: incompatible types: String cannot be converted to Number Interface type array : For interface type array, we can assign elements as its implementation class objects. JAVA // Java program to illustrate the concept of array // element assignments on Interface type array public class Test { public static void main(String[] args) { Runnable[] run = new Runnable[2]; // Thread class implements Runnable interface. run[0] = new Thread(); run[1] = new Thread(); } } JAVA // Java program to illustrate the concept of array // element assignments on Interface type array public class Test { public static void main(String[] args) { Runnable[] run = new Runnable[2]; run[0] = new Thread(); // String class does not implements Runnable interface. run[1] = new String(“GFG”); } } Output: Compile-time error(Incompatible types) Explanation: In the above program, we are giving elements of String class that's cause compile time error. Because we know that String does not implements Runnable interface. Reference : https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html Comment More infoAdvertise with us Next Article Interesting facts about Array assignment in Java B Bishal Kumar Dubey Improve Article Tags : Misc Java Java-Arrays Java-Array-Programs Interesting-Facts +1 More Practice Tags : JavaMisc Similar Reads 25 Interesting Facts about Arrays in Java Java arrays are the workhorses of many programs, offering a structured way to store and manipulate data. While arrays are a fundamental concept, there are intriguing facts that developers may not be aware of for Java Arrays. In this blog post, we'll explore 25 interesting facts about arrays in Java, 8 min read Initialize an ArrayList in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.ArrayList inherits the AbstractList class and imp 4 min read Difference between Arrays and Collection in Java An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the co 3 min read One Dimensional Array in Java An array is a type of data structure that can store a collection of elements. These elements are stored in contiguous memory locations and provide efficient access to each element based on the index of each array element.In this article, we will learn about a one-dimensional array in Java.What is an 7 min read Internal Working of ArrayList in Java ArrayList is a resizable array implementation in Java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object classes. ArrayList class in Java has 3 constructors. It has its own version of readObject and wri 10 min read Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read AtomicLongArray incrementAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.incrementAndGet() is an inbuilt method in Java that atomically increments the value at any index of an AtomicLongArray by one. The method takes the index value of the AtomicLongArray as the parameter, increments the value at that index and returns the 2 min read How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java 5 min read Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value 5 min read Arrays Class in Java The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of an Object class. The methods of this class can be used by the class name itself.The 15 min read Like