Found 7442 Articles for Java

Java Program to Convert OutputStream to String

Shriansh Kumar
Updated on 05-Aug-2024 18:10:08

2K+ Views

The process of converting an OutputStream to a String is used during unit testing when it is necessary to check the content of the OutputStream before displaying it. In this article, we will explain Java programs to convert OutputStream into String. Here, String is a class in Java which represents sequence of characters and OutputStream class of java.io package is the superclass of those classes that represents an output stream of bytes. Using toString() Method The toString() method of the java.io.ByteArrayOutputStream class converts the specified stream's contents into String using the platform's default character set. Example The following ... Read More

Java Program to Compare Strings

Samual Sam
Updated on 23-Jan-2025 22:54:05

6K+ Views

In this article, we will compare two Strings in Java using the compareTo() method, equals() method, or == operator. Each approach serves a specific purpose in comparing strings, whether it's lexicographical comparison, checking for content equality, or comparing object references. Using the compareTo() methodThe compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer ... Read More

Java Program to Compare Strings

Samual Sam
Updated on 23-Jan-2025 22:54:05

6K+ Views

In this article, we will compare two Strings in Java using the compareTo() method, equals() method, or == operator. Each approach serves a specific purpose in comparing strings, whether it's lexicographical comparison, checking for content equality, or comparing object references. Using the compareTo() methodThe compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer ... Read More

Java Program to Convert contents of a file to byte array and Vice-Versa

Ankith Reddy
Updated on 13-Mar-2020 12:56:11

292 Views

The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array. Assume the file myData contains the following data −Exampleimport java.io.File; import java.io.FileInputStream; public class FileToByteArray {    public static void main(String args[]) throws Exception{       File file = new File("myData");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray = new byte[(int)file.length()];       fis.read(bytesArray);       String s = new String(bytesArray);       System.out.println(s);    } }OutputHi how are you welcome to Tutorialspoint

Java Program to Convert contents of a file to byte array and Vice-Versa

Ankith Reddy
Updated on 13-Mar-2020 12:56:11

292 Views

The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array. Assume the file myData contains the following data −Exampleimport java.io.File; import java.io.FileInputStream; public class FileToByteArray {    public static void main(String args[]) throws Exception{       File file = new File("myData");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray = new byte[(int)file.length()];       fis.read(bytesArray);       String s = new String(bytesArray);       System.out.println(s);    } }OutputHi how are you welcome to Tutorialspoint

Java program to calculate the GCD of a given number using recursion

karthikeya Boyini
Updated on 13-Mar-2020 12:54:09

3K+ Views

You can calculate the GCD of given two numbers, using recursion as shown in the following program.Exampleimport java.util.Scanner; public class GCDUsingRecursion {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number :: ");       int firstNum = sc.nextInt();       System.out.println("Enter second number :: ");       int secondNum = sc.nextInt();       System.out.println("GCD of given two numbers is ::"+gcd(firstNum, secondNum));    }    public static int gcd(int num1, int num2) {       if (num2 != 0){          return gcd(num2, num1 % num2);       } else{          return num1;       }    } }OutputEnter first number :: 625 Enter second number :: 125 GCD of given two numbers is ::125

Java program to calculate the GCD of a given number using recursion

karthikeya Boyini
Updated on 13-Mar-2020 12:54:09

3K+ Views

You can calculate the GCD of given two numbers, using recursion as shown in the following program.Exampleimport java.util.Scanner; public class GCDUsingRecursion {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number :: ");       int firstNum = sc.nextInt();       System.out.println("Enter second number :: ");       int secondNum = sc.nextInt();       System.out.println("GCD of given two numbers is ::"+gcd(firstNum, secondNum));    }    public static int gcd(int num1, int num2) {       if (num2 != 0){          return gcd(num2, num1 % num2);       } else{          return num1;       }    } }OutputEnter first number :: 625 Enter second number :: 125 GCD of given two numbers is ::125

Java program to multiply given floating point numbers

Shriansh Kumar
Updated on 16-Aug-2024 07:29:13

1K+ Views

Suppose two floating point numbers are given as operands and your task is to write a Java program to multiply the given numbers. To perform this operation, initialize two float values, multiply and store the result in another float type variable. Float is a datatype in Java which stores numbers with fractional part. Example Scenario: Input 1: num1 = 1.2 Input 2: num2 = 1.4 Output: product = 1.68 Using Multiplication Operator The multiplication operator is represented by asterisk sign (*). It is categorized under arithmetic operator in Java. It can be used to multiply float values ... Read More

Java program to multiply given floating point numbers

Shriansh Kumar
Updated on 16-Aug-2024 07:29:13

1K+ Views

Suppose two floating point numbers are given as operands and your task is to write a Java program to multiply the given numbers. To perform this operation, initialize two float values, multiply and store the result in another float type variable. Float is a datatype in Java which stores numbers with fractional part. Example Scenario: Input 1: num1 = 1.2 Input 2: num2 = 1.4 Output: product = 1.68 Using Multiplication Operator The multiplication operator is represented by asterisk sign (*). It is categorized under arithmetic operator in Java. It can be used to multiply float values ... Read More

Java program to convert a Set to an array

Lakshmi Srinivas
Updated on 21-Jun-2024 11:15:54

15K+ Views

The Set object provides a method known as toArray(). This method accepts an empty array as argument, converts the current Set to an array and places in the given array. To convert a Set object to an array − Create a Set object. Add elements to it. Create an empty array with size of the created Set. Convert the Set to an array using the toArray() method, bypassing the above-created array as an argument to it. Print the contents of the array.ExampleLive Demoimport java.util.HashSet; import java.util.Set; public class SetToArray {    public static void main(String args[]){       Set set = new HashSet();   ... Read More

Advertisements