/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target="_blank">Verifica l&apos;indirizzo in memoria e il contenuto di due strighe</a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {

    /**
	 * @see String#substring(int beginIndex, int endIndex)
	 *
	 */
	public static void subText() {
		String text = new String("ITAitp [ Italy Italo Persechino ]");
		String text_ = text.substring(0, 6);

		print(text);
		print(text_);
		chkMemoryAddress(text, text_);
		chkTextContent(text, text_);
	}

	public static void comparisonBetween() {
		String text = new String("ITAitp [ Italy Italo Persechino ]");
		String text_ = new String("ITAitp [ Italy Italo Persechino ]");

		print(text);
		print(text_);
		chkMemoryAddress(text, text_);
		chkTextContent(text, text_);
	}

	public static void comparisonOnAssigment() {
		String text;
		String text_ = new String("ITAitp [ Italy Italo Persechino ]");

		// assegnazione ( solo per lo stesso tipo di data-type )
		text = text_;

		print(text);
		print(text_);
		chkMemoryAddress(text, text_);
		chkTextContent(text, text_);
	}

	/**
	 * @see String#hashCode()
	 *
	 */
	private static void print(String text) {
		System.out.println("testo    -> " + text);
		System.out.println("hashCode -> " + text.hashCode());
	}

	private static void chkMemoryAddress(String text, String text_) {
		if (text != text_)
			System.out.println(text + " ha un indirizzo in memoria diverso da " + text_);
		else
			System.out.println(text + " ha un indirizzo in memoria uguale a " + text_);
	}

	private static void chkTextContent(String text, String text_) {
		if (text.equals(text_))
			System.out.println(text + " ha contenuto uguale a " + text_);
		else
			System.out.println(text + " ha contenuto diverso da " + text_);
	}

    public static void main(String[] args) {
		// Unit test - verifica l'indirizzo in memoria e il contenuto di una stringa
		// e di una nuova generata dalla prima
		subText();

        System.out.println();

		// Unit test - verifica l'indirizzo in memoria e il contenuto di due stringhe
		// indipendenti, ma con lo stesso contenuto
		comparisonBetween();

         System.out.println();

		// Unit test - verifica l'indirizzo in memoria e il contenuto di una stringa e
		// di un nuovo identificatore a cui viene assegnata
		comparisonOnAssigment();
    }
}

Embed on website

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