Programming Articles - Page 2912 of 3363

Get Canonical Name for a class in Java

Nancy Den
Updated on 29-Jun-2020 06:44:18

487 Views

The canonical name of a class can be obtained using the java.lang.Class.getCanonicalName() method. This method returns the canonical name of the underlying class and returns null if there is no canonical name for the underlying class.A program that demonstrates the getCanonicalName() method to obtain the canonical name is given as follows −Example Live Demopackage Test; import java.lang.*; public class Demo {    public static void main(String[] args) {       Demo obj = new Demo();       Class c = obj.getClass();       System.out.println("The canonical name of the underlying Class is: " + c.getCanonicalName());    } }OutputThe canonical ... Read More

Get the class name for various objects in Java

Krantik Chavan
Updated on 29-Jun-2020 06:45:23

381 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. The getPackage() method gets the package for the given class.A program that gets the class name for various objects is given as follows −Example Live Demopackage Test; import java.io.IOException; import java.util.HashMap; public class Demo {    public static void main(String args[]) throws IOException {       Object obj = "string";       System.out.println("The class name is: " + obj.getClass().getName());       ... Read More

Get the Component Type of an Array Object in Java

Anvi Jain
Updated on 25-Jun-2020 13:02:42

2K+ Views

In order to get the component type of an Array Object in Java, we use the getComponentType() method. The getComponentType() method returns the Class denoting the component type of an array. If the class is not an array class this method returns null.Declaration − The java.lang.Class.getComponentType() method is declared as follows -public Class getComponentType()Let us see a program to the get the component type of an Array Object in Java -Example Live Demopublic class Example {    public static void main(String[] args) {       int[] array = new int[] {1, 2, 3};       // obtain the Class ... Read More

Working with Array.setInt to fill an array in Java

Smita Kapse
Updated on 25-Jun-2020 13:04:06

160 Views

In order to fill an array in Java, we use the Array.setInt() method. The java.lang.reflect.Array.setInt(Object array, int index, int value) method assigns the value of the component with a particular index of the given array object to the specified integer value.Declaration − The java.lang.reflect.Array.setInt(Object array, int index, int value) is declared as follows -public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionLet us see a program to fill an array in Java using the Array.setInt() method to fill an array in Java -Example Live Demoimport java.lang.reflect.Array; public class Example {    public static void main(String[] args) {   ... Read More

Create integer array with Array.newInstance in Java

Nancy Den
Updated on 25-Jun-2020 13:13:53

262 Views

The java.lang.reflect.Array.newInstance(Class componentType, int length) method forms a new array with the component type and length as specified in the argumentsDeclaration − The java.lang.reflect.Array.newInstance(Class componentType, int length) method is declared as follows -public static Object newInstance(Class componentType, int length) throws IllegalArgumentException, NegativeArraySizeExceptionLet us see a program to create an integer array with Array.newInstance with Java Reflection -Example Live Demoimport java.lang.reflect.Array; public class Example {    public static void main(String[] args) {       int[] arr = (int[]) Array.newInstance(int.class, 3); // creates a new array       Array.set(arr, 0, 5);       Array.set(arr, 1, 9);       Array.set(arr, ... Read More

Create array with Array.newInstance with Java Reflection

Krantik Chavan
Updated on 25-Jun-2020 13:14:40

172 Views

The java.lang.reflect.Array.newInstance(Class componentType, int length) method forms a new array with the component type and length as specified in the argumentsDeclaration − The java.lang.reflect.Array.newInstance(Class componentType, int length) method is declared as follows −public static Object newInstance(Class componentType, int length) throws IllegalArgumentException, NegativeArraySizeExceptionLet us see a program to create array with Array.newInstance with Java Reflection −Example Live Demoimport java.lang.reflect.Array; public class Example {    public static void main(String[] args) {       String[] arr = (String[]) Array.newInstance(String.class, 3); // creates a new array       Array.set(arr, 0, "A");       Array.set(arr, 1, "B");       Array.set(arr, 2, "C"); ... Read More

Determining If an Object Is an Array in Java

Anvi Jain
Updated on 29-Jun-2020 06:38:56

6K+ Views

In order to determine if an object is an Object is an array in Java, we use the isArray() and getClass() methods.The isArray() method checks whether the passed argument is an array. It returns a boolean value, either true or falseSyntax - The isArray() method has the following syntax -Array.isArray(obj)The getClass() method method returns the runtime class of an object. The getClass() method is a part of the java.lang.Object class.Declaration − The java.lang.Object.getClass() method is declared as follows −public final Class getClass()The getClass() method acts as the intermediate method which returns an runtime class of the object, which enables the terminal ... Read More

How to schedule tasks in Java to run for repeated fixed-rate execution, beginning after the specified delay

Smita Kapse
Updated on 29-Jun-2020 06:39:50

469 Views

One of the methods of the Timer class is void scheduleAtFixedRate(TimerTask task, long delay, long period). This method schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.In fixed-rate execution, each execution is scheduled with respect to the scheduled run time of the initial execution. Fixed-rate execution is apt for repetitive activities that are respond to absolute time. Likewise, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain in sync.Declaration − The java.util.Time.scheduleAtFixedRate(TimerTask task, long delay, long period) method is declared as follows −public void scheduleAtFixedRate(TimerTask task, long delay, long period)Here, task is ... Read More

Get Array Dimensions in Java

Nancy Den
Updated on 29-Jun-2020 06:41:04

2K+ Views

In order to get Array Dimensions in Java, we use the getClass(), isArray() and getComponentType() methods with decision making in combination with iterative statements.The getClass() method method returns the runtime class of an object. The getClass() method is a part of the java.lang.Object class.Declaration − The java.lang.Object.getClass() method is declared as follows −public final Class getClass()The isArray() method checks whether the passed argument is an array. It returns a boolean value, either true or falseSyntax - The isArray() method has the following syntaxArray.isArray(obj)The getComponentType() method returns the Class denoting the component type of an array. If the class is not an ... Read More

Get array upperbound in Java Multidimensional Arrays

Krantik Chavan
Updated on 25-Jun-2020 13:18:03

614 Views

In order to get the array upperbound of a multidimensional array, we use the length() method. For a 2D array, the length() method returns the number of rows. We can access the number of columns using the array_name[0].length method.Let us see a program to get the array upperbound in Java Multidimensional arraysExample Live Demopublic class Example {    public static void main(String args[]) {       String[][] str = new String[5][10];       System.out.println("1st dimension : " + str.length); // displays the number of rows       System.out.println("2nd dimension : " + str[0].length); // displays the number of columns    } }Output1st dimension : 5 2nd dimension : 10

Advertisements