Java Articles

Page 107 of 450

Convert array to generic list with Java Reflections

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 728 Views

An array can be converted into a fixed size list using the java.util.Arrays.asList() method. This method is basically a bridge between array based AP!’s and collection based API’s.A program that demonstrates the conversion of an array into a generic list is given as follows −Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       String str[] = new String[]{"apple", "orange", "mango", "guava", "melon"};       List list = Arrays.asList(str);       System.out.println("The list is: " + list);    } }The output of the above program is as follows ...

Read More

Demonstrate getting the immediate superclass information in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 196 Views

The immediate superclass information of any entity such as an object, class, primitive type, interface etc. can be obtained using the method java.lang.Class.getSuperclass().A program that demonstrates this is given as follows −Examplepackage Test; import java.lang.*; class Class1{ } class Class2 extends Class1{ } public class Demo { public static void main(String args[]) { Class1 obj1 = new Class1(); Class2 obj2 = new Class2(); Class c; c = obj2.getClass(); ...

Read More

Get the declared method by name and parameter type in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 1K+ Views

The declared method can be obtained by name and parameter type by using the java.lang.Class.getDeclaredMethod() method. This method takes two parameters i.e. the name of the method and the parameter array of the method.The getDeclaredMethod() method returns a Method object for the method of the class that matches the name of the method and the parameter array that are the parameters.A program that gets the declared method by name and parameter type using the getDeclaredMethod() method is given as follows −Examplepackage Test; import java.lang.reflect.*; public class Demo {    public String str;    private Integer func1() {       ...

Read More

Check whether String is an interface or class in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 685 Views

The method java.lang.Class.isInterface() is used to find if String is an interface or class in Java. This method returns true if String is an interface, otherwise it returns false.A program that demonstrates this is given as follows −Examplepackage Test; import java.lang.*; public class Demo {    public static void main(String[] args) {       Class c = String.class;       boolean interfaceFlag = c.isInterface();       System.out.println("This is an Interface? " + interfaceFlag);    } }OutputThis is an Interface? falseNow let us understand the above program.The isInterface() method is used to find if String is an interface ...

Read More

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

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 190 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 −Examplepackage 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() method. ...

Read More

Load class with forName() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 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 −Exampleimport java.lang.*; public ...

Read More

How to call Private Constructor in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 1K+ 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 −Examplepackage 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 = new ...

Read More

Get all declared fields from a class in Java

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 4K+ 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 −Examplepackage Test; import java.lang.reflect.*; public class Demo {    int i;    char c;    public Demo(int i, char c) {       this.i = i;       ...

Read More

Get the name of a primitive type in Java

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

The getName() method is used to get the names of the entities such as primitive type, interface, class, array class, void etc. that represented by the class objects. These names are returned in the form of a string.A program that gets the name of a primitive type using getName() method is given as follows -Examplepublic class Demo {    public static void main(String[] argv) throws Exception {       String name1 = float.class.getName();       System.out.println(name1);       String name2 = int.class.getName();       System.out.println(name2);       String name3 = char.class.getName();       System.out.println(name3); ...

Read More

Generate Random double type number in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 8K+ Views

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java.util.Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.Declaration - The java.util.Random.nextDouble() method is declared as follows −public float nextDouble()Let us see a program to generate random double type numbers in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       System.out.println(rd.nextDouble()); // displaying a random double value between 0.0 & 1.0    } ...

Read More
Showing 1061–1070 of 4,498 articles
« Prev 1 105 106 107 108 109 450 Next »
Advertisements