Home » Java » Core Java » Page 5

Core Java

Lambda Expressions Before And After Java 8

Quick guide to few example programs on before and after Java 8. Java 8 Concepts further simplifies the code and profound for core logic rather than syntax. 1. Introduction In this tutorial, We’ll learn how to write programs using java 8 lambda and Stream concepts with examples. Many developers feel learning java 8 concepts may be hard to understand. But once ...

Read More »

Java 14: Helpful NullPointerException Messages

A new JVM option, -XX:+ShowCodeDetailsInExceptionMessages, has been introduced in Java 14, in order to provide helpful NullPointerException messages showing precisely what was null when a NullPointerException occurred. For example, consider the code below: 1 var name = library.get("My Book").getAuthor().getName(); Before Java 14, the JVM would only print the method, filename, and line number that caused the NPE: 1 2 Exception ...

Read More »

Convert time unit to duration in Java

java.util.concurrent.TimeUnit represents time durations in Java at a given unit of granularity and provides utility methods to convert across units. java.util.concurrent.TimeUnit was introduced back in the old Java days (1.5) but since then it has been extended several times already. In this blog post you will learn how to use java.util.concurrent.TimeUnit to convert a given time unit to a duration ...

Read More »

Switch as an expression in Java with Lambda-like syntax

As of Java 14, the switch expression has an additional Lambda-like (case ... -> labels) syntax and it can be used not only as a statement, but also as an expression that evaluates to a single value. Lambda-like syntax (case ... -> labels) With the new Lambda-like sytax, if a label is matched, then only the expression or statement to ...

Read More »

All you wanted to know about Throwable

This article is a tutorial about exceptions. But not the usual one. There are many of those that tell you what exceptions are for, how you can throw one, catch one, the difference between checked and runtime exceptions, and so on. There is no need for another. It would also be boring for you. If not, then go and read ...

Read More »

Record type in Java

JDK 14, released in March 2020, introduced records (preview language feature) which provide a compact syntax for declaring classes whose main purpose is to hold data. In records, all low-level, repetitive and error-prone code is like constructors, accessor and utlity methods such as equals(), hashCode(), toString() are automatically derived based on the record’s state description. Prerequisites You will need JDK ...

Read More »

10 Best Practices to Handle Java Exceptions

In this article, we will see the best practices to handle Java Exceptions. Exception handling in Java is not an easy matter because novices find it hard to know, and even expert developers can waste hours discussing which Java exceptions should be thrown or handled. Hence maximum development companies have their set of rules on how to use them. If ...

Read More »

Looking at Java Records

JEP 359, available as preview feature in JDK 14, introduces records to Java. Records are an easy way to model plain data aggregates. A simple Range record looks like this: 1 record Range(int from, int to) {} A record definition is literally the same as a final class with: immutable fieldspublic accessorsa constructorimplementations for equals(), hashCode() and toString() So we ...

Read More »

Java puzzlers from OCA part 7

In this part of the Java Puzzlers from OCA series, I will show multiple ways of defining Strings and potential surprises related to that. Two basic types of creating Strings are creation with new keyword, and by just using the string literal. 1 2 String strWithNew = new String("hey"); String strWithLiteral = "ho"; As Strings are frequently used JVM uses ...

Read More »