Commit 086e1f9b by Patryk Czarnik

Mutowalnosc

parent 82a1abea
package p02_obiekty;
import java.util.concurrent.atomic.AtomicInteger;
public class Mutowalnosc1 {
static void metoda1(int a, Integer b, AtomicInteger c) {
System.out.println("Początek metody");
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
a += 1;
b += 2;
// technicznie: b = Integer.valueOf(b.intValue() + 2);
// żeby zrozumieć: b = new Integer(b.intValue() + 2);
// w dalszej części metody w b jest dowiązanie do innego obiektu, niż obiekt widziany z maina
// AtomicInteger to "mutowalna" wersja Integer
c.incrementAndGet();
c.addAndGet(2);
System.out.println("\nKoniec metody");
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
}
static void main() {
int a = 100;
Integer b = 200;
AtomicInteger c = new AtomicInteger(300);
metoda1(a, b, c);
System.out.println("\nKoniec main");
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
package p02_obiekty;
public class Mutowalnosc2 {
static void metoda1(String a, StringBuilder b) {
System.out.println("Początek metody");
System.out.println("a: " + a);
System.out.println("b: " + b);
a += " ma kota";
// technicznie: a = new String(a + " ma kota");
b.append(" ma psa");
System.out.println("\nKoniec metody");
System.out.println("a: " + a);
System.out.println("b: " + b);
}
static void main() {
String a = "Ala";
StringBuilder b = new StringBuilder("Ola");
metoda1(a, b);
System.out.println("\nKoniec main");
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment