SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
Effective Java Puzzlers


             JSUG 9th Meeting
             Vienna, 12.01.2009
             Christoph Pickl




Monday, January 12, 2009               1
Agenda

        i. The books
                 Effective Java & Java Puzzlers

        ii. It’s your turn
                 Solve some puzzles at your own

        iii. Solve’em together
                 Provide solutions and background infos

        iv. Good advice
                 Some nuggets to be more effective

        v. Summary

Monday, January 12, 2009                                   2
The Books




Monday, January 12, 2009               3
Effective Java


        by         Joshua Bloch
              designed/implemented many
          
              Java platform libraries
        57 items on 252 pages
        program from an API
         designer’s point of view




                           amazon.com
Monday, January 12, 2009                  4
Java Puzzlers


        by Joshua Bloch and
         Neal Gafter
        95 puzzles on 282 pages
        Covers different topics
                    Expressions, Strings, Loops,
               
                    Exceptions, Classes, Threads,
                    Java Library, Serialization




                           amazon.com
Monday, January 12, 2009                            5
Puzzle Alone




Monday, January 12, 2009                  6
Puzzle alone
         hand out questionnaires and pencils
                 18 puzzles
         45 minutes time (max!)
         providing name is optionally
                 results will be evaluated
                 best result will be placed in “hall of fame”
         most of them are multiple choice
                 make use of “Other...” option
         no talking, no cheating, no use of internet :)


Monday, January 12, 2009                                         7
Puzzle Together




Monday, January 12, 2009                     8
#1 Simple Subtraction
        public class SimpleSubtraction {
          public static void main(String[] args) {
            System.out.println(2.00 - 1.10);
          }
        }

       // solution #1: poor - still uses binary floating-point!
       System.out.printf(“%.2f%n”, 2.00 - 1.10);

       // solution #2: use integral types
       System.out.println((200 - 110) + “ cents”);

       // solution #3: use BigDecimal(String)
       System.out.println(new BigDecimal(“2.00”).
                              subtract(new BigDecimal(“1.10”)));

                           avoid float and double where exact answers are required;
                            for monetary calculations, use int, long or BigDecimal



Monday, January 12, 2009                                                              9
2# Simple Addition
        public class SimpleAddition {
          public static void main(String[] args) {
            System.out.println(12345 + 5432l);
          }
        }


       List<String> l = new ArrayList<String>();
       l.add(quot;Fooquot;);
       System.out.println(1);



       System.out.println(12345 + 5432L);




                           always use a capitel L in long literals, never a lowercase l




Monday, January 12, 2009                                                                  10
#3 Simple Division
        public class SimpleDivision {
          public static void main(String[] args) {
            final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
            final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
            System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
          }
        }

       // computation of constant overflows!
       long MICROS_PER_DAY = ((int) (24 * 60 * 60 * 1000 * 1000));
       // afterwards widening primitive conversion [JLS 5.1.2]

       final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;
       final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000;
       System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);


                              when working with large numbers,
                           watch out for overflow - it’s a silent killer



Monday, January 12, 2009                                                   11
#4 Compound Legal
        ________ x         = ________;
        ________ i         = ________;
        x += i;            // first statement legal
        x = x + i;         // second statement illegal


       short x = 0;
       int i = 123456;
       x += i;    // narrowing primitive conversion [JLS 5.1.3]
       x = x + i; // won’t compile: “possible loss of precision”


       // [JLS 15.26.2] says about compound assignment operator:
       // E1 op= E2 <==> E1 = (T) ((E1) op (E2))



                               do not use compound assignment operators
                                on variables of type byte, short or char



Monday, January 12, 2009                                                   12
#5 Compound Illegal
        ________ x         = ________;
        ________ i         = ________;
        x += i;            // first statement illegal
        x = x + i;         // second statement legal




       Object x =          “object string ”;
       String i =          “real string”;
       x += i;             // left-hand side object reference type != String
       x = x + i;          // is assignment compatible [JLS 5.2]
                           // string concatenation is performed [JLS 15.26.2]




Monday, January 12, 2009                                                        13
#6 Unicode Escapes
        public class UnicodeEscapes {
          public static void main(String[] args) {
            // u0022 is the unicode escape for double quote (quot;)
            System.out.println(quot;au0022.length() + u0022bquot;.length());
          }
        }
       public class LinePrinter {
         public static void printLine() {
           // Note: u000A is Unicode representation of linefeed (LF)
           char c = 0x000A;
           System.out.print(c);
         }
       }



                             do not use Unicode escapes to represent ASCII characters;
                           avoid Unicode escapes except where they are truly necessary



Monday, January 12, 2009                                                                 14
#7 Classify Characters
        public class Classifier {
           public static void main(String[] args) {
             System.out.println(classify('n') +
                                  classify('+') + classify('2'));
           }
           public static String classify(char c) {
             if(quot;0123456789quot;.indexOf(c) >= 0)
               return quot;NUMERAL quot;;
             if(quot;abcdefghijklmnopqrstuvwxyzquot;.indexOf(c) >= 0)
               return quot;LETTER quot;;
        // TODO finish implementation of operator classification
        // if(quot;+-*/&|!=quot;.indexOf(c) >= 0)
        //     return quot;OPERATOR quot;;
        //
             return quot;UNKOWN quot;;
           }
                       comment out a section of code by make use of
        }
                           a sequence of single-line comments



Monday, January 12, 2009                                              15
#8 Count Loops
        public class InTheLoop {
          public static final int END = Integer.MAX_VALUE;
          public static final int START = END - 100;
          public static void main(String[] args) {
            int count = 0;
            for (int i = START; i <= END; i++)
              count++;
            System.out.println(count);
          }
        }


       for (long i = START; i <= END; i++)
         count++;



                     whenever using an integral type, be aware of the boundary conditions;
                            and again: watch out for overflow - it’s a silent killer



Monday, January 12, 2009                                                                     16
#9 Never Ending Story
        ___ start = _____________________;
        int         Integer.MAX_VALUE - 1
        for (int i = start; i <= start + 1; i++) { }

       ______ i = ________________________; // see [IEEE-754]
       double     Double.POSITIVE_INFINITY
       while (i == i + 1) { }

        double
        ______ i = __________; // see [JLS 15.21.1]
                   Double.NaN
        while (i != i) { }

        String i = “foobar” // see [JLS 15.18.1]
        ______     ________;
        while(i != i + 0) { }
        Integer     new Integer(0)
        _______ i = ______________;
        Integer     new Integer(0)
        _______ j = ______________;
        while(i <= j && j <= i && i != j) { }

                  binary floating-point arithmetic is only an approximation to real arithmetic;
                                operator overloading can be very misleading



Monday, January 12, 2009                                                                          17
#10 Overloaded Constructors
        public class Confusing {
          public Confusing(Object o) {
            System.out.println(quot;Objectquot;);
          }
          public Confusing(double[] d) {
            System.out.println(quot;double arrayquot;);
          }
          public static void main(String[] args) {
            new Confusing(null);
          }
        }
       // overloading process operates in two phases [JLS 15.12.2.5]

       new Confusing((Object) null);


                           avoid overloading; use different names for different methods
                       (not possible for constructors, therefore use static factory methods)



Monday, January 12, 2009                                                                       18
#11 Which Instance
        public class Type1 {
          public static void main(String[] args) {
            String s = null;
            System.out.println(s instanceof String);
          }
        }

        public class Type2 {
          public static void main(String[] args) {
            System.out.println(new Type2() instanceof String);
          } // compile time error!!! [JLS 15.20.2, 15.16, 5.5]
        }

        public class Type3 {
          public static void main(String[] args) {
            Type3 t = (Type3) new Object();
          } // runtime exception!!!
        }


Monday, January 12, 2009                                         19
#12 What’s the Point?
        class Point {                 class Point2 extends Point {
          final int x, y;               final String c;
          final String name;            Point2(int x,int y,String C) {
                                                      2
          Point (int X, int Y) {          super(x, y);
                                                5
            x=X;y=Y;                      c = C;
                               3
            name = makeN();             }
          }                             String makeN() {
                                                                     4
          String makeN() {                return super.makeN()+quot;:quot;+c;
            return quot;[quot;+x+quot;,quot;+y+quot;]quot;;     }
          }                             public static void main (..) {
          final String toString() {       System.out.println(
                           6                                             1
            return name;                    new Point2(4,2,quot;purplequot;));
          }                             } // prints quot;[4,2]:purplequot;
                                          // prints quot;[4,2]:nullquot;
        }                             }




Monday, January 12, 2009                                                     20
#12 What’s the Point?
        class Point {                             class Point2 extends Point {
         final int x, y;                             final String c;
         final String name;                          Point2(int x,int y,String C) {
         Point (int X, int Y) {                         super(x, y);
           x=X;y=Y;                                     c = C;
           // lazy initializing                      }
         }                                           String makeN() {
         String makeN() {                               return super.makeN()+quot;:quot;+c;
           return quot;[quot;+x+quot;,quot;+y+quot;]quot;;                   }
         }                                           public static void main (..) {
         String toString() {                            System.out.println(
           if(name == null) {                              new Point2(4,2,quot;purplequot;));
             name = makeN();                         }
           }                                      }
           return name;
         }
        } it’s possible observing final instance field before its value has been assigned;
                         never call overridable methods from constructors



Monday, January 12, 2009                                                                     21
#13 Null and Void
        public class Null {
          public static void greet() {
            System.out.println(quot;Hello world!quot;);
          }
          public static void main(String[] args) {
            System.out.println(((Null) null).greet());
          }
        }



       System.out.println(Null.greet();




                           invoke static methods in a static way



Monday, January 12, 2009                                           22
#14 Name It
        public class Name {
          private final String first, last;
          public Name(String first, String last) {
            this.first = first; this.last = last;
          }
          public boolean equals(Object o) {
            if(!(o instanceof Name)) return false;
            Name n = (Name) o;
            return n.first.equals(first) && n.last.equals(last);
          }

             public static void main(String[] args) {
               Set<Name> set = new HashSet<Name>();
               set.add(new Name(quot;Sparquot;, quot;Datquot;));
               System.out.println(set.contains(new Name(quot;Sparquot;, quot;Datquot;)));
             }
        }




Monday, January 12, 2009                                                    23
#14 Name It
        public class Name {
          private final String first, last;
          public Name(String first, String last) {
            this.first = first; this.last = last;
          }
          public boolean equals(Object o) {
            if(!(o instanceof Name)) return false;
            Name n = (Name) o;
            return n.first.equals(first) && n.last.equals(last);
          }
          public int hashCode() {
            return 37 * first.hashCode() + last.hashCode();
          }
        }



                           you MUST override hashCode whenever you override equals




Monday, January 12, 2009                                                             24
#15 Shades of Gray
        public class ShadesOfGray {
          public static void main(String[] args) {
            System.out.println(X.Y.Z);
          }
        }
        class X {
          static class Y {
            static String Z = quot;Blackquot;;
          }
          static C Y = new C();
        }
        class C {
          static String Z = quot;Whitequot;;
        }


       // when a variable and a type have the same name and
       // both are in scope, the variable takes precedence [JLS 6.5.2]



Monday, January 12, 2009                                                 25
#15 Shades of Gray
        public class ShadesOfGray {
          public static void main(String[] args) {
            System.out.println(Ex.Why.z);
          }
        }
        class Ex {
          static class Why {
            static String z = quot;Blackquot;;
          }
          static See y = new See();
        }
        class See {
          String z = quot;Whitequot;;
        }



                           always obey the standard Java naming conventions




Monday, January 12, 2009                                                      26
A Glossary of Name Reuse
         Overriding
                            method overrides other superclass’ instance methods with the
                             same signature (enabling dynamic dispatch)

         Hiding
                            field/static method/member type hides other with same name
                             (signature) of supertypes

         Overloading
                            method with the same name but with another signature

         Shadowing
                            variable/method/type shadows other with same name&scope

         Obscuring
                            variable obscures a type with the same name


Monday, January 12, 2009                                                                    27
#16 Reflection Infection
       public class Reflector {
         public static void main(String[] args) {
           Set<String> set = new HashSet<String>();
           set.add(quot;fooquot;);
           Iterator it = set.iterator();
           Method m = it.getClass().getMethod(quot;hasNextquot;);
           System.out.println(m.invoke(it));
         }
       }
       Exception in thread quot;mainquot; IllegalAccessException:
         Class Reflector can not access a member of a class HashMap
         $HashIterator with modifiers quot;publicquot;
       // you cannot legally access a member of
       // a nonpublic type from another package [JLS 6.6.1]
       Method m = Iterator.class.getMethod(quot;hasNextquot;);

                                    when accessing a type reflectively,
                           use a Class object that represents an accessible type



Monday, January 12, 2009                                                           28
#17 Lazy Initialization


     Class initialization [JLS 12.4.2]

       The class is not yet initialized.
     
      The class is being initialized by
       the current thread: a recursive
       request for initialization.
      The class is being initialized by
       some thread other than the
       current thread.
      The class is already initialized




Monday, January 12, 2009                   29
#17 Lazy Initialization
        public class Lazy {
          private static boolean initialized = false;
          static {
            Thread thread = new Thread(new Runnable() {
              public void run() {
                initialized = true;
              }});
            thread.start();
            try {
              thread.join();
            } catch(InterruptedException e) {
              throw new AssertionError(e);
            }
          }
          public static void main(String[] args) {
            System.out.println(initialized);
          }
        }



Monday, January 12, 2009                                  30
#18 Class Warfare
        at.spardat.puzzler.client;
        public class PrintWords {
          public static void main(String[] args) {
            System.out.println(
                Words.FIRST + quot; quot; + Words.SECOND + quot; quot; + Words.THIRD);
          }
        }
        at.spardat.puzzler.library;
        public class Words {
          private Words() { }
          public static final String FIRST = quot;thequot;;
          public static final String SECOND = null;
          public static final String THIRD = quot;setquot;;
        }


                           API designers should think long and hard
                               before exporting a constant field



Monday, January 12, 2009                                                 31
Effective Nuggets




Monday, January 12, 2009                       32
Effective Java
         always override toString
         static factory methods instead constructors
         favor immutability
         favor composition over inheritance
         prefer interfaces to abstract classes
         use overloading rarely
         string concatenation’s performance
         favor static over nonstatic member classes
         minimize accessibility

Monday, January 12, 2009                                33
Summary
         binary floating-point arithmetic is inexact
         be aware of silent overflows
         obey general naming conventions
         overriding equals => overriding hashCode

         carefully read API documentation

                           if you are not shure what a piece of code does,
                           it’s very likely that it doesn’t do what you want it to do


Monday, January 12, 2009                                                                34
Hardcore Java


        by Robert Simmons
        344 pages full of
         hardcore stuff
        Covers:
                    Final (!), Immutable Types,
               
                    Collections, Exceptions,
                    Constants, Nested Classes,
                    Reflection, References




Monday, January 12, 2009                          35
!at’s it


Monday, January 12, 2009          36

Weitere ähnliche Inhalte

Was ist angesagt?

Swift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationSwift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationJacopo Mangiavacchi
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classesIntro C# Book
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handlingIntro C# Book
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 

Was ist angesagt? (20)

Swift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationSwift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML Personalization
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
OOP v3
OOP v3OOP v3
OOP v3
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
Jeop game-final-review
Jeop game-final-reviewJeop game-final-review
Jeop game-final-review
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 

Andere mochten auch

2) java development
2) java development2) java development
2) java developmenttechbed
 
3) web development
3) web development3) web development
3) web developmenttechbed
 
819 Static Channel Allocation
819 Static Channel Allocation819 Static Channel Allocation
819 Static Channel Allocationtechbed
 
Photoshop tut
Photoshop tutPhotoshop tut
Photoshop tuttechbed
 
Part 3 web development
Part 3 web developmentPart 3 web development
Part 3 web developmenttechbed
 
Lab 5a) create a struts application
Lab 5a) create a struts applicationLab 5a) create a struts application
Lab 5a) create a struts applicationtechbed
 
Uses Of Wifi
Uses Of WifiUses Of Wifi
Uses Of Wifitechbed
 

Andere mochten auch (7)

2) java development
2) java development2) java development
2) java development
 
3) web development
3) web development3) web development
3) web development
 
819 Static Channel Allocation
819 Static Channel Allocation819 Static Channel Allocation
819 Static Channel Allocation
 
Photoshop tut
Photoshop tutPhotoshop tut
Photoshop tut
 
Part 3 web development
Part 3 web developmentPart 3 web development
Part 3 web development
 
Lab 5a) create a struts application
Lab 5a) create a struts applicationLab 5a) create a struts application
Lab 5a) create a struts application
 
Uses Of Wifi
Uses Of WifiUses Of Wifi
Uses Of Wifi
 

Ähnlich wie JSUG - Effective Java Puzzlers by Christoph Pickl

Ähnlich wie JSUG - Effective Java Puzzlers by Christoph Pickl (20)

Kotlin
KotlinKotlin
Kotlin
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Java
JavaJava
Java
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Decompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 PresentationDecompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 Presentation
 
C#, What Is Next?
C#, What Is Next?C#, What Is Next?
C#, What Is Next?
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 
Deuce STM - CMP'09
Deuce STM - CMP'09Deuce STM - CMP'09
Deuce STM - CMP'09
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
3.OPERATORS_MB.ppt .
3.OPERATORS_MB.ppt                      .3.OPERATORS_MB.ppt                      .
3.OPERATORS_MB.ppt .
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 

Mehr von Christoph Pickl

JSUG - AS3 vs Java by Christoph Pickl
JSUG - AS3 vs Java by Christoph PicklJSUG - AS3 vs Java by Christoph Pickl
JSUG - AS3 vs Java by Christoph PicklChristoph Pickl
 
JSUG - Layouting TeX documents with the Memoir class
JSUG - Layouting TeX documents with the Memoir classJSUG - Layouting TeX documents with the Memoir class
JSUG - Layouting TeX documents with the Memoir classChristoph Pickl
 
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven Dolg
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven DolgJSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven Dolg
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven DolgChristoph Pickl
 
JSUG - ActionScript 3 vs Java by Christoph Pickl
JSUG - ActionScript 3 vs Java by Christoph PicklJSUG - ActionScript 3 vs Java by Christoph Pickl
JSUG - ActionScript 3 vs Java by Christoph PicklChristoph Pickl
 
JSUG - TeX, LaTeX und der Rest by Norbert Preining
JSUG - TeX, LaTeX und der Rest by Norbert PreiningJSUG - TeX, LaTeX und der Rest by Norbert Preining
JSUG - TeX, LaTeX und der Rest by Norbert PreiningChristoph Pickl
 
JSUG - TeX Day by Christoph Pickl
JSUG - TeX Day by Christoph PicklJSUG - TeX Day by Christoph Pickl
JSUG - TeX Day by Christoph PicklChristoph Pickl
 
JSUG - The Sound of Shopping by Christoph Pickl
JSUG - The Sound of Shopping by Christoph PicklJSUG - The Sound of Shopping by Christoph Pickl
JSUG - The Sound of Shopping by Christoph PicklChristoph Pickl
 
JSUG - Tim aka EPROG2 by Martin Schuerrer
JSUG - Tim aka EPROG2 by Martin SchuerrerJSUG - Tim aka EPROG2 by Martin Schuerrer
JSUG - Tim aka EPROG2 by Martin SchuerrerChristoph Pickl
 
JSUG - Java Service Enabler by Andreas Hubmer
JSUG - Java Service Enabler by Andreas HubmerJSUG - Java Service Enabler by Andreas Hubmer
JSUG - Java Service Enabler by Andreas HubmerChristoph Pickl
 
JSUG - Hoppla by Florian Motlik and Petar Petrov
JSUG - Hoppla by Florian Motlik and Petar PetrovJSUG - Hoppla by Florian Motlik and Petar Petrov
JSUG - Hoppla by Florian Motlik and Petar PetrovChristoph Pickl
 
JSUG - Google Web Toolkit by Hans Sowa
JSUG - Google Web Toolkit by Hans SowaJSUG - Google Web Toolkit by Hans Sowa
JSUG - Google Web Toolkit by Hans SowaChristoph Pickl
 
JSUG - TU Wien Cocoon Project by Andreas Pieber
JSUG - TU Wien Cocoon Project by Andreas PieberJSUG - TU Wien Cocoon Project by Andreas Pieber
JSUG - TU Wien Cocoon Project by Andreas PieberChristoph Pickl
 
JSUG - TU Wien Castor Project by Lukas Lang
JSUG - TU Wien Castor Project by Lukas LangJSUG - TU Wien Castor Project by Lukas Lang
JSUG - TU Wien Castor Project by Lukas LangChristoph Pickl
 
JSUG - LaTeX Introduction by Christoph Pickl
JSUG - LaTeX Introduction by Christoph PicklJSUG - LaTeX Introduction by Christoph Pickl
JSUG - LaTeX Introduction by Christoph PicklChristoph Pickl
 
JSUG - OSGi by Michael Greifeneder
JSUG - OSGi by Michael GreifenederJSUG - OSGi by Michael Greifeneder
JSUG - OSGi by Michael GreifenederChristoph Pickl
 
JSUG - Filthy Flex by Christoph Pickl
JSUG - Filthy Flex by Christoph PicklJSUG - Filthy Flex by Christoph Pickl
JSUG - Filthy Flex by Christoph PicklChristoph Pickl
 
JSUG - Seam by Florian Motlik
JSUG - Seam by Florian MotlikJSUG - Seam by Florian Motlik
JSUG - Seam by Florian MotlikChristoph Pickl
 
JSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan ZarnikovJSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan ZarnikovChristoph Pickl
 
JSUG - Java FX by Christoph Pickl
JSUG - Java FX by Christoph PicklJSUG - Java FX by Christoph Pickl
JSUG - Java FX by Christoph PicklChristoph Pickl
 
JSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklJSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklChristoph Pickl
 

Mehr von Christoph Pickl (20)

JSUG - AS3 vs Java by Christoph Pickl
JSUG - AS3 vs Java by Christoph PicklJSUG - AS3 vs Java by Christoph Pickl
JSUG - AS3 vs Java by Christoph Pickl
 
JSUG - Layouting TeX documents with the Memoir class
JSUG - Layouting TeX documents with the Memoir classJSUG - Layouting TeX documents with the Memoir class
JSUG - Layouting TeX documents with the Memoir class
 
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven Dolg
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven DolgJSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven Dolg
JSUG - Cocoon3 Student Project Idea by Reinhard Poetz and Steven Dolg
 
JSUG - ActionScript 3 vs Java by Christoph Pickl
JSUG - ActionScript 3 vs Java by Christoph PicklJSUG - ActionScript 3 vs Java by Christoph Pickl
JSUG - ActionScript 3 vs Java by Christoph Pickl
 
JSUG - TeX, LaTeX und der Rest by Norbert Preining
JSUG - TeX, LaTeX und der Rest by Norbert PreiningJSUG - TeX, LaTeX und der Rest by Norbert Preining
JSUG - TeX, LaTeX und der Rest by Norbert Preining
 
JSUG - TeX Day by Christoph Pickl
JSUG - TeX Day by Christoph PicklJSUG - TeX Day by Christoph Pickl
JSUG - TeX Day by Christoph Pickl
 
JSUG - The Sound of Shopping by Christoph Pickl
JSUG - The Sound of Shopping by Christoph PicklJSUG - The Sound of Shopping by Christoph Pickl
JSUG - The Sound of Shopping by Christoph Pickl
 
JSUG - Tim aka EPROG2 by Martin Schuerrer
JSUG - Tim aka EPROG2 by Martin SchuerrerJSUG - Tim aka EPROG2 by Martin Schuerrer
JSUG - Tim aka EPROG2 by Martin Schuerrer
 
JSUG - Java Service Enabler by Andreas Hubmer
JSUG - Java Service Enabler by Andreas HubmerJSUG - Java Service Enabler by Andreas Hubmer
JSUG - Java Service Enabler by Andreas Hubmer
 
JSUG - Hoppla by Florian Motlik and Petar Petrov
JSUG - Hoppla by Florian Motlik and Petar PetrovJSUG - Hoppla by Florian Motlik and Petar Petrov
JSUG - Hoppla by Florian Motlik and Petar Petrov
 
JSUG - Google Web Toolkit by Hans Sowa
JSUG - Google Web Toolkit by Hans SowaJSUG - Google Web Toolkit by Hans Sowa
JSUG - Google Web Toolkit by Hans Sowa
 
JSUG - TU Wien Cocoon Project by Andreas Pieber
JSUG - TU Wien Cocoon Project by Andreas PieberJSUG - TU Wien Cocoon Project by Andreas Pieber
JSUG - TU Wien Cocoon Project by Andreas Pieber
 
JSUG - TU Wien Castor Project by Lukas Lang
JSUG - TU Wien Castor Project by Lukas LangJSUG - TU Wien Castor Project by Lukas Lang
JSUG - TU Wien Castor Project by Lukas Lang
 
JSUG - LaTeX Introduction by Christoph Pickl
JSUG - LaTeX Introduction by Christoph PicklJSUG - LaTeX Introduction by Christoph Pickl
JSUG - LaTeX Introduction by Christoph Pickl
 
JSUG - OSGi by Michael Greifeneder
JSUG - OSGi by Michael GreifenederJSUG - OSGi by Michael Greifeneder
JSUG - OSGi by Michael Greifeneder
 
JSUG - Filthy Flex by Christoph Pickl
JSUG - Filthy Flex by Christoph PicklJSUG - Filthy Flex by Christoph Pickl
JSUG - Filthy Flex by Christoph Pickl
 
JSUG - Seam by Florian Motlik
JSUG - Seam by Florian MotlikJSUG - Seam by Florian Motlik
JSUG - Seam by Florian Motlik
 
JSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan ZarnikovJSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan Zarnikov
 
JSUG - Java FX by Christoph Pickl
JSUG - Java FX by Christoph PicklJSUG - Java FX by Christoph Pickl
JSUG - Java FX by Christoph Pickl
 
JSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph PicklJSUG - Tech Tips1 by Christoph Pickl
JSUG - Tech Tips1 by Christoph Pickl
 

Kürzlich hochgeladen

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Kürzlich hochgeladen (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

JSUG - Effective Java Puzzlers by Christoph Pickl

  • 1. Effective Java Puzzlers JSUG 9th Meeting Vienna, 12.01.2009 Christoph Pickl Monday, January 12, 2009 1
  • 2. Agenda i. The books  Effective Java & Java Puzzlers ii. It’s your turn  Solve some puzzles at your own iii. Solve’em together  Provide solutions and background infos iv. Good advice  Some nuggets to be more effective v. Summary Monday, January 12, 2009 2
  • 4. Effective Java  by Joshua Bloch designed/implemented many  Java platform libraries  57 items on 252 pages  program from an API designer’s point of view amazon.com Monday, January 12, 2009 4
  • 5. Java Puzzlers  by Joshua Bloch and Neal Gafter  95 puzzles on 282 pages  Covers different topics Expressions, Strings, Loops,  Exceptions, Classes, Threads, Java Library, Serialization amazon.com Monday, January 12, 2009 5
  • 7. Puzzle alone  hand out questionnaires and pencils  18 puzzles  45 minutes time (max!)  providing name is optionally  results will be evaluated  best result will be placed in “hall of fame”  most of them are multiple choice  make use of “Other...” option  no talking, no cheating, no use of internet :) Monday, January 12, 2009 7
  • 9. #1 Simple Subtraction public class SimpleSubtraction { public static void main(String[] args) { System.out.println(2.00 - 1.10); } } // solution #1: poor - still uses binary floating-point! System.out.printf(“%.2f%n”, 2.00 - 1.10); // solution #2: use integral types System.out.println((200 - 110) + “ cents”); // solution #3: use BigDecimal(String) System.out.println(new BigDecimal(“2.00”). subtract(new BigDecimal(“1.10”))); avoid float and double where exact answers are required; for monetary calculations, use int, long or BigDecimal Monday, January 12, 2009 9
  • 10. 2# Simple Addition public class SimpleAddition { public static void main(String[] args) { System.out.println(12345 + 5432l); } } List<String> l = new ArrayList<String>(); l.add(quot;Fooquot;); System.out.println(1); System.out.println(12345 + 5432L); always use a capitel L in long literals, never a lowercase l Monday, January 12, 2009 10
  • 11. #3 Simple Division public class SimpleDivision { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } } // computation of constant overflows! long MICROS_PER_DAY = ((int) (24 * 60 * 60 * 1000 * 1000)); // afterwards widening primitive conversion [JLS 5.1.2] final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); when working with large numbers, watch out for overflow - it’s a silent killer Monday, January 12, 2009 11
  • 12. #4 Compound Legal ________ x = ________; ________ i = ________; x += i; // first statement legal x = x + i; // second statement illegal short x = 0; int i = 123456; x += i; // narrowing primitive conversion [JLS 5.1.3] x = x + i; // won’t compile: “possible loss of precision” // [JLS 15.26.2] says about compound assignment operator: // E1 op= E2 <==> E1 = (T) ((E1) op (E2)) do not use compound assignment operators on variables of type byte, short or char Monday, January 12, 2009 12
  • 13. #5 Compound Illegal ________ x = ________; ________ i = ________; x += i; // first statement illegal x = x + i; // second statement legal Object x = “object string ”; String i = “real string”; x += i; // left-hand side object reference type != String x = x + i; // is assignment compatible [JLS 5.2] // string concatenation is performed [JLS 15.26.2] Monday, January 12, 2009 13
  • 14. #6 Unicode Escapes public class UnicodeEscapes { public static void main(String[] args) { // u0022 is the unicode escape for double quote (quot;) System.out.println(quot;au0022.length() + u0022bquot;.length()); } } public class LinePrinter { public static void printLine() { // Note: u000A is Unicode representation of linefeed (LF) char c = 0x000A; System.out.print(c); } } do not use Unicode escapes to represent ASCII characters; avoid Unicode escapes except where they are truly necessary Monday, January 12, 2009 14
  • 15. #7 Classify Characters public class Classifier { public static void main(String[] args) { System.out.println(classify('n') + classify('+') + classify('2')); } public static String classify(char c) { if(quot;0123456789quot;.indexOf(c) >= 0) return quot;NUMERAL quot;; if(quot;abcdefghijklmnopqrstuvwxyzquot;.indexOf(c) >= 0) return quot;LETTER quot;; // TODO finish implementation of operator classification // if(quot;+-*/&|!=quot;.indexOf(c) >= 0) // return quot;OPERATOR quot;; // return quot;UNKOWN quot;; } comment out a section of code by make use of } a sequence of single-line comments Monday, January 12, 2009 15
  • 16. #8 Count Loops public class InTheLoop { public static final int END = Integer.MAX_VALUE; public static final int START = END - 100; public static void main(String[] args) { int count = 0; for (int i = START; i <= END; i++) count++; System.out.println(count); } } for (long i = START; i <= END; i++) count++; whenever using an integral type, be aware of the boundary conditions; and again: watch out for overflow - it’s a silent killer Monday, January 12, 2009 16
  • 17. #9 Never Ending Story ___ start = _____________________; int Integer.MAX_VALUE - 1 for (int i = start; i <= start + 1; i++) { } ______ i = ________________________; // see [IEEE-754] double Double.POSITIVE_INFINITY while (i == i + 1) { } double ______ i = __________; // see [JLS 15.21.1] Double.NaN while (i != i) { } String i = “foobar” // see [JLS 15.18.1] ______ ________; while(i != i + 0) { } Integer new Integer(0) _______ i = ______________; Integer new Integer(0) _______ j = ______________; while(i <= j && j <= i && i != j) { } binary floating-point arithmetic is only an approximation to real arithmetic; operator overloading can be very misleading Monday, January 12, 2009 17
  • 18. #10 Overloaded Constructors public class Confusing { public Confusing(Object o) { System.out.println(quot;Objectquot;); } public Confusing(double[] d) { System.out.println(quot;double arrayquot;); } public static void main(String[] args) { new Confusing(null); } } // overloading process operates in two phases [JLS 15.12.2.5] new Confusing((Object) null); avoid overloading; use different names for different methods (not possible for constructors, therefore use static factory methods) Monday, January 12, 2009 18
  • 19. #11 Which Instance public class Type1 { public static void main(String[] args) { String s = null; System.out.println(s instanceof String); } } public class Type2 { public static void main(String[] args) { System.out.println(new Type2() instanceof String); } // compile time error!!! [JLS 15.20.2, 15.16, 5.5] } public class Type3 { public static void main(String[] args) { Type3 t = (Type3) new Object(); } // runtime exception!!! } Monday, January 12, 2009 19
  • 20. #12 What’s the Point? class Point { class Point2 extends Point { final int x, y; final String c; final String name; Point2(int x,int y,String C) { 2 Point (int X, int Y) { super(x, y); 5 x=X;y=Y; c = C; 3 name = makeN(); } } String makeN() { 4 String makeN() { return super.makeN()+quot;:quot;+c; return quot;[quot;+x+quot;,quot;+y+quot;]quot;; } } public static void main (..) { final String toString() { System.out.println( 6 1 return name; new Point2(4,2,quot;purplequot;)); } } // prints quot;[4,2]:purplequot; // prints quot;[4,2]:nullquot; } } Monday, January 12, 2009 20
  • 21. #12 What’s the Point? class Point { class Point2 extends Point { final int x, y; final String c; final String name; Point2(int x,int y,String C) { Point (int X, int Y) { super(x, y); x=X;y=Y; c = C; // lazy initializing } } String makeN() { String makeN() { return super.makeN()+quot;:quot;+c; return quot;[quot;+x+quot;,quot;+y+quot;]quot;; } } public static void main (..) { String toString() { System.out.println( if(name == null) { new Point2(4,2,quot;purplequot;)); name = makeN(); } } } return name; } } it’s possible observing final instance field before its value has been assigned; never call overridable methods from constructors Monday, January 12, 2009 21
  • 22. #13 Null and Void public class Null { public static void greet() { System.out.println(quot;Hello world!quot;); } public static void main(String[] args) { System.out.println(((Null) null).greet()); } } System.out.println(Null.greet(); invoke static methods in a static way Monday, January 12, 2009 22
  • 23. #14 Name It public class Name { private final String first, last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if(!(o instanceof Name)) return false; Name n = (Name) o; return n.first.equals(first) && n.last.equals(last); } public static void main(String[] args) { Set<Name> set = new HashSet<Name>(); set.add(new Name(quot;Sparquot;, quot;Datquot;)); System.out.println(set.contains(new Name(quot;Sparquot;, quot;Datquot;))); } } Monday, January 12, 2009 23
  • 24. #14 Name It public class Name { private final String first, last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if(!(o instanceof Name)) return false; Name n = (Name) o; return n.first.equals(first) && n.last.equals(last); } public int hashCode() { return 37 * first.hashCode() + last.hashCode(); } } you MUST override hashCode whenever you override equals Monday, January 12, 2009 24
  • 25. #15 Shades of Gray public class ShadesOfGray { public static void main(String[] args) { System.out.println(X.Y.Z); } } class X { static class Y { static String Z = quot;Blackquot;; } static C Y = new C(); } class C { static String Z = quot;Whitequot;; } // when a variable and a type have the same name and // both are in scope, the variable takes precedence [JLS 6.5.2] Monday, January 12, 2009 25
  • 26. #15 Shades of Gray public class ShadesOfGray { public static void main(String[] args) { System.out.println(Ex.Why.z); } } class Ex { static class Why { static String z = quot;Blackquot;; } static See y = new See(); } class See { String z = quot;Whitequot;; } always obey the standard Java naming conventions Monday, January 12, 2009 26
  • 27. A Glossary of Name Reuse  Overriding  method overrides other superclass’ instance methods with the same signature (enabling dynamic dispatch)  Hiding  field/static method/member type hides other with same name (signature) of supertypes  Overloading  method with the same name but with another signature  Shadowing  variable/method/type shadows other with same name&scope  Obscuring  variable obscures a type with the same name Monday, January 12, 2009 27
  • 28. #16 Reflection Infection public class Reflector { public static void main(String[] args) { Set<String> set = new HashSet<String>(); set.add(quot;fooquot;); Iterator it = set.iterator(); Method m = it.getClass().getMethod(quot;hasNextquot;); System.out.println(m.invoke(it)); } } Exception in thread quot;mainquot; IllegalAccessException: Class Reflector can not access a member of a class HashMap $HashIterator with modifiers quot;publicquot; // you cannot legally access a member of // a nonpublic type from another package [JLS 6.6.1] Method m = Iterator.class.getMethod(quot;hasNextquot;); when accessing a type reflectively, use a Class object that represents an accessible type Monday, January 12, 2009 28
  • 29. #17 Lazy Initialization Class initialization [JLS 12.4.2] The class is not yet initialized.   The class is being initialized by the current thread: a recursive request for initialization.  The class is being initialized by some thread other than the current thread.  The class is already initialized Monday, January 12, 2009 29
  • 30. #17 Lazy Initialization public class Lazy { private static boolean initialized = false; static { Thread thread = new Thread(new Runnable() { public void run() { initialized = true; }}); thread.start(); try { thread.join(); } catch(InterruptedException e) { throw new AssertionError(e); } } public static void main(String[] args) { System.out.println(initialized); } } Monday, January 12, 2009 30
  • 31. #18 Class Warfare at.spardat.puzzler.client; public class PrintWords { public static void main(String[] args) { System.out.println( Words.FIRST + quot; quot; + Words.SECOND + quot; quot; + Words.THIRD); } } at.spardat.puzzler.library; public class Words { private Words() { } public static final String FIRST = quot;thequot;; public static final String SECOND = null; public static final String THIRD = quot;setquot;; } API designers should think long and hard before exporting a constant field Monday, January 12, 2009 31
  • 33. Effective Java  always override toString  static factory methods instead constructors  favor immutability  favor composition over inheritance  prefer interfaces to abstract classes  use overloading rarely  string concatenation’s performance  favor static over nonstatic member classes  minimize accessibility Monday, January 12, 2009 33
  • 34. Summary  binary floating-point arithmetic is inexact  be aware of silent overflows  obey general naming conventions  overriding equals => overriding hashCode  carefully read API documentation if you are not shure what a piece of code does, it’s very likely that it doesn’t do what you want it to do Monday, January 12, 2009 34
  • 35. Hardcore Java  by Robert Simmons  344 pages full of hardcore stuff  Covers: Final (!), Immutable Types,  Collections, Exceptions, Constants, Nested Classes, Reflection, References Monday, January 12, 2009 35