Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 107 of 450
Convert array to generic list with Java Reflections
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 MoreDemonstrate getting the immediate superclass information in Java
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 MoreGet the declared method by name and parameter type in Java
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 MoreCheck whether String is an interface or class in Java
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 MoreProve that the interface for a primitive type is an empty array in Java
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 MoreLoad class with forName() method in Java
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 MoreHow to call Private Constructor in Java
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 MoreGet all declared fields from a class in Java
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 MoreGet the name of a primitive type in Java
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 MoreGenerate Random double type number in Java
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