Online, the question of whether arrays are objects or variables is conflicting. Are arrays objects, or variables?
Blue Pelican Java book claims that they are variables, but they must be instantiated, so I'm not sure.
Online, the question of whether arrays are objects or variables is conflicting. Are arrays objects, or variables?
Blue Pelican Java book claims that they are variables, but they must be instantiated, so I'm not sure.
when you ask "Are arrays objects, or variables?" I think you mean "Are arrays objects, or primitive data types?"
Arrays are objects and refer to a collection of primitive data types or other objects.
Arrays can store two types of data:
Try this code to check whether array is object or not .
String[] str=new String[] {"A","B","X"};
if (str instanceof Object){
System.out.println("Yes!");
}else{
System.out.println("No!");
}
instanceof? In other words, it will always print Yes! (well unless str is null).null reference (for primitive types it won't even compile).In simple terms, a variable is how you declare an object and access it. So they are not two mutually exclusive things.
An array is an object, and you can use a variable to access it. While an array is an object, it may hold values of primitive types (example int[]) or it may hold objects of a class type (example String[])
int[] arr1 = new int[2];
System.out.println(arr1[0]); //output: 0
This will create an array object that can hold two values of primitive type int. The array object can be accessed using the variable arr1. Since the array holds primitives, they will be initialized to the default value 0 (or false for boolean).
String[] arr2 = new String[2];
System.out.println(arr2[0]); //output: null
This will create an array object that holds two objects of class type String. The array object can be accessed using the variable arr2. Since the array holds objects, it will initially hold null, which means no object.