Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Porownanie {
- public static void main(String[] args) {
- String s1 = "foo"; // s1 to referencja do nowego obiektu String - "foo"
- String s2 = "foo"; // s2 to taka sama referencja
- //(optymalizacja Javy - juz istnieje taki obiekt w pamieci)
- System.out.println(s1 == s2); // porownanie fizyczne (obiektow w pamieci)
- // zwraca true
- System.out.println(s1.equals(s2)); // porownanie strukturalne (zawartosci obiektow)
- // zwraca true
- String s3 = new String("foo"); // tworzymy odrebny obiekt
- //(ale o takiej samej zawartosci)
- System.out.println(s1 == s3); // fizyczne, false
- System.out.println(s1.equals(s3)); // strukturalne, true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement