Object Oriented Programming Articles

Page 303 of 589

Generate a random array of integers in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 40K+ Views

In order to generate random array of integers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to generate a random array of integers in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       int[] arr = new int[5];       for (int i = 0; i

Read More

Print a 2D Array or Matrix in Java

George John
George John
Updated on 11-Mar-2026 32K+ Views

In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper.For this the logic is to access each element of array one by one and make them print separated by a space and when row get to end in matrix then we will also change the row.Examplepublic class Print2DArray {    public static void main(String[] args) {       final int[][] matrix = {          { 1, 2, 3 },          { 4, 5, 6 },          { 7, 8, 9 }       };       for (int i = 0; i

Read More

Array Copy in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 30K+ Views

Array in Java can be copied to another array using the following ways.Using variable assignment. This method has side effects as changes to the element of an array reflects on both the places. To prevent this side effect following are the better ways to copy the array elements.Create a new array of the same length and copy each element.Use the clone method of the array. Clone methods create a new array of the same size.Use System.arraycopy() method.  The arraycopy() can be used to copy a subset of an array.ExampleCreate a java class named Tester.Tester.javapublic class Tester {    public static ...

Read More

How to find Min/Max numbers in a java array?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 32K+ Views

You can find the minimum and maximum values of an array using for loops −Examplepublic class MinAndMax {    public int max(int [] array) {       int max = 0;             for(int i=0; imax) {             max = array[i];          }       }       return max;    }    public int min(int [] array) {       int min = array[0];             for(int i=0; i

Read More

How to convert string to array of integers in java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 44K+ Views

You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.Exampleimport java.util.Arrays; public class StringToIntegerArray { public static void main(String args[]) { String [] str = {"123", "345", "437", "894"}; int size = str.length; int [] arr = new int [size]; for(int i=0; i

Read More

What is the difference between class variables and instance variables in Java?

Johar Ali
Johar Ali
Updated on 11-Mar-2026 40K+ Views

Following are the notable differences between Class (static) and instance variables. Instance variables Static (class) variables Instance variables are declared in a class, but outside a method, constructor or any block. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops. ...

Read More

How to pass Arrays to Methods in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 66K+ Views

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.Suppose we have two methods min() and max() which accepts an array and these methods calculates the minimum and maximum values of the given array respectively:Exampleimport java.util.Scanner; public class ArraysToMethod {    public int max(int [] array) {       int max = 0;       for(int i=0; imax) {   ...

Read More

How to find a unique character in a string using java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Sep-2025 17K+ Views

In this article, we will learn how to find a unique character in a string in Java. A unique character is a character that occurs only once in the string, while all others can occur multiple times. We can solve this problem using the following ways: Using Brute Force method. Using HashMap. Using brute force method To find a unique character in a given string, we will use a brute force approach. In the brute force approach, we will use two loops to compare each character to the rest of the string. Let's look at the steps: ...

Read More

How to check that a string is parse-able to a double in java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Sep-2025 6K+ Views

Let's learn how to check if a string is parseable to a double in Java. We have multiple ways to do this. Some of them are: Using Double.parseDouble() Using Double.valueOf() Using the constructor of the Double class Using Double.parseDouble() The parseDouble() method of the java.lang.Double class accepts a String value, parses it, and returns the double value of the given String. If you pass a null value to this method, it throws a NullPointerException and if this method is not able to parse the given string ...

Read More

What is ARM in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Sep-2025 1K+ Views

Automatic resource management(ARM) or try-with-resources is a new exception handling mechanism that we are going to discuss in this article. What is a resource? A resource is an object which implements AutoClosable interface. Whenever you use a resource in your program, it is recommended to close it after use. For example, if you open a file using FileInputStream, it is recommended to close the stream after usage. This is done to free up the system resources. In Java, we can use the close() method to close the resources. But if you forget to close the resource, it will lead ...

Read More
Showing 3021–3030 of 5,881 articles
« Prev 1 301 302 303 304 305 589 Next »
Advertisements