Found 4336 Articles for Java 8

Compare two-byte arrays in a single line in Java

George John
Updated on 25-Jun-2020 12:16:20

105 Views

Two byte arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order. A program that compares two byte arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new byte[] { 1, 7, 5 }, new byte[] { 1, 7, 5 });       System.out.println("The two byte arrays are ... Read More

Dump Multi-Dimensional arrays in Java

Ankith Reddy
Updated on 25-Jun-2020 12:18:52

225 Views

A multi-dimensional array can be easily printed by using the method java.util.Arrays.deepToString() in Java. This method converts the multi-dimensional array to string and prints the array contents enclosed in square brackets.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       int arr [][]= { {5, 8, 6, 3}, {1, 9, 7}, {4, 8, 2}, {6} };       System.out.println("The multi-dimensional array content is:");       System.out.println(Arrays.deepToString(arr));    } }OutputThe multi-dimensional array content is: [[5, 8, 6, 3], [1, 9, 7], [4, 8, ... Read More

Java Program to sort an array in case-sensitive order

Arjun Thakur
Updated on 25-Jun-2020 12:19:25

304 Views

An array can be sorted in case-sensitive order using the java.util.Arrays.sort() method. Only a single argument required in this case for this method i.e. the array to be sorted. A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" };       System.out.print("The unsorted array is: ");       System.out.println(Arrays.toString(arr));       Arrays.sort(arr);       System.out.print("The sorted array in case-sensitive order is: ");       System.out.println(Arrays.toString(arr));    } ... Read More

Sort Byte Array in Java

Chandu yadav
Updated on 25-Jun-2020 12:20:19

244 Views

A byte array can be sorted using the java.util.Arrays.sort() method with a single argument required i.e. the array to be sorted. A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       byte[] arr = new byte[] { 4, 1, 9, 7, 5};       System.out.print("The original byte array is: ");       for (byte i: arr) {          System.out.print(i + " ");       }       Arrays.sort(arr);       System.out.print("The sorted byte array is: "); ... Read More

Get the list of all declared fields in Java

Smita Kapse
Updated on 25-Jun-2020 12:21:02

7K+ Views

The list of all declared fields can be obtained using the java.lang.Class.getDeclaredFields() method as it returns an array of field objects. These field objects include the objects with the public, private, protected and default access but not the inherited fields.Also, the getDeclaredFields() method returns a zero length array if the class or interface has no declared fields or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Example Live Demoimport java.lang.reflect.*; public class Demo {    public static void main(String[] argv) throws Exception {       Class ... Read More

Get all declared fields from a class in Java

Krantik Chavan
Updated on 25-Jun-2020 12:22:18

3K+ Views

An array of field objects is returned by the method java.lang.Class.getDeclaredFields(). These field objects include the objects with the public, private, protected and default access but not the inherited fields.Also, the getDeclaredFields() method returns a zero length array if the class or interface has no declared fields or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    int i;    char c;    public Demo(int i, char c) {       this.i = i;     ... Read More

How to call Private Constructor in Java

Anvi Jain
Updated on 25-Jun-2020 12:23:27

925 Views

The method java.lang.Class.getDeclaredConstructor() can be used to obtain the constructor object for the private constructor of the class. The parameter for this method is a Class object array that contains the formal parameter types of the constructor.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    String str;    Double d;    public Demo(String str, Double d) {       this.str = str;       this.d = d;    }    public static void main(String[] args) {       try {          Demo obj = ... Read More

Load class with forName() method in Java

Nancy Den
Updated on 25-Jun-2020 12:24:16

2K+ Views

The class object associated with the class with the given string name can be returned with the method java.lang.Class.forName(String name, boolean initialize, ClassLoader loader), using the class loader that is used to load the class.The parameters in the forName() method are name, initialize and loader. If the value of the parameter loader is null, then the class is loaded using the bootstrap class loader. Also, if the initialize parameter is true, then only the class is initialized if it has not been initialized earlier.A program that loads the class using the forName() method is given as follows −Example Live Demoimport java.lang.*; ... Read More

Prove that the interface for a primitive type is an empty array in Java

Anvi Jain
Updated on 25-Jun-2020 12:25:30

89 Views

The getInterfaces() method is used to prove that the interface for a primitive type is an empty array. A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.util.*; public class Demo {    public static void main(String[] args) {       Class c = int.class;       Class[] interfaces = c.getInterfaces();       System.out.println("The Interface is: " + Arrays.asList(interfaces));    } }OutputThe Interface is: []Now let us understand the above program.The int.class is a reference to the Class object for the primitive type int. The interface for this is determined using the getInterfaces() ... Read More

Compare two char arrays in a single line in Java

Smita Kapse
Updated on 25-Jun-2020 12:26:39

3K+ Views

Two char arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two char arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new char[] { 'a', 'b', 'c' }, new char[] { 'a', 'b', 'c' });       System.out.println("The two char arrays are equal? ... Read More

Advertisements