Home » Java » Core Java » Page 10

Core Java

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 »

A Java XMPP Load Test Tool

In this article, we will develop an XMPP Load Test Tool written in Java. Table Of Contents 1. Introduction 2. XMPP Load Test tools 3. Prerequisites 4. LoadXmppTest Java program 4.1. Create a new Maven project 4.2. Create the main class 4.3. XmppManager class 4.4. Build 4.5. Load test 5. Summary 6. References 7. Download the Maven project   1. ...

Read More »

How to convert Array to List in Java

Hello Friends, In this tutorial, we will learn, various ways in which we can convert an array to a List. package com.blogspot.javasolutionsguide; import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class ArrayToList { public static void main(String[] args) { //Before Java 8 System.out.println("Before JAVA 8........................................"); // 1. ...

Read More »

Java puzzlers from OCA part 6

Even for new Java developers, constructors are probably no big mystery. In essence, when you create an instance of a class, the constructor of this class is started. In the 6th part of Java Puzzlers series, we will see a case related to constructors. 01 02 03 04 05 06 07 08 09 10 public class Puzzler {       public ...

Read More »