Administrator

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.

Website: https://howtodoinjava.com

Github: https://github.com/lokeshgupta1981/

Twitter: @HowToDoInJAVA

LinkedIn: https://www.linkedin.com/in/lokeshgupta1981/

My Articles:

Program to calculate average of N numbers

Learn to calculate the average of the given N numbers in Java using three different methods. All methods are different based on how they provide the input to the program. 1. Algorithm for calculating the average In simple words, to calculate the average of N numbers we have to add …

Java – Set Classpath from Command Line

Learn to use the -classpath or -cp option to set the Java classpath from the command prompt in Windows and Linux OS. 1. Java Classpath The classpath is the list of directory locations that the Java runtime environment searches for the classes and other resource files, during program execution. The …

Installing Apache Kafka on Windows

Learn to install Apache Kafka on Windows 10 and execute ‘start server‘ and ‘stop server‘ scripts related to Kafka and Zookeeper. We will also verify the Kafka installation by creating a topic, producing a few messages to it and then using a Kafka consumer to read the messages. 1. Prerequisites …

Apache Kafka Tutorial

This Apache Kafka tutorial is for absolute beginners and discusses the architecture, core components and other advanced concepts.

Java 14 – New Features and Improvements

Java 14 reached general availability on 17 March 2020. In this post, we will go through some features from the list of 16 new features added in Java programming language. We can find the JDK 14 binaries here. 1. JEP 305 – Pattern Matching for instanceof (Preview) In Java 14, …

Java Record Type

Java records were introduced as a preview feature in Java 14 [JEP-359] and finalized in Java 16 [JEP-395]. A record, in Java, acts as a transparent carrier for immutable data. Conceptually, records can be thought of as tuples that are already available via 3rd party libraries. Though, records are built-in type …

Java Text Blocks

In Java, a text block is a multi-line string literal. It means we do not need to get into the mess of explicit line terminators, string concatenations, and delimiters.

Java 14 – Helpful NullPointerException (NPE)

Java 14 (JEP 358) improves the usability of NullPointerException generated by the JVM by describing precisely which variable was null. Lets understand this in detail. 1. Better NullPointerException First, we need to pass -XX:+ShowCodeDetailsInExceptionMessages JVM flag to enable this feature while running the application. Make sure, you are passing it. …

The yield keyword in Java

Introduced in Java 13 as part of the enhancements in Project Amber, the ‘yield‘ keyword aims to simplify code, making switch expressions more concise and expressive. Let us learn about ‘yield‘ keyword, its purpose, syntax, and see some practical examples. 1. The ‘yield‘ Keyword The ‘yield’ keyword enhances the switch …

Java Enhanced Switch Expressions

In Java, a switch statement generally allows the application to have multiple possible execution paths based on the value of a given expression in runtime. The evaluated expression is called the selector expression which must be of type char, byte, short, int, Character, Byte, Short, Integer, String, or an enum. …

Java Period – Difference between Dates in Days, Months and Years

Learn to create and use the Period class that was introduced as part of the new Date Time API in Java 8. The Period class represents the period of time in date-based values such as days, months, years, weeks, or years in the ISO-8601 calendar system such as “1 year …

Java TemporalAdjusters – Calculate Recurring Dates

Learn to use Java TemporalAdjusters that help with complex date-time calculations such as calculating recurring meeting dates, processing weekly reports, sending automated monthly report outs, etc. 1. Overview In the new Java Date API, the Temporal interface represents a date, time, or a combination of both. For example, LocalDate, LocalDateTime …

Format a Date to String in Java

Learn to format a given date to a specified formatted string in Java. We will learn to use inbuilt patterns and custom patterns with DateTimeFormatter and SimpleDateFormat. 1. Formatting with DateTimeFormatter [Java 8] Since Java 8, We can use DateTimeFormatter for all types of date and time related formatting tasks. …

Check if a Date is Valid in Java

Learn to validate whether a given string contains a date value in Java using various date validation techniques available in Java 8.

Getting All Dates Between Two Dates in Java

Learn to get all the dates between the two given dates. We will see the solutions in Java 7, Java 8, and Java 9. 1. LocalDate.datesUntil() (since Java 9) LocalDate‘s datesUntil() method returns a sequential ordered stream of all dates between two given dates. The returned stream starts from startDate …

Number of Days between Two Dates in Java

In Java, we can use the ChronoUnit and LocalDate.until() methods to calculate the number of days between two given dates. Other solutions exist, but these are the simplest and do not require importing an additional library. 1. Using ChronoUnit to Find the Number of Days between Two Dates Using ChronoUnit.DAYS.between() …

Find all Business Days between Two Dates

Learn to find all business days between two given dates in Java. The business days are considered all weekdays, excluding all holidays falling on weekdays. Once we have a List of business dates, we can use the list.size() API to get the total count of business days. The given examples …

Add or Subtract Business Days in Java

Learn to add or subtract a given number of business days to new date times classes in Java such as LocalDate, LocalDateTime and ZonedDateTime. The given example takes the holidays list as well into consideration. Read More: Add or Subtract Days to/from Date 1. Adding Business Days It uses two …

Java DayOfWeek

Learn to determine which day of the week is a given date in Java. The weekdays are considered all 7 days from Sunday, Monday till Saturday. 1. DayOfWeek Enum DayOfWeek is an enum representing the seven days of the week – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. 2. …

Java TemporalQuery

TemporalQuery is a standard way of querying the temporal objects (LocalDate, LocalDateTime etc) for making better business decisions. In Java 8, all major date-time classes implement Temporal and TemporalAccessor interfaces so TemporalQuery can be run against all those Java classes. 1. TemporalQuery interface In the new Java Date API, Temporal …

Calculate Next and Previous Date in Java

Java examples to get the next day or the previous day for any given day. The example uses legacy java.util.Date class as well as java.time.LocalDate class from Java 8. We can use this example code to calculate tomorrow’s and yesterday’s dates based on the date of today. 1. Using LocalDate …

Python – Lists vs Tuples

Learn the differences between lists and tuples in Python. Tuples are great for creating Value Objects. Lists are for storing a collection of value objects.

Python – Comparing tuples

In Python, tuples are compared lexicographically by comparing corresponding elements of two tuples. Learn to compare heterogeneous and unequal tuples.

Python – Tuples

In Pyhton, a tuple is similar to list except it is immutable and are written with optional round brackets. A Tuple is: immutable ordered heterogeneous indexed (starts with zero) written with round brackets (optional but recommneded) is faster during iteration, since it is immutable Tuples are useful for creating objects …

Spring HATEOAS Pagination Links

Learn to build automatic pagination links (next, prev) in spring boot hateaos application using PagedModel and PagedResourcesAssembler classes.

Spring Boot HATEOAS

Learn to build hateoas links for REST resources using RepresentationModel and RepresentationModelAssemblerSupport in a Spring boot application example.

Python List

In Python, lists are ordered, indexed (indices start at 0), mutable, heterogeneous (items need not be of the same type) and written CSV in square brackets.

Python Strings

Larn about Python string datatype and its functions. Learn how to format, align, find length and other such useful programs and exercises.

Python Integer (with Examples)

In Python, the int data type (integer) represents whole numbers that can be positive or negative and can have unlimited precision.

Python Keywords

Python keywords are the reserved words and these keywords cannot be used for any other purpose than they have been designed for.

Spring Batch Partitioning into Multiple Steps

Learn to use Spring batch partitioning to use multiple threads to process a range of data sets in a spring boot application. 1. Parallel Processing and Step Partitioning 1.1. Parallel Processing Mostly batch processing problems can be solved using a single-threaded, but a few complex scenarios like single-threaded processing taking …

Python Variables (with Examples)

In Python tutorial, learn the basics of variables, naming conventions, and the difference between local and global variables with examples.

Python Single and Multi-Line Comments with Shortcuts

In Python (or any other programming language), comments are used to explain the source code. Comments describe the code, which helps in maintaining the application in the future for ourselves and others. In Python, comments come in two primary forms: single-line comments and multiple-line comments. Let us learn in detail. …

Java IntPredicate Example

Java 8 IntPredicate is a functional interface whose functional method is boolean test(int a). It can be considered a function returning true or false value.

Guide to IntStream in Java

Java IntStream represents a stream of primitive int-valued elements supporting sequential and parallel aggregate operations.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.