Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Algorithms
  • Analysis of Algorithms
  • Sorting
  • Searching
  • Greedy
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Divide and Conquer
  • Geometric Algorithms
  • Mathematical Algorithms
  • Pattern Searching
  • Bitwise Algorithms
  • Branch & Bound
  • Randomized Algorithms
Open In App
Next Article:
Diamond operator for Anonymous Inner Class with Examples in Java
Next article icon

Diamond operator for Anonymous Inner Class with Examples in Java

Last Updated : 16 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Prerequisite: Anonymous Inner Class

Diamond Operator: Diamond operator was introduced in Java 7 as a new feature.The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable. The diamond operator could not be used with Anonymous inner classes in JDK 7. In JDK 9, it can be used with the anonymous class as well to simplify code and improves readability. Before JDK 7, we have to create an object with Generic type on both side of the expression like:

  // Here we mentioned the generic type  // on both side of expression while creating object  List<String> geeks = new ArrayList<String>();  

When Diamond operator was introduced in Java 7, we can create the object without mentioning generic type on right side of expression like:

  List<String> geeks = new ArrayList<>();  

Problem with Diamond Operator in JDK 7? With the help of Diamond operator, we can create an object without mentioning the generic type on the right hand side of the expression. But the problem is it will only work with normal classes. Suppose you want to use the diamond operator for anonymous inner class then compiler will throw error message like below: Java

// Program to illustrate the problem // while linking diamond operator // with an anonymous inner class  abstract class Geeksforgeeks<T> {     abstract T add(T num1, T num2); }  public class Geeks {     public static void main(String[] args)     {         Geeksforgeeks<Integer> obj = new Geeksforgeeks<>() {             Integer add(Integer n1, Integer n2)             {                 return (n1 + n2);             }         };         Integer result = obj.add(10, 20);         System.out.println("Addition of two numbers: " + result);     } } 
Output:

  prog.java:9: error: cannot infer type arguments for Geeksforgeeks          Geeksforgeeks  obj = new Geeksforgeeks  () {                                                          ^    reason: cannot use '' with anonymous inner classes    where T is a type-variable:      T extends Object declared in class Geeksforgeeks  1 error  
Java developer extended the feature of the diamond operator in JDK 9 by allowing the diamond operator to be used with anonymous inner classes too. If we run the above code with JDK 9, then code will run fine and we will generate the below output. Output:
  Addition of two numbers: 30  

Next Article
Diamond operator for Anonymous Inner Class with Examples in Java

B

bishaldubey
Improve
Article Tags :
  • Java
  • Algorithms
  • Technical Scripter
  • DSA
Practice Tags :
  • Algorithms
  • Java

Similar Reads

    Class forName(String, boolean, ClassLoader) method in Java with Examples
    The forName(String, boolean, ClassLoader) method of java.lang.Class class is used to get the instance of this Class with the specified class name, using the specified class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.Syntax: pu
    2 min read
    Anonymous Inner Class in Java
    Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as o
    7 min read
    Different Types of Classes in Java with Examples
    A class is a user-defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:   Modifiers: A class can be public or has default access
    11 min read
    Class getSimpleName() method in Java with Examples
    The getSimpleName() method of java.lang.Class class is used to get the simple name of this class, as given in the sourcecode. The method returns the simple name of this class in the form of String. If this class is anonymous, then this method returns empty string.Syntax: public String getSimpleName(
    2 min read
    Java - Divide the Classes into Packages with Examples
    Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains vari
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences