Creating an Array with Reference Variables Flashcards

1
Q

Same for toString() applied on arrays !

A

public static String toString(Object[] a)

It belongs to the java.util.Arrays class

String[] letters1= {“A”,”B”,”C”};
System.out.println(java.util.Arrays.toString(letters1));//prints [A,B,C]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Give a particularity of the equals method when applied to an array !

A

int[] array1 = new int[] {1,2,3,4};
int array2[] = array1;
System.out.println(Arrays.toString(array2)); // [1, 2, 3, 4]
System.out.println(array1.equals(array2)); // Prints true

int[] array1 = new int[] {1,2,3,4};
int array2[] = {1,2,3,4};
System.out.println(Arrays.toString(array2)); // [1, 2, 3, 4]
System.out.println(array1.equals(array2)); // Prints false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a characteristic of reference variable types of any array !

A

In Java, arrays of objects (like String[]) are arrays of references. For each element in the array, a reference is stored. These references point to the actual String objects that are stored elsewhere in memory.

For String[] variable = {“A”, “B”, “C”};, the JVM allocates an array of String references.

Each element of the String array (variable[0], variable[1], variable[2]) is a reference to a String object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define the java.util.Arrays.toString method !

A

The java.util.Arrays.toString method is a utility method in the Java standard library that provides a human-readable string representation of an array. This method is part of the java.util.Arrays class, which contains static methods for array manipulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain what happens in memory when creating an array !

A

EXAMPLE 1 :
String[] names; The variable names is a reference type variable that has been declared but not initialized. By default, it has a value of null, indicating that it currently does not point to any array of String objects. However, it has the potential to be assigned to reference an array of String objects in the future

EXAMPLE 2 :

String[] names = new String[2];
The variable names points to an array that can hold 2 String references. Initially, names[0] and names[1] are null, meaning they do not yet point to any String objects. However, they have the potential to be assigned references to String objects in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain why we can not assign a variable of type Object to an array of type string !

A

Object variable = new String[]{“test”};
String [] tab = variable; /* COMPILES SUCCESSFULL*/

Object: The Object class is the root of the Java class hierarchy. Every class in Java inherits from Object. Therefore, an Object reference can hold any type of object.

String[]: This is an array specifically designed to hold String objects. String[] is a more specific type compared to Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain why when we do assign a variable of type array of strings to variable of type object, we can not access elements of that array !

A

The Object class in Java provides basic methods that are available to all objects, such as equals(), hashCode(), toString(), getClass()

However, Object does not include methods for handling arrays, such as indexing, length retrieval, or array-specific operations.

Array-specific features like indexing (using []), accessing elements, and getting the length are intrinsic to arrays themselves, not part of Object.

Arrays have their own inherent properties and methods, like the length property and direct indexing, which are not defined in the Object class.

Since methods for array handling (e.g., array indexing) are not defined in the Object class, they are not overridden by any subclasses of Object. They are simply not part of the Object class in the first place.

Overriding in Java refers to providing a specific implementation of a method defined in a superclass in a subclass. Since array-specific methods are not defined in Object, there’s nothing to override.

EXAMPLE :

Object variable = tableau;

System.out.println(((String[])variable)[2]);//Prints A

You cannot access array elements through an Object reference because Object does not support array operations or indexing.

To access array elements, you need to cast the Object to the correct array type, which provides the necessary array operations and properties.

Casting: In Java, casting is a way to tell the compiler that you are treating an object as a different type. This is typically done when you have a reference to an object of a more general type and you want to use it as a more specific type.

When casting an Object to a more specific type (like String, Integer, or a custom class), it is important to ensure that the actual object is indeed of that type. If the cast is invalid, a ClassCastException will be thrown at runtime.

IMPORTANT :
Java’s type system allows polymorphism, where a superclass reference can point to a subclass object. Casting is used to access subclass-specific methods and properties:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly