Java 14 Tutorial
Selected Reading
- Java 14 - Discussion
- Java 14 - Useful Resources
- Java 14 - Quick Guide
- Java 14 - Deprecation & Removals
- Java 14 - Others
- Java 14 - NUMA Aware G1
- Java 14 - Packaging Tools
- Java 14 - NullPointerException
- Java 14 - pattern for instanceOf
- Java 14 - Text Blocks
- Java 14 - Switch Expressions
- Java 14 - Environment Setup
- Java 14 - Overview
- Java 14 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 14 - pattern for instanceOf
Java 14 - Pattern matching in instanceof
Java 14 introduces instanceof operator to have type test pattern as is a preview feature. Type test pattern has a predicate to specify a type with a single binding variable.
Syntax
if (obj instanceof String s) { }
Example
Consider the following example −
ApiTester.java
pubpc class APITester { pubpc static void main(String[] args) { String message = "Welcome to Tutorialspoint"; Object obj = message; // Old way of getting length if(obj instanceof String){ String value = (String)obj; System.out.println(value.length()); } // New way of getting length if(obj instanceof String value){ System.out.println(value.length()); } } }
Compile and Run the program
$javac -Xpnt:preview --enable-preview -source 14 APITester.java $java --enable-preview APITester
Output
25 25Advertisements