package javacodebook.core.concat; public class Starter { public static void main(String[] args) { verbinden(); langsam(); schnell(); } public static void verbinden() { // Aneinanderreihen von Strings. Wie immer führen // viele Wege nach Rom. StringBuffer buf = new StringBuffer(); String a, b, c; // Die zu verbindenen Strings a = "Fischer Fritz"; b = "fischt frische Fische"; // Den Operator + verwenden c = a + " " + b; System.out.println(c); // Die Methode concat verwenden c = a.concat(" ").concat(b); System.out.println(c); // Einen StringBuffer verwenden buf.append(a).append(" ").append(b); System.out.println(buf.toString()); } public static void langsam() { String a = "String1"; String b = "String2"; String c = null; long start = System.currentTimeMillis(); for(int i=0; i<100000; i++) { c = a + b; } System.out.println(System.currentTimeMillis()-start); } public static void schnell() { String a = "String1"; String b = "String2"; String c = null; long start = System.currentTimeMillis(); for(int i=0; i<100000; i++) { c = a.concat(b); } System.out.println(System.currentTimeMillis()-start); } }