Core Java Language concepts

Using the Not Operator in If Conditions in Java

  • Satish 

1. Introduction In Java’s if-else statements we can take a certain action when an expression is true, and an alternative when it is false. In this tutorial, we’ll learn about how to reverse the logic using the not operator. 2. The if-else Statement Let’s start with a simple if-else statement: boolean isValid = true; if (isValid) { System.out.println(“Valid”); } else { System.out.println(“Invalid”); } What if our program only needs to handle the negative case? How would we re-write the above example? One option is to simply remove the code… Read More »Using the Not Operator in If Conditions in Java

Control Structures in Java

  • Satish 

1. Overview In the most basic sense, a program is a list of instructions. Control structures are programming blocks that can change the path we take through those instructions. In this tutorial, we’ll explore control structures in Java. There are three kinds of control structures: Conditional Branches, which we use for choosing between two or more paths. There are three types in Java: if/else/else if, ternary operator and switch. Loops that are used to iterate through multiple values/objects and repeatedly run specific code blocks. The basic loop types in… Read More »Control Structures in Java

Concrete Class in Java

  • Satish 

1. Introduction In this quick guide, we’ll discuss the term “concrete class” in Java. First, we’ll define the term. Then, we’ll see how it’s different from interfaces and abstract classes. 2. What Is a Concrete Class? A concrete class is a class that we can create an instance of, using the new keyword. In other words, it’s a full implementation of its blueprint. A concrete class is complete. Imagine, for example, a Car class: public class Car { public String honk() { return “beep!”; } public… Read More »Concrete Class in Java

A Guide to the finalize Method in Java

  • Satish 

1. Overview In this tutorial, we’ll focus on a core aspect of the Java language – the finalize method provided by the root Object class. Simply put, this is called before the garbage collection for a particular object.   2. Using Finalizers The finalize() method is called the finalizer. Finalizers get invoked when JVM figures out that this particular instance should be garbage collected. Such a finalizer may perform any operations, including bringing the object back to life. The main purpose of a finalizer is, however,… Read More »A Guide to the finalize Method in Java

Object Type Casting in Java

  • Satish 

1. Overview The Java type system is made up of two kinds of types: primitives and references. We covered primitive conversions in this article, and we’ll focus on references casting here, to get a good understanding of how Java handles types. 2. Primitive vs. Reference Although primitive conversions and reference variable casting may look similar, they’re quite different concepts. In both cases, we’re “turning” one type into another. But, in a simplified way, a primitive variable contains its value, and conversion of… Read More »Object Type Casting in Java

Wrapper Classes in Java

  • Satish 

1. Overview As the name suggests, wrapper classes are objects encapsulating primitive Java types. Each Java primitive has a corresponding wrapper: boolean, byte, short, char, int, long, float, double  Boolean, Byte, Short, Character, Integer, Long, Float, Double These are all defined in the java.lang package, hence we don’t need to import them manually. 2. Wrapper Classes “What’s the purpose of a wrapper class?”. It’s one of the most common Java interview questions. Basically, generic classes only work with objects and don’t support primitives. As a result, if… Read More »Wrapper Classes in Java

Using Java Assertions

  • Satish 

1. Introduction The Java assert keyword allows developers to quickly verify certain assumptions or state of a program. In this article, we’ll take a look at how to use the Java assert keyword. 2. History of Java Assertions The Java assert keyword was introduced in Java 1.4, so it’s been around for quite a while. However, it remains a little-known keyword that can drastically reduce boilerplate and make our code more readable. For example, often times in our code we need to verify certain conditions that might… Read More »Using Java Assertions

Class Loaders in Java

  • Satish 

1. Introduction to Class Loaders Class loaders are responsible for loading Java classes during runtime dynamically to the JVM (Java Virtual Machine). Also, they are part of the JRE (Java Runtime Environment). Hence, the JVM doesn’t need to know about the underlying files or file systems in order to run Java programs thanks to class loaders. Also, these Java classes aren’t loaded into memory all at once, but when required by an application. This is where class loaders come into the picture.… Read More »Class Loaders in Java

Java – Try with Resources

  • Satish 

1. Overview Support for try-with-resources – introduced in Java 7 – allows us to declare resources to be used in a try block with the assurance that the resources will be closed when after the execution of that block. The resources declared must implement the AutoCloseable interface. 2. Using try-with-resources Simply put, to be auto-closed, a resource must be both declared and initialized inside the try, as shown below: try (PrintWriter writer = new PrintWriter(new File(“test.txt”))) { writer.println(“Hello World”); } 3. Replacing try–catch-finally With try-with-resources The simple and obvious… Read More »Java – Try with Resources