Java Array Revealed_2

In Java array of characters is not a String :



In Java programming language an array of char is  neither a String nor an array of char is terminated by '\u0000'  (the NULL character).
A String object is immutable, that is, its contents never change, while an array of
char has mutable elements,But the length of array is final.

Java Array Revealed_1

IN the Java programming language, arrays are objects ,and  dynamically
created, and may be assigned to variables of type Object. All methods of
class Object may be invoked on an array.

Let's talk about Array variables:

A variable of array type holds a reference to an object. Declaring a variable of array
type does not create an array object or allocate any space for array components. It

creates only the variable itself, which can contain a reference to an array.
And interesting thing is about the array declaration style.

Example:

byte[ ] set, get, wet[ ];

This declaration is equivalent to:


byte set[ ], get[ ], wet[ ] [ ];


It is not recommend  "mixed notation" in an array variable declaration, where

brackets appear on both the type and in declarators

Array Mutability

Just as with object mutability, you need to be aware of array mutability. First look this example below.


public class ShippingInfo {
private static final String[] states = {
"AK", "AZ", "CA", "DE", "NV", "NY"};
}

The following code fragment could be used to iterate through the list of states and print them out.
for (int i = 0; i < ShippingInfo.states.length; i++) {
System.out.println(ShippingInfo.states[i]);
}
This is easy enough, but using the final keyword with arrays can be tricky. The following code does not
compile as you might expect.
ShippingInfo.states = new String[50];
It fails because you cannot assign values to final variables. The following code, however, is perfectly legal:
ShippingInfo.states[5] = "Java City";
This code replaces the entry for  ("NV") with "Java City." Obviously, final arrays are not
immutable, and passing them around can violate encapsulation. No syntax in the Java programming language
provides a truly immutable array. This can lead to all kinds of inconsistencies. To avoid these problems, one
solution is to make the following changes:
1. Make the array private.
2. Add a getStates method.
3. Return a copy of the states array from the getStates method.
                                               
                                                  OR



you can use Iterator as inner class to increase the performance of the program.

Example:

public class ShippingInfo {
private static final String[] states = {
"AK", "AZ", "CA", "DE", "NV", "NY"};
public static Iterator getStates() {
return new StateIterator();
}
public static class StateIterator implements Iterator {
private int current = 0;
/* from Iterator */
public boolean hasNext() {
return current < states.length;
}
/* from Iterator */
public Object next() {
return nextState();
}
/* from Iterator */
public void remove() {
throw new UnsupportedOperationException();
}
/* custom typesafe next */
public String nextState() {
if (current < states.length) {
String state = states[current];
current++;
return state;
} else {
throw new NoSuchElementException();
}
}
}

}



The following code snippet can be used to iterate through the array safely, without concern that it could be accidentally damaged as read only.
Iterator iter = ShippingInfo.getStates();
while (iter.hasNext()) {
System.out.println(iter.next());



Simple Object Immutability

While most objects are mutable, some are not. For example, any bean that provides a setXXX method is
mutable. Immutable objects can be used to define values or attributes that you don't want to be changed.


Example:

public class MyConstant {
private double value;
public MyConstant(double value) {
this.value = value;
}
public double getValue() {
return value;
}

}

This is not the only way to make object immutable, this is one of the ways available.

Truth Behind String Literal

Most of the problems with using String stem from the fact that String objects are immutable. Once they've
been created, they cannot be changed. Operations that might appear to modify String objects actually generate completely new ones.

Then what happen with string literal ?
The String and StringBuffer classes are meant to be used together to overcome this situation.

Example:

String xyz = "x" + y + "z";
It automatically transforms the code to
String xyz = new StringBuffer().append("x")
                                                    .append(y)
                                                    .append("z")

                                                     .toString();


This gives you an idea how String concatenation actually works. Note that two objects are created to perform the transformation: A new StringBuffer is created explicitly and a new String is returned from toString.

Do interface really extends the member of Object class?

To know this, we'll have to know about the members of  interface.

Interface Members:

The members of an interface are:
• Those members declared in the interface.
• Those members inherited from direct superinterfaces.
• If an interface has no direct superinterfaces, then the interface implicitly declares
a public abstract member method m with signature s, return type r, and throws
clause t corresponding to each public instance method m with signature s, return
type r, and throws clause t declared in Object, unless a method with the same
signature, same return type, and a compatible throws clause is explicitly declared
by the interface.
It is a compile-time error if the interface explicitly declares such a method m in
the case where m is declared to be final in Object.
It follows that is a compile-time error if the interface declares a method with a
signature that is override-equivalent to a public method of Object, but
has a different return type or incompatible throws clause.

 
Copyright (c) 2013 Java Discovery.