import java.util.Arrays;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target="_blank">Shallow copy</a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {
        private static final class Text implements Cloneable {
        private char[] characters;
    
        public Text(char[] characters) {
            this.characters = characters;
        }
    
        public char[] getCharacters() {
            return characters;
        }
    
        /**
         * @see Arrays#hashCode(char[])
         *
         */
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + Arrays.hashCode(characters);
            return result;
        }
    
        /**
         * @see Arrays#equals(char[], char[])
         *
         */
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Text)) {
                return false;
            }
            Text other = (Text) obj;
            return Arrays.equals(characters, other.characters);
        }
    
        /**
         * @see String#format(String, Object...)
         * @see Arrays#copyOf(char[], int)
         * 
         * {@link Math#min(double, double)}
         *
         */
        @Override
        public String toString() {
            final int maxLen = 10;
            return String.format("Text [characters=%s]",
                    characters != null ? Arrays.toString(Arrays.copyOf(characters, Math.min(characters.length, maxLen)))
                            : null);
        }
    
        public Text shallowCopy() throws CloneNotSupportedException {
            return (Text) clone();
        }
    
        /**
         * @see Cloneable
         * 
         * @throws CloneNotSupportedException
         *
         */
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }

	public static void main(String args[]) throws Exception {
		// Unit test - shallow copy

		Text text = new Text(new char[] { 'I', 'T', 'A', 'L', 'O' });
		System.out.println(text.getCharacters());

		Text textShallow = (Text) text.shallowCopy();

		// l'oggeto shallow modifica la variabile d'istanza dell'oggetto text ( stesso tipo )
		char[] textContent = textShallow.getCharacters();

		textContent[0] = 'B';
		textContent[1] = 'I';
		textContent[2] = 'K';
		textContent[3] = 'E';
		textContent[4] = '*';

		System.out.println(text.getCharacters());

		// Unit test - verifica il contenuto e l'indirizzo di una shallow copy e del suo oggetto d'origine

		if (text.equals(textShallow))
			System.out.println("text (contenuto) == textShallow (contenuto)");
		else
			System.out.println("text (contenuto) != textShallow (contenuto)");

		if (text == textShallow)
			System.out.println("text (indirizzo) == textShallow (indirizzo)");
		else
			System.out.println("text (indirizzo) != textShallow (indirizzo)");

		if (text.getCharacters() == textShallow.getCharacters())
			System.out.println("text.characters (indirizzo) == textShallow.characters (indirizzo)");
		else
			System.out.println("text.characters (indirizzo) != textShallow.characters (indirizzo)");
	}
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: