- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assigning arrays in Java.
While creating variables first of all we will declare them, initialize them, assign/re-assign values to them.
Similarly, while creating arrays −
You can declare an array just like a variable −
int myArray[];
You can create an array just like an object using the new keyword −
myArray = new int[5];
You can initialize the array by assigning values to all the elements one by one using the index −
myArray [0] = 101; myArray [1] = 102;
Assigning values to an array
When we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.
But, when you try to assign a higher datatype to lower, at the time of compilation you will get an error saying “incompatible types: possible lossy conversion”
Therefore while assigning values to the elements of an array you can assign any value which will be cast implicitly.
Example
public class ArrayExample { public static void main(String args[]){ //Declaring an array int[] myArray = new int[5]; //Assigning values to the array myArray[0] = 2020; myArray[1] = 'h'; byte by = 4; myArray[2] = by; short sh = 8; myArray[3] = sh; myArray[4] = 465; System.out.println(Arrays.toString(myArray)); } }
Output
[2020, 104, 4, 8, 465]
But, if you try to assign incompatible types (higher types) to a variable a compile-time error will be generated saying “possible lossy conversion”.
Example
import java.util.Arrays; public class ArrayExample { public static void main(String args[]){ //Declaring an array int[] myArray = new int[5]; //Assigning values to the array myArray[0] = 2020; myArray[1] = 'h'; byte by = 4; myArray[2] = by; short sh = 8; myArray[3] = 152L; myArray[4] = 465; System.out.println(Arrays.toString(myArray)); } }
Output
Compile-time error
ArrayExample.java:12: error: incompatible types: possible lossy conversion from long to int myArray[3] = 152L; ^ 1 error
If you have an array of objects while assigning values to the elements of it, you need to make sure that the objects you assign should be of the same or, a subtype of the class (which is the type of the array).
Example
import java.util.Arrays; public class ArrayExample { public static void main(String args[]){ //Declaring an array Number[] num = new Number[5]; //Assigning values to the array num[0] = 2020; num[1] = 125L; num[2] = 2545.F; num[3] = 152L; num[4] = 465; System.out.println(Arrays.toString(num)); } }
Output
[2020, 125, 2545.0, 152, 465]
But to an array of objects if you assign a value which is neither the declared type nor its subtype a compile-time error will be generated.
Example
import java.util.Arrays; public class ArrayExample { public static void main(String args[]){ //Declaring an array Number[] num = new Number[5]; //Assigning values to the array num[0] = 2020; num[1] = 125L; num[2] = 2545.F; num[3] = 152L; num[4] = "hello"; System.out.println(Arrays.toString(num)); } }
Output
ArrayExample.java:11: error: incompatible types: String cannot be converted to N umber num[4] = "hello"; ^ 1 error
If you have created an array of objects of an interface type you need to make sure that the values, you assign to the elements of the array are the objects of the class that implements the said interface.