Programming Articles - Page 2914 of 3363

Get the fully-qualified name of a class in Java

George John
Updated on 25-Jun-2020 13:48:16

12K+ Views

A fully-qualified class name in Java contains the package that the class originated from. An example of this is java.util.ArrayList. The fully-qualified class name can be obtained using the getName() method.A program that demonstrates this is given as follows −Example Live Demopublic class Demo {    public static void main(String[] argv) throws Exception {       Class c = java.util.ArrayList.class;       String className = c.getName();       System.out.println("The fully-qualified name of the class is: " + className);    } }OutputThe fully-qualified name of the class is: java.util.ArrayListNow let us understand the above program.The getName() method is used ... Read More

Get the unqualified name of a class in Java

Ankith Reddy
Updated on 25-Jun-2020 13:49:24

738 Views

A qualified class name in Java contains the package that the class originated from. In contrast to this, the unqualified class name contains only the class name without any package information. A program that gets the unqualified name of a class is given as follows:Example Live Demopublic class Demo {    public static void main(String[] argv) throws Exception {       Class c = java.util.ArrayList.class;       String className = c.getName();       System.out.println("The qualified class name is: " + className);       if (className.lastIndexOf('.') < 0) {          className = className.substring(className.lastIndexOf('.') + 1);   ... Read More

Get the Name of a Member Object in Java

Arjun Thakur
Updated on 25-Jun-2020 13:50:08

5K+ Views

The getName() method is used to get the names of the entities such as interface, class, array class, void etc. that are represented by the class objects. These names are returned in the form of a string.A program that gets the name of the member objects using getName() method is given as follows −Example Live Demoimport java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main {    public static void main(String[] argv) throws Exception {       Class c = java.lang.Integer.class;       Method m = c.getMethods()[0];       Field f = c.getFields()[0];       Constructor cons = ... Read More

Listing the Modifiers of a Class Object in Java

Chandu yadav
Updated on 25-Jun-2020 13:50:34

200 Views

The modifiers for a class or an interface are returned by the java.lang.Class.getModifiers() method. These modifiers are encoded as an integer and they consist of the JVM’s constants for public, private, protected, final, abstract, static and interface.A program that demonstrates this is given as follows −Example Live Demoimport java.lang.reflect.Modifier; public class Main {    public static void main(String[] argv) throws Exception {       int modifiers = String.class.getModifiers();       String modifierString = Modifier.toString(modifiers);       System.out.println("The Modifier is: " + modifierString);    } }OutputThe Modifier is: public finalNow let us understand the above program.The method getModifiers() is ... Read More

Get super class of an object in Java

George John
Updated on 25-Jun-2020 13:52:19

764 Views

The immediate superclass of any entity such as an object, class, primitive type, interface etc. can be obtained using the method java.lang.Class.getSuperclass(). This method contains no parameters.A program that demonstrates this is given as follows −Example Live Demopublic class Main {    public static void main(String[] args) {       Object obj1 = new String("Hello");       Object obj2 = new Integer(15);       Class c1 = obj1.getClass().getSuperclass();       System.out.println("Super Class = " + c1);       Class c2 = obj2.getClass().getSuperclass();       System.out.println("Super Class = " + c2);    } }OutputSuper Class = ... Read More

Check for the availability of a package in Java

Ankith Reddy
Updated on 25-Jun-2020 13:59:35

731 Views

The availability can be checked using the method java.lang.Class.forName(). The class object associated with the class with the given string name can be returned using the method java.lang.Class.forName(String name, boolean initialize, ClassLoader loader), using the class loader that is used to load the class.A program that demonstrates this is given as follows −Example Live Demopublic class Main {    public static void main(String args[]) {       System.out.println(Availability("java.lang.String"));    }    public static boolean Availability(String name) {       boolean flag = false;       try {          Class.forName(name, false, null);         ... Read More

Find the Package of an Object in Java

Arjun Thakur
Updated on 25-Jun-2020 14:00:45

1K+ Views

The package for an object of a class can be obtained using the getPackage() method with the help of the class loader of the class. If there is no package object created by the class loader of the class, then null is returned.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.LinkedList; import java.util.HashSet; public class Main {    public static void main(String[] args) {       System.out.println("The package is: " + new ArrayList().getClass().getPackage().getName());       System.out.println("The package is: " + new LinkedList().getClass().getPackage().getName());       System.out.println("The package is: " + new HashSet().getClass().getPackage().getName());   ... Read More

What would getPackage() return for a primitive or array in unnamed package in Java?

Chandu yadav
Updated on 25-Jun-2020 14:01:22

173 Views

The package for a class can be obtained using the java.lang.Class.getPackage() method with the help of the class loader of the class.The getPackage() method returns null for a primitive or array in unnamed package. A program that demonstrates this is given as follows −Example Live Demopublic class Main {    public static void main(String[] argv) throws Exception {       Package pack1 = int.class.getPackage();       System.out.println(pack1);       Package pack2 = int[].class.getPackage();       System.out.println(pack2);    } }Outputnull nullNow let us understand the above program.The getPackage() method is used to obtain the package for the class. ... Read More

What would getPackage() return for a class in unnamed package in Java?

George John
Updated on 25-Jun-2020 14:02:36

301 Views

The package for a class can be obtained using the java.lang.Class.getPackage() method with the help of the class loader of the class.The getPackage() method returns null for a class in unnamed package. A program that demonstrates this is given as follows −Example Live Democlass Class1 {    public class Main {       public static void main(String[] argv) throws Exception {       Class c = Class1.class;       System.out.println(c.getPackage());    } }OutputnullNow let us understand the above program.The getPackage() method is used to obtain the package for the class. However, the getPackage() method returns null for the ... Read More

Java program to implement binary search on float array

Ankith Reddy
Updated on 23-Jan-2025 23:06:21

341 Views

In this article, we will learn how to implement binary search on a float array in Java using the Arrays.binarySearch() method. Binary search on a float array can be implemented by using the method java.util.Arrays.binarySearch(). This method returns the index of the required float element if it is available in the array, otherwise, it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array. Java.util.Arrays.binarySearch(). methodThe Arrays.binarySearch() method in Java searches for a specified element in a sorted array. It returns the index of the element ... Read More

Advertisements