Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

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

Share:

    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

    • Container starting and termination order in a pod

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    What’s up next?

     

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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

    Red Hat legal and privacy links

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

    Report a website issue