Java Articles

Page 121 of 450

Multiple inheritance by Interface in Java

Arushi
Arushi
Updated on 11-Mar-2026 86K+ Views

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.A program that demonstrates multiple inheritance by interface in Java is given as follows:Exampleinterface AnimalEat {    void eat(); } interface AnimalTravel {    void travel(); } class Animal implements AnimalEat, AnimalTravel {    public void eat() {       System.out.println("Animal is eating");    }    public void travel() {       System.out.println("Animal is travelling");    } ...

Read More

Java program for Multiplication of Array elements

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 20K+ Views

To find the product of elements of an array.create an empty variable. (product)Initialize it with 1.In a loop traverse through each element (or get each element from user) multiply each element to product.Print the product.Exampleimport java.util.Arrays; import java.util.Scanner; public class ProductOfArrayOfElements {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int product = 1;       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Read More

Split String with Comma (,) in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 34K+ Views

Let’s say the following is our string.String str = " This is demo text, and demo line!";To split a string with comma, use the split() method in Java.str.split("[,]", 0);The following is the complete example.Examplepublic class Demo {     public static void main(String[] args) {        String str = "This is demo text, and demo line!";        String[] res = str.split("[,]", 0);        for(String myStr: res) {           System.out.println(myStr);        }     } }OutputThis is demo text and demo line!

Read More

Concatenate string to an int value in Java

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

To concatenate a string to an int value, use the concatenation operator.Here is our int.int val = 3;Now, to concatenate a string, you need to declare a string and use the + operator.String str = "Demo" + val;Let us now see another example.Exampleimport java.util.Random; public class Demo {    public static void main( String args[] ) {       int val = 3;       String str = "" + val;       System.out.println(str + " = Rank ");    } }Output3 = Rank

Read More

Java Program to convert ASCII code to String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 18K+ Views

To convert ASCII to string, use the toString() method. Using this method will return the associated character.Let’s say we have the following int value, which works as ASCII for us.int asciiVal = 89;Now, use the toString() method.String str = new Character((char) asciiVal).toString();Examplepublic class Demo {    public static void main(String []args) {       int asciiVal = 87;       String str = new Character((char) asciiVal).toString();       System.out.println(str);    } }OutputW

Read More

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
Showing 1201–1210 of 4,498 articles
« Prev 1 119 120 121 122 123 450 Next »
Advertisements