Core Java

Case-Insensitive String Matching in Java

  • Satish 

1. Overview There are many ways to check if a String contains a substring. In this article, we’ll be looking for substrings within a String while focusing on case-insensitive workarounds to String.contains() in Java. Most importantly, we’ll provide examples of how to solve this issue. 2. The Simplest Solution: String.toLowerCase The simplest solution is by using String.toLowerCase(). In this case, we’ll transform both strings to lowercase and then use the contains() method: assertTrue(src.toLowerCase().contains(dest.toLowerCase())); We can also use String.toUpperCase() and it would provide the same result. 3. String.matches With Regular Expressions Another option is by… Read More »Case-Insensitive String Matching in Java

Comparing Long Values in Java

  • Satish 

1. Overview In this short tutorial, we’ll discuss different ways to compare two Long instances. We emphasize the problems that arise when using the reference comparison operator (==). 2. Problem Using Reference Comparison Long is a wrapper class for the primitive type long. Since they are objects and not primitive values, we need to compare the content of Long instances using .equals() instead of the reference comparison operator (==). In some cases, we may get the idea that == is okay, but looks are deceiving. Consider that we can use ==… Read More »Comparing Long Values in Java

Overflow and Underflow in Java

  • Satish 

1. Introduction In this tutorial, we’ll look at the overflow and underflow of numerical data types in Java. We won’t dive deeper into the more theoretical aspects — we’ll just focus on when it happens in Java. First, we’ll look at integer data types, then at floating-point data types. For both, we’ll also see how we can detect when over- or underflow occurs. 2. Overflow and Underflow Simply put, overflow and underflow happen when we assign a value that is… Read More »Overflow and Underflow in Java

Checked and Unchecked Exceptions in Java

  • Satish 

1. Overview Java Exceptions fall into two main categories: checked exceptions and unchecked exceptions. In this article, we’ll provide some code samples on how to use them. 2. Checked Exceptions In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist. Java verifies checked exceptions at compile-time. Therefore, we should use the throws keyword to declare a checked exception: private static void checkedExceptionWithThrows() throws FileNotFoundException { File file = new File(“not_existing_file.txt”);… Read More »Checked and Unchecked Exceptions in Java

Java Copy Constructor

  • Satish 

1. Introduction A copy constructor in a Java class is a constructor that creates an object using another object of the same Java class. That’s helpful when we want to copy a complex object that has several fields, or when we want to make a deep copy of an existing object. 2. How to Create a Copy Constructor To create a copy constructor, we can first declare a constructor that takes an object of the same type as a parameter: public class Employee { private… Read More »Java Copy Constructor

Anonymous Classes in Java

  • Satish 

1. Introduction In this tutorial, we’ll consider anonymous classes in Java. We’ll describe how we can declare and create instances of them. We’ll also briefly discuss their properties and limitations. 2. Anonymous Class Declaration Anonymous classes are inner classes with no name. Since they have no name, we can’t use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may… Read More »Anonymous Classes in Java

Immutable Objects in Java

  • Satish 

1. Overview In this tutorial, we’ll learn what makes an object immutable, how to achieve immutability in Java, and what advantages come with doing so. 2. What’s an Immutable Object? An immutable object is an object whose internal state remains constant after it has been entirely created. This means that the public API of an immutable object guarantees us that it will behave in the same way during its whole lifetime. If we take a look at the class String, we can… Read More »Immutable Objects in Java

Create a Custom Exception in Java

  • Satish 

1. Introduction In this tutorial, we’ll cover how to create a custom exception in Java. We’ll show how user-defined exceptions are implemented and used for both checked and unchecked exceptions. 2. The Need for Custom Exceptions Java exceptions cover almost all general exceptions that are bound to happen in programming. However, we sometimes need to supplement these standard exceptions with our own. The main reasons for introducing custom exceptions are: Business logic exceptions – Exceptions that are specific to the business… Read More »Create a Custom Exception in Java

Exception Handling in Java

  • Satish 

1. Overview In this tutorial, we’ll go through the basics of exception handling in Java as well as some of its gotchas. 2. First Principles 2.1. What Is It? To better understand exceptions and exception handling, let’s make a real-life comparison. Imagine that we order a product online, but while en-route, there’s a failure in delivery. A good company can handle this problem and gracefully re-route our package so that it still arrives on time. Likewise, in Java, the code can… Read More »Exception Handling in Java

The “final” Keyword in Java

  • Satish 

1. Overview While inheritance enables us to reuse existing code, sometimes we do need to set limitations on extensibility for various reasons; the final keyword allows us to do exactly that. In this tutorial, we’ll take a look at what the final keyword means for classes, methods, and variables. 2. Final Classes Classes marked as final can’t be extended. If we look at the code of Java core libraries, we’ll find many final classes there. One example is the String class. Consider the situation if we can extend the String class, override any of its methods, and… Read More »The “final” Keyword in Java