Found 4336 Articles for Java 8

Check whether String is an interface or class in Java

Nancy Den
Updated on 25-Jun-2020 12:27:18

474 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 −Example Live Demopackage 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 ... Read More

List the Interfaces that an Interface Extends in Java

Krantik Chavan
Updated on 25-Jun-2020 12:27:52

298 Views

The interfaces that are implemented by an interface that is represented by an object can be determined using the java.lang.Class.getInterfaces() method. This method returns an array of all the interfaces that are implemented by the interface.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.*; import java.util.*; public class Demo {    public static void main(String[] args) {       listInterfaces(java.util.List.class);    }    public static void listInterfaces(Class c) {       System.out.println("The interface is: " + c.getName());       Class[] interfaces = c.getInterfaces();       System.out.println("The Interfaces are: " + Arrays.asList(interfaces)); ... Read More

Demonstrate getting the immediate superclass information in Java

Anvi Jain
Updated on 30-Jul-2019 22:30:24

108 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 −Example Live Demopackage 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
Updated on 25-Jun-2020 12:29:02

727 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 −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    public String str;    private Integer func1() {     ... Read More

Compare two float arrays in a single line in Java

Nancy Den
Updated on 25-Jun-2020 12:29:39

145 Views

Two float arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two float arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new float[] { 1.5F, 8.3F, 7.6F }, new float[] { 1.5F, 8.3F, 7.6F });       System.out.println("The two float arrays are equal? ... Read More

Get class from an object in Java

Krantik Chavan
Updated on 25-Jun-2020 12:30:16

203 Views

The runtime class of an object can be obtained using the java.lang.Object.getClass(). Also, the getName() method is used to get the name of the class.A program that demonstrates getting the class of an object using the getClass() method is given as follows −Example Live Demopublic class Demo {    public static void main (String [] args) {       Integer integer = new Integer(10);       Class c = integer.getClass();       System.out.println ("The class is: " + c.getName());       c = new Demo().getClass();       System.out.println ("The class is: " + c.getName());    } ... Read More

Java Program to implement Binary Search on an array

George John
Updated on 25-Jun-2020 12:34:59

306 Views

Binary search can be implemented on an array by using the method java.util.Arrays.binarySearch(). This method returns the index of the required 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.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int arr[] = { 3, 9, 1, 6, 4};       Arrays.sort(arr);       System.out.print("The sorted array is: "); ... Read More

How to extend the size of an array in Java

Ankith Reddy
Updated on 25-Jun-2020 12:36:05

415 Views

An array has a fixed number of values and its size is defined when it is created. Therefore, the size of the array cannot be changed later. To solve this problem, an ArrayList should be used as it implements a resizable array using the List interface.A program that demonstrates ArrayList in Java is given as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Melon");       aList.add("Orange");       aList.add("Mango");       aList.add("Grapes");     ... Read More

Search elements in a sorted object array in Java

Arjun Thakur
Updated on 25-Jun-2020 12:38:00

152 Views

An element can be searched in a sorted object array in Java by using the methodjava.util.Arrays.binarySearch(). This method returns the index of the required 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. A program that searches the required element in a sorted object array is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String str[] = { "P", "M", "A", "T", "D"};     ... Read More

Sort arrays of objects in Java

Chandu yadav
Updated on 25-Jun-2020 12:39:27

405 Views

An array of objects can be sorted using the java.util.Arrays.sort() method with a single argument required i.e. the array to be sorted. A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = new String[]{"apple", "orange", "mango", "guava", "melon"};       int n = str.length;       System.out.println("The original array of strings is: ");       for (int i = 0; i < n; i++) {          System.out.println(str[i]);       }     ... Read More

Advertisements