Java ArithmeticException The ArithmeticException of java.lang package is an unchecked exception in Java. It is raised when an attempt is made to use a wrong mathematical expression in the Java program. An example of ArithmeticExeption is division by 0. If you divide two numbers and the number in the denominator is zero. The Java Virtual Machine will throw ArithmeticException. Example: Throwing ArithmeticException In the following Java program, we try a mathematical operation where we divide a number by 0. public class ArithmeticExceptionTest { public static void main(String[] args) { int a ... Read More
The Java team and developer community suggest us to follow naming conventions. They are just conventions and are not mandatory but help in writing Java programs that are more understandable and easier to read. For example, class names should generally be nouns, and interface names should be adjectives. Additionally, capitalize the first letter of each separate word. The names used for classes, variables, and methods are called identifiers. Need to Follow Java Naming Conventions Following are the reasons to follow naming conventions: Multiple developers work on the same project simultaneously. Names that follow naming standards reduce ... Read More
The NumberFormatException is a class that represents an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number. Here, the parseXXX() is a group of Java built-in methods that are used to convert string representations of numbers into their corresponding primitive data types. Causes for NumberFormatException in Java The NumberFormatException extends IllegalArgumentException class of java.lang package. It can be thrown by many methods/constructors of the classes. Following are some of them: int parseInt(String s): It throws NumberFormatException if ... Read More
An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods. If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java. ... Read More
Complex numbers are expressed in the form of p + iq, where p and q indicate real numbers and i represents imaginary numbers. For instance, 8.2 + 5.6i is a complex number, where 8.2 is the real part and 5.6i is the imaginary part. As you can see, the real number part contains an integer value (mathematical, not Java integer), and the imaginary part has an additional symbol i. In this article, we will understand how to add two complex numbers in Java. Adding Complex Numbers Since complex numbers are divided into two parts (real and imaginary), the real numbers ... Read More
In Java if you try to print a List (or any) object directly using the print statement a hash-code of the object will be printed (default behaviour). If you want to print each element of your object (such as List, Array, etc.) or perform any operation on them one by one, you need to iterate through it. Java provides various ways to iterate through objects. Following are different approaches to iterate through a List object: Using For Loop Using While Loop Using Iterator Object Iterate over ... Read More
A List is a sequence of elements of similar data types, and like an array, its elements are stored at specific indices, which can be accessed and searchable through the index (i.e., starting at index 0). We can search through each element in the list and retrieve its index (or position) if it is found in the list. We can search in a List in the following ways - Using indexOf() Method of List Interface Using lastIndexOf() Method Using Comparison Approach Searching in a List using ... Read More
In Java, the ArrayList class provides various built-in methods to remove all or a single element from it. Once those methods are called on the ArrayList, the list will become empty (). You can verify this using the size() method; it returns 0, if the list is empty. We can remove all the elements of an ArrayList in Java: Using clear() Method of List Interface Using removeAll() Method Removing all ArrayList Elements using clear() Method The clear() method of the List interface removes (deletes) all the elements from a list. ... Read More
To iterate over a List using a for loop, we need to know the size (which can be calculated using the size() method) of the list, so that the loop can execute an accessing element code block till the last position, which is starts from index 0. Once the loop reaches at the last index of the list, it will stop executing and exit. Here are a few examples of how to iterate over a list using a for loop: Iterate a List using For Loop A for-loop is a "control flow statement " which is ... Read More
A List is an ordered collection or an interface that contains elements of similar types. Since the list is a collection object, we can iterate (or loop) a list using different control flow statements or an Iterator object. This article will discuss one way to (i.e., using forEach loop) iterate over a list: Iterate over a List Using forEach Loop A forEach loop is a control flow statement that helps to iterate (or loop through) over a collections object. As the List is an ordered collection, it can be easily iterated using a forEach loop or any other control flow ... Read More