Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public class Porownanie {
  2.         public static void main(String[] args) {
  3.                 String s1 = "foo"; // s1 to referencja do nowego obiektu String - "foo"
  4.                 String s2 = "foo"; // s2 to taka sama referencja
  5.                                 //(optymalizacja Javy - juz istnieje taki obiekt w pamieci)
  6.                 System.out.println(s1 == s2); // porownanie fizyczne (obiektow w pamieci)
  7.                                                 // zwraca true
  8.                 System.out.println(s1.equals(s2)); // porownanie strukturalne (zawartosci obiektow)
  9.                                                 // zwraca true
  10.                 String s3 = new String("foo"); // tworzymy odrebny obiekt
  11.                                                 //(ale o takiej samej zawartosci)
  12.                 System.out.println(s1 == s3); // fizyczne, false
  13.                 System.out.println(s1.equals(s3)); // strukturalne, true
  14.         }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement