Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat Sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

Simplify Local Variable Type Definition Using the Java 10 var Keyword

May 25, 2018
Rahul Kumar
Related topics:
Java
Related products:
Red Hat Enterprise Linux

    As many of you might have heard, Java 10 was released in March 2018. It is a short-term release from Oracle Corporation and came with lot of new features and enhancements. One of the important features in Java 10 is local variable type inference, which is detailed in JEP (Java Enhancement Proposal) 286. The upcoming Java release, due in September 2018, will be a long-term-support (LTS) version of Java. (Note that generally, LTS releases are due every three years.)

    Let's look at a  Java 10 local variable type inference feature example now.

    The main advantage of this feature is to reduce boilerplate variable type definitions and to increase code readability. Here's an example:

    String s=new String("Java 10");
    Integer int=new Integer(10);
    

    A Java developer would have no problem reading the above two statements. However, as another example, here are some more-complex statements that are kind of pain to write:

    MAP<String,String> map=new HashMap<String,String>(); 
    MAP<User,List<String>> listofMovies=new HashMap<>();
    

    In Java 10, the var keyword allows local variable type inference, which means the type for the local variable will be inferred by the compiler, so you don't need to declare that. Hence, you can replace the above two statements as shown below:

    var map=new HashMap<String,String>();
    var listofMovies=new HashMap<User,List<String>>();

    Below are the few points to remember about local variable type inference in Java 10:

    1. Each statement containing the var keyword has a static type which is the declared type of value. This means that assigning a value of a different type will always fail. Hence, Java is still a statically typed language (unlike JavaScript), and there should be enough information to infer the type of a local variable. If that is not there, compilation fails, for example:

    var id=0;// At this moment, compiler interprets 
    //variable id as integer.
    id="34"; // This will result in compilation error 
    //because of incompatible types: java.lang.String 
    //can't be converted to int.
    

    Notice that JavaScript also has the concept of a var keyword, but that is completely different from Java 10 var. JavaScript does not have type definitions for variables. As a result, the above example would have been successfully interpreted by the JavaScript runtime, and that is one of the reasons TypeScript was introduced.

    2. Let's look at an inheritance scenario. Assume there are two subclasses (Doctor, Engineer) extended from the parent class Person. Let's say someone creates an object of Doctor, as shown below:

    var p=new Doctor(); // In this case, what should be
    //the type of p; it is Doctor or Person?

    Note that in such cases, a variable declared with var is always the type of the initializer (Doctor, in this case), and var may not be used when there is no initializer. Therefore, if you reassign the above variable p, as shown below, compilation fails:

    p=new Engineer(); // Compilation error saying
    //incompatible types
    

    So we can say that polymorphic behavior does not work with the var keyword.

    3. The following  are places where you cannot use local variable type inference:

    a)  You can't use local variable type inference with method arguments:

    public long countNumberofFiles(var fileList);// Compilation 
    //error because compiler cannot infer type of local
    //variable fileList; cannot use 'var' on variable without 
    //initializer
    

    b) You cannot initialize a var variable to null. By assigning null, it is not clear what the type should be, since in Java, any object reference can be null. In the following example, because there is no predefined data type for a null value, the compiler is not able to interpret a type for count, which would cause a complication error.

    var count=null;// Compilation error because 
    //compiler cannot infer type for local variable
    //count since any Java object reference can be null
    

    Note that JavaScript has a data type NULL, which can hold only one value: Null.

    c) You can't use local variable type inference with lambda expressions, because those require an explicit target type. For example, the following causes a compilation error:

    var z = () -> {} // Compilation error because
    //compiler cannot infer type for local variable z;
    //lambda expression needs an explicit target type
    

    Java 10 var is desgined to improve the readability of code for other developers who read the  code. In some situations, it can be good to use var; however, in other situations it can reduce the readability of code.

    Here's an example of looping over an entrySet of a Map:

    Map<String, List<String>> companyToEmployees= new HashMap<>();
      for (Map.Entry<String, List<String>> entry: companyToEmployees . entrySet()) {
          List<String> employees= entry.getValue();
    }
    

    Let's rewrite the above code using Java 10 var:

    var companyToEmployees= new HashMap<String, List<String>>();
      for (var entry: companyToEmployees. entrySet()) {
           var employees= entry.getValue();
    }
    

    From the above example, it is clear that using var might not always be good.

    For more information about the var keyword, I recommend going through the Java 10 local variable type reference docs.

    Last updated: August 1, 2023

    Recent Posts

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    • Control multicluster costs with right-sizing in Red Hat Advanced Cluster Management 5

    • Lightbits is now certified for Red Hat OpenStack Services on OpenShift

    • What's new in Red Hat OpenShift 5 for developers

    • On-demand screen context in Red Hat Developer Hub intelligent assistant

    What’s up next?

     

    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    Red Hat Developer

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility
    Ask AI