Java Articles

Page 140 of 450

\\nWhy we should follow the naming conventions in Java?\\n

Shriansh Kumar
Shriansh Kumar
Updated on 27-May-2025 4K+ Views

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

How to handle the NumberFormatException (unchecked) in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 27-May-2025 2K+ Views

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

Java Program to Add Two Complex numbers

Shriansh Kumar
Shriansh Kumar
Updated on 27-May-2025 2K+ Views

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

How to iterate over a list in Java?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 6K+ Views

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

How to search in a List of Java object?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 4K+ Views

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

How to iterate a List using for Loop in Java?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 9K+ Views

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

How to use List size() method in Java With Examples?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 754 Views

In Java, a List is an interface which stores a sequence of elements of similar types. Since the list contains multiple elements, we might need to know how many element the list currently have. To count the total number of elements present in a list, the List interface provides a method named size(). The List size() Method The size() method of List interface is used to retrieve size of the current List. The size refers to total number of items currently the list holds. For example, a list have {1, 2, 3}, then the size of the list will be ...

Read More

How To Calculate Volume of Prism in Java?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 1K+ Views

A Prism refers to a three-dimensional solid object which has two identical ends. A prism has two faces, which are: Top and Bottom face. Lateral face Both top and bottom faces are called bases which are identical to each other. All lateral faces are also identical to each other which belong to the class of parallelograms. When the base of the prism is triangular in shape that prism is called a Triangular prism. Similarly, when the base of a prism is rectangular in shape it is called a Rectangular prism. ...

Read More

Can we override a start() method in Java?

Vivek Verma
Vivek Verma
Updated on 26-May-2025 3K+ Views

Yes! we can override the start() method of the Thread class in Java. But, if we do so, we must call it using super.start(), which will create a new thread; otherwise, no new thread will be created, and the run() method will not execute in parallel for the other threads. Overriding the start() Method In Java, the start() method is used to begin (start) the execution of a new Thread. When this method is called, it invokes the run() method, which executes in parallel with other threads.  Example In the following example, we override the start() method within the subclass of ...

Read More

What are the differences between default constructor and parameterized constructor in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 16K+ Views

The default and parameterized constructors are two types of Constructor in Java. The constructor is a special member of a Java class whose name is the same as the class name. It is used to assign values to a class variable at the time of object creation. In this article, we are going to discuss the difference between default and parameterized constructors. Default Constructor When we do not add a constructor to a Java class. The compiler adds a default constructor implicitly. It accepts 0 arguments. If we do not initialize the instance variables of a class, a default constructor will ...

Read More
Showing 1391–1400 of 4,496 articles
« Prev 1 138 139 140 141 142 450 Next »
Advertisements