Voodoo-Kiste: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen (Die Seite wurde neu angelegt: „Kategorie:Java == String-Magic Level 1 == Der erste Eintrag der lustigen Java-Voodookiste widmet sich ganz einfach mal der wunderbaren Kunst der Stringkonkat…“) |
K |
||
Zeile 1: | Zeile 1: | ||
[[Kategorie:Java]] | [[Kategorie:Java]] | ||
+ | [[Kategorie:Java-Codeschnipsel]] | ||
== String-Magic Level 1 == | == String-Magic Level 1 == | ||
Aktuelle Version vom 8. August 2013, 17:49 Uhr
Inhaltsverzeichnis
String-Magic Level 1
Der erste Eintrag der lustigen Java-Voodookiste widmet sich ganz einfach mal der wunderbaren Kunst der Stringkonkatenation. Ein wenig Mystery ist auch dabei, ganz bestimmt
<code=java>
System.out.println("funny concat : " + 1 + 1); // it's okay if you first think it's 2 System.out.println("stupid funny concat : " + + + + + + 1 + + + + 1); // <- yeah! wth? System.out.println("stupid funny concat : " +- + + +- + + -+-1 + + + + -+-1); // <- omg! wtf?!?!?! System.out.println("so nice :) : " + - - - - - - + 1 +- - - -1); // <- omfg!!! wtfh?!???!!! //System.out.println("never ever 1st :( : " ++ 1); // never ever, you cannot do so, never, believe it :) //System.out.println("never ever 2nd :( : " + -- 1); // absolutely no!
</code=java>
Integer-Magic Level 1
Es folgt ein mächtiger Kniff aus der Java-Voodookiste mit anschließendem Brain**** am Ende:
<code=java>
Integer i0 = 5000; Integer i1 = 5000; System.out.println(i0 == i1); // ok, we should know this is false, or not? Look at the end ;) System.out.println(i0.intValue() == i1.intValue()); // yeah, it's right mister // let's try smaller values i0 = 127; i1 = 127; System.out.println(i0.intValue() == i1.intValue()); // yeees sir! System.out.println(i0 == i1); // wth? i0 = -128; i1 = -128; System.out.println(i0 == i1); // wtfh?!?!?! // try this: give the VM the following VM-Argument -Djava.lang.Integer.IntegerCache.high=5001 // and things becomes very scary ;)
</code=java>
Byte-Magic Level 1
Zur Abwechslung der Grundkurs Byte-Magic für Level 1 Wizards
<code=java>
byte b = 1; // ok, it's nice too see //byte b = 128; // we know that's not compiling ;) //b = b + 127; // hey, we know this too :) it's absolutely safe, ever! b += 127; // but wth? b ++; // omg!!! b ++; // wtf?!?!? for(int i = 0; i < 169; i ++) { b ++; // yeah! } b --; System.out.println(b); // the solution of the biggest question ever! short s = 1; s = Short.MAX_VALUE; //s = s + 1; // yeah! it's not compiling mister ;) s += 1; // ok, we like bytes, we like you too! // and so on ... you already know the solution :) int i = 1; i = Integer.MAX_VALUE; i = i + 1; // don't be panic OO i = i + 10; // we like byte and short more than other numeric types ;) System.out.println(i); // it's something crazy... char c = 1; c = 65535; // yeah, you know the c64? //c = c + 1; // ok char, we like you too ;) don't compile this :) c ++; c ++; System.out.println((char) (c - Short.MAX_VALUE)); // because you're funny as bytes and shorts :)
</code=java>
Write-through-Magic Level 1
Es folgt ein kleiner Ausflug in die Kunst des Durchschreibenden-Voodoozaubers von Ank-Pheng.
<code=java>
// lets create a simple list List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); String[] array = list.toArray(new String[0]); // create an array from it System.out.println("1st list : " + list); // ok, i know i know! System.out.println("1st array-size: " + array.length); // yeah, i expected this ;) System.out.println("set 'a' to 'z' in list ..."); // ok chummer! list.set(0, "z"); System.out.println("1st list : " + list); // where's the trick here? System.out.println("1st array[0] : " + array[0]); // no trick! what you think? We don't summon demons or cast spells here, ever! ever ever! // let's create a simple array array = new String[]{"a", "b", "c"}; list = Arrays.asList(array); // create a list from it System.out.println("2nd list : " + list); // ok, i already know it! System.out.println("2nd array size: " + array.length); // yessss! what you expect here? System.out.println("set first element to 'z' in array ..."); // try it baby! try! array[0] = "z"; System.out.println("2nd array[0] : " + array[0]); // yeah! i know!!! System.out.println("2nd list : " + list); // omg!!! wtfh?!?!?! i can't believe... // believe the power of write-through magic ;)
</code=java>