Found 9326 Articles for Object Oriented Programming

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

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

201 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 25-Jun-2020 14:03:31

195 Views

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.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       float f_arr[] = { 5.2f, 9.5f, 6.5f, 2.8f, 3.6f };       Arrays.sort(f_arr);       System.out.print("The sorted array ... Read More

Generate Random Long type numbers in Java

Arjun Thakur
Updated on 25-Jun-2020 14:04:27

4K+ Views

In order to generate Random long type numbers in Java, we use the nextLong() method of the java.util.Random class. This returns the next random long value from the random generator sequence.Declaration − The java.util.Random.nextLong() method is declared as follows −public long nextLong()Let us see a program to generate random long 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.nextLong()); // displaying a random long value    } }Output-4787108556148621714Note - The output may vary on Online compilers.Read More

Display the package name of a class in Java

Chandu yadav
Updated on 25-Jun-2020 14:05:00

5K+ 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. 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.Date; public class Main {    public static void main(String[] args) {       Date d = new Date();       Package p = d.getClass().getPackage();       String pName = p.getName();       System.out.println("The package name is: " + pName);    } }OutputThe package name is: ... Read More

Get the list of all the public methods in Java

George John
Updated on 25-Jun-2020 14:05:34

5K+ Views

A list of all the public methods of a class or interface that is represented by an object are provided using the method java.lang.Class.getMethods(). The public methods include that are declared by the class or interface and also those that are inherited by the class or interface.Also, the getMethods() method returns a zero length array if the class or interface has no public 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 Main {    public static void main(String[] argv) ... Read More

Get the list of all the declared methods in Java

Ankith Reddy
Updated on 25-Jun-2020 14:07:31

825 Views

The list of all the declared methods can be obtained 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 Main {    public static void main(String[] argv) throws Exception {       Class c = java.lang.String.class; ... Read More

Java Program to fill elements in a char array

Arjun Thakur
Updated on 25-Jun-2020 14:08:07

604 Views

Elements can be filled in a char array using the java.util.Arrays.fill() method. This method assigns the required char value to the char 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 {       char[] charArray = new char[5];       char charValue = 'A';       Arrays.fill(charArray, charValue);       System.out.println("The char array content is: " + Arrays.toString(charArray)); ... Read More

Compare two double arrays in a single line in Java

Chandu yadav
Updated on 25-Jun-2020 14:08:45

130 Views

Two double 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 double 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 double[] { 7.5D, 9.2D, 1.8D }, new double[] { 7.5D, 9.2D, 1.8D });       System.out.println("The two double arrays are equal? ... Read More

Swap elements of ArrayList with Java collections

Chandu yadav
Updated on 25-Jun-2020 14:31:53

8K+ Views

In order to swap elements of ArrayList with Java collections, we need to use the Collections.swap() method. It swaps the elements at the specified positions in the list.Declaration −The java.util.Collections.swap() method is declared as follows −public static void swap(List list, int i, int j)where i is the index of the first element to be swapped, j is the index of the other element to be swapped and list is the list where the swapping takes place.Let us see a program to swap elements of ArrayList with Java collections −Example Live Demoimport java.util.*; public class Example {    public static void ... Read More

Shuffle elements of ArrayList with Java Collections

George John
Updated on 25-Jun-2020 14:32:41

3K+ Views

In order to shuffle elements of ArrayList with Java Collections, we use the Collections.shuffle() method. The java.util.Collections.shuffle() method randomly permutes the list using a default source of randomness.Declaration −The java.util.Collections.shuffle() method is declared as follows −public static void shuffle(List list)Let us see a program to shuffle elements of ArrayList with Java Collections −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       ArrayList list = new ArrayList();       list.add(1);       list.add(2);       list.add(7);       list.add(8);       list.add(3);       list.add(9); ... Read More

Advertisements