import java.util.Arrays;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target="_blank">Deep 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;
		}

		public void setCharacters(char[] characters) {
			this.characters = 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);
		}

		/**
		 * @see Arrays#copyOf(char[], int)
		 *
		 */
		@Override
		public Object clone() throws CloneNotSupportedException {
			Text clone = (Text) super.clone();

			// deep cloning
			clone.setCharacters(Arrays.copyOf(characters, characters.length));

			return clone;
		}
	}

	public static void main(String args[]) throws Exception {
		// Unit test - contenuto/indirizzo di una copia profonda
		try {
			Text text = new Text(new char[] { 'I', 'T', 'A', 'L', 'O' });
			System.out.println(text.getCharacters());

			Text textDeepCopy = (Text) text.clone();

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

			// oggeto generato da una copia profonda, non modifica la variabile d'istanza
			// del
			// oggetto text ( stesso tipo )
			char[] textContent = textDeepCopy.getCharacters();

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

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

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

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

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

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

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

Embed on website

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