java - What can an Object[] array hold? -


i new java programming language , had question arrays. string[] arrays hold strings. array[] arrays hold other arrays. object[] array? clearly, these hold objects. but, since object superclass in java, mean object[] array can hold every type of object in java? in other words, can array hold objects child classes of object array created hold? can number[] array hold integer?

yes can learn lot trying small program:

public class example {     public static void main(string[] args) {         string string = "string";         integer integer = new integer(1);         int integerprimitive = 2;         float floatboxed = new float(1.23);         float floatprimitive = 1.23f;          // can hold different types inheriting object         object[] objects = new object[] {                  string,                  integer,                  integerprimitive,                 floatboxed,                  floatprimitive };          // can hold inherits number; cannot hold string         number[] numbers = new number[] {                  integer,                  integerprimitive,                  floatboxed,                 floatprimitive };          (int = 0; < objects.length; i++) {             system.out.println("objects[" + + "] = " + objects[i]);         }          (int = 0; < numbers.length; i++) {             system.out.println("numbers[" + + "] = " + numbers[i]);         }     } } 

output:

objects[0] = string objects[1] = 1 objects[2] = 2 objects[3] = 1.23 objects[4] = 1.23 numbers[0] = 1 numbers[1] = 2 numbers[2] = 1.23 numbers[3] = 1.23 

the key knowing array container can hold first observing if object types same or if object sub-class of array container type.

in question if number can hold integer, should see inheritance of integer in javadocs inherits number. can see number inherits object.

class number

class integer


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -