Found 33676 Articles for Programming

Display the current method name in Java

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

2K+ Views

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java.lang.StackTraceElement.getMethodName() method.A program that demonstrates this is given as follows −Example Live Demopublic class Demo {    public static void main(String args[]) {       System.out.println       ("The method name is: " + new Exception().getStackTrace()[0].getMethodName());    } }OutputThe method name is: mainNow let us understand the above program.The method getMethodName() is used to obtain the current method name that contains the execution point that is represented by the current stack trace element. This is printed using ... Read More

Call methods of an object using reflection in Java

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

771 Views

The methods of an object can be called using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included.Also, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods 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.Method; class ClassA {    private String name = "John";    public String returnName() {       return name;    } ... Read More

Java Program to fill elements in an int array

Anvi Jain
Updated on 25-Jun-2020 12:06:00

3K+ Views

Elements can be filled in an int array using the java.util.Arrays.fill() method. This method assigns the required int value to the int array in Java. The two parameters required are the array name and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       int[] intArray = new int[5];       int intValue = 9;       Arrays.fill(intArray, intValue);       System.out.println("The int array content is: " + Arrays.toString(intArray)); ... Read More

Generate Random double type number in Java

Smita Kapse
Updated on 25-Jun-2020 12:06:23

7K+ 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 −Example Live Demoimport 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

Display the declared methods of java.lang.Math

Nancy Den
Updated on 25-Jun-2020 12:07:09

142 Views

The methods of the java.lang.Math class can be listed using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included. Also, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods 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.Method; public class Demo {    public static void main(final String[] args) {       final Method[] methods = Math.class.getDeclaredMethods(); ... Read More

List methods of a class using Java Reflection

Krantik Chavan
Updated on 25-Jun-2020 12:08:15

2K+ Views

The methods of a class can be listed using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included.Also, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods 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[] args) {       Class c = Thread.class;       ... Read More

Get the name of a primitive type in Java

Anvi Jain
Updated on 25-Jun-2020 12:08:55

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 -Example Live Demopublic 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();       ... Read More

Java Program to fill elements in a byte array

Chandu yadav
Updated on 25-Jun-2020 12:10:05

253 Views

Elements can be filled in a byte array using the java.util.Arrays.fill() method. This method assigns the required byte value to the byte array in Java. The two parameters required are the array name and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       byte[] byteArray = new byte[5];       byte byteValue = 5;       Arrays.fill(byteArray, byteValue);       System.out.println("The byte array content is: " + Arrays.toString(byteArray)); ... Read More

Create new instance of an Array with Java Reflection Method

George John
Updated on 25-Jun-2020 12:11:22

477 Views

A new instance of an Array can be created using the java.lang.reflect.Array.newInstance() method. This method basically creates a new array with the required component type as well as length.A program that demonstrates the creation of an array using the Array.newInstance() method is given as follows −Example Live Demoimport java.lang.reflect.Array; public class Demo {    public static void main (String args[]) {       int arr[] = (int[])Array.newInstance(int.class, 5);       Array.set(arr, 0, 5);       Array.set(arr, 1, 1);       Array.set(arr, 2, 9);       Array.set(arr, 3, 3);       Array.set(arr, 4, 7);     ... Read More

Initialize an Array with Reflection Utilities in Java

Ankith Reddy
Updated on 25-Jun-2020 12:12:24

212 Views

An array can be initialized using the method java.util.Arrays.fill() that is a utility method provided in the java.util.Arrays class. This method assigns the required value to all the elements in the array or to all the elements in the specified range.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] arg) {       int[] arr = {2, 5, 8, 1, 9};       System.out.print("The array elements are: ");       for (int i = 0; i < arr.length; i++) {          System.out.print(arr[i] ... Read More

Advertisements