Found 7442 Articles for Java

Java Program to Illustrate a Method without Parameters and Return Type

Sakshi Ghosh
Updated on 11-Apr-2023 14:55:22

463 Views

First, let us get acquainted with the syntax, and examples, and then finally the implementation. The methods in Java are of great importance since it allows reusability of the same code, reducing the number of statements to be written within the code. There are three main parts of the method in order to make it executable. Declaration of the method. Definition of the method. Invoking the method. Method invoking is the last step, while the other two can be interchanged. The only thing to be kept in mind here is, the method must be declared before invoking it. ... Read More

Java Program to Illustrate a Method with 2 Parameters and without Return Type

Sakshi Ghosh
Updated on 11-Apr-2023 14:54:10

2K+ Views

A method without a return type is termed a void method since it returns nothing. This method can accept multiple parameters. In this tutorial, we will implement Java programs illustrating a method with two parameters and without return type. First of all, we are going to get acquainted with the syntax, examples, and, finally implementation. Syntax public static void method_name (int parameter1, int parameter2) Here, public − It is the access specifier specifying who can access this method. static − It is mandatory to make the method static to avoid errors. void − it denotes that this method returns ... Read More

Java program to handle Unchecked Exception

Sakshi Ghosh
Updated on 24-Sep-2024 21:43:45

1K+ Views

In this article, we will learn to handle Unchecked Exception in Java. Exceptions are the unexpected circumstances occurring during the implementation of the program i.e., at the run time, that interrupt the usual working of the program. It can occur due to various reasons such as Illegal input given by the user, Failure of the devices, Loss of network connection, Physical limitations, Code faults, trying to open an unavailable file, etc. There are two major categories of Exceptions  − Checked Exception ... Read More

Java Program to Handle Divide by Zero and Multiple Exceptions

Shiva Keerthi
Updated on 31-May-2024 14:07:14

10K+ Views

Exceptions are the unusual events which disrupt the normal flow of execution of a program. When an exception occurs an object called exception object is generated, which contains details of exception like name, description, state of program. In this section, we are going to write a java program to handle divide by zero exception and how to handle multiple exceptions. Types of Exceptions− There are three types of exceptions − Checked exception − Checked exceptions are compile time exceptions i.e, they are checked during compilation of program.These cannot be ignored and must be handled by the programmer. ... Read More

Java Program to Find Sum of First N Odd numbers and Even numbers

Shiva Keerthi
Updated on 11-Apr-2023 14:17:21

4K+ Views

In this article, we are going to write a java program to find the sum of first n Odd and Even numbers. A number can be divided into two categories based on whether when it is divided by ‘2’ gives remainder ‘0’ or ‘1’. Odd numbers are the numbers which cannot be divided by ‘2’ or these numbers give remainder as 1 when they are divided by ‘2’. In other terms which can be written in the form of ‘2n+1’and Even numbers are numbers which can be divided by 2 i.e., these numbers give remainder as 0 when they are ... Read More

Java Program to Get Year from Date

Shiva Keerthi
Updated on 11-Apr-2023 14:15:57

4K+ Views

In our day to day life, there is a huge data being generated in this real world. Among the information generated, Dates are generally used for various things such as scheduling an appointment, planning for day-to-day activities. While working with dates in programming, extracting the year from a specific date can be a common task. We can extract the Year from a particular Date using different in-built functions in Java programming language. In this article we will be discussing different approaches for extracting year from a particular date. Example Input: "13-07-2000" Output: 2000 Example Input: "1997/07/13" Output: 1997 ... Read More

Java program to get Size of Directory

Shiva Keerthi
Updated on 28-Jan-2025 14:57:26

856 Views

A group of files combined stored at a common location is known as a “Directory”. A directory not only contains a group of files but also it can contain a group of other directories as well. A directory is also known as a “Folder”. Directories are a fundamental component of file systems and are used by different operating systems to provide a logical organization of files and to help us perform file management tasks such as searching, sorting, and moving files. In this section, we are going to write a Java program to get the size of a directory. What is ... Read More

Java Program to Concatenate Two List

Bharti Kumari
Updated on 31-May-2024 16:15:26

9K+ Views

Introduction In Java, a list is an ordered collection of elements that allows duplicates. Sometimes, we may need to combine two lists into one list. Concatenating two lists means joining the elements of the two lists to form a new list containing all the elements of both lists. There are various ways to concatenate two lists in Java, including using loops, streams, and built-in methods. In this context, we will explore different approaches to concatenate two lists in Java. One approach is to use the addAll() method provided by the List interface. This method adds all the elements of one ... Read More

Java program to compute the sum of numbers in a list using while-loop

Bharti Kumari
Updated on 21-Oct-2024 17:42:02

677 Views

In this article, we will learn to compute the sum of numbers in a list using Java with a while-loop. We will look at two examples: one that sums a list of integers and another that sums a list of double values. For each example, we will create an ArrayList to store the numbers, and then use a while-loop to iterate through the list. During the iteration, we will add each number to a variable called "sum, " which will keep track of the total. This will help you understand how to use loops and ArrayLists in Java effectively. Different ... Read More

Java Program to Compute the Sum of Numbers in a List Using For-Loop

Bharti Kumari
Updated on 10-Apr-2023 15:58:44

3K+ Views

Introduction Java is a popular programming language that is used for developing a wide range of applications, including those that involve working with lists of numbers. One common task is to compute the sum of numbers in a list, which can be accomplished using a for-loop. In this approach, we first create a list of numbers, then initialize a variable to hold the sum. We then use a for-loop to iterate over each element in the list, adding it to the sum variable. After the loop has completed, the sum variable contains the total sum of all the numbers in ... Read More

Advertisements