CS2030 AY19/20 Semester 2
  • Introduction
  • Textbook Contributions
  • Peer Learning Tasks
  • Piazza Participation
  • Setting Up Lab Environment
  • Setting Up Java
  • Setting Up Vim
  • Setting Up MacVim
  • Setting Up Sunfire and PE Nodes
  • Setting Up Checkstyle
  • Textbook
  • CS2030 Java Style Guide
  • CS2030 Javadoc Specification
  • JDK 11 Download Link
  • Codecrunch
  • Github Repo
  • Piazza Forum

  • The Diamond Operator


    Edit the material here!

    You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context.

    Box<Burger> boxofBurger = new Box();  // type: raw
    Box<Burger> boxofBurger = new Box<Burger>(); // type: Burger
    Box<Burger> boxofBurger = new Box<>(); // type: Burger

    However, if we create a new object using the <> diamond operator without assigning it to another variable, the type argument is inferred to be of type Object.

    Box boxofBurger = new Burger<>();  // type taken to be Object