Java Articles

Page 75 of 450

Can we extend interfaces in Java? Explain?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Just like classes you can extend one interface from another using the extends keyword as shown below −interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations{    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }But, when you implement the sub-class you need to provide body for the abstract methods in both interfaces.ExampleIn the following example we have created two interfaces ...

Read More

Write a program to convert an array to String in Java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 413 Views

The Arrays class of the java.util package provides a method named toString() this method accepts an array value (of any type) and returns a String.ExampleFollowing Java program accepts various arrays from the user, converts them into String values and prints the results.import java.util.Arrays; import java.util.Scanner; public class ObjectArrayToStringArray {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       //Integer array to String       System.out.println("Enter 5 integer values: ");       int intArray[] = new int[5];       for(int i=0; i

Read More

What are copy constructors in Java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 6K+ Views

Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.writing a copy constructorUsually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values.int name; int age; public Student(String name, int age){ this.name = name; this.age ...

Read More

Display nanoseconds with Java Date and Time Conversion Character

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To display nanoseconds, use the ‘N’ Date and Time conversion specifier.System.out.printf("Nanoseconds = %tN", d);Exampleimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Nanoseconds = %tN", d);    } }OutputNanoseconds = 092000000

Read More

How to compile assert in Java?

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 727 Views

In order to compile assert in Java, we simply set the boolean expression as false.Let us see an example program −Examplepublic class Example {    public static void main(String[] args) {       assert false;       System.out.println("Compiled and executed successfully!!!");    } }OutputCompiled and executed successfully!!!

Read More

How to prevent Reflection to break a Singleton Class Pattern?

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 978 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singletonimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();       ...

Read More

How to prevent Reflection to break a Singleton Class Pattern?

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 978 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singletonimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();       ...

Read More

How to prevent Serialization to break a Singleton Class Pattern?

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 746 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using serialization, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{    public static void main(String[] args)    throws ClassNotFoundException, IOException{       A a = A.getInstance();       A b = ...

Read More

How to prevent Serialization to break a Singleton Class Pattern?

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 746 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using serialization, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{    public static void main(String[] args)    throws ClassNotFoundException, IOException{       A a = A.getInstance();       A b = ...

Read More

How to swap or exchange objects in Java?

George John
George John
Updated on 11-Mar-2026 491 Views

Java uses call by value while passing parameters to a function. To swap objects, we need to use their wrappers. See the example below −Examplepublic class Tester{    public static void main(String[] args) {       A a = new A();       A b = new A();       a.value = 1;       b.value = 2;       //swap using objects       swap(a, b);       System.out.println(a.value +", " + b.value);       Wrapper wA = new Wrapper(a);       Wrapper wB = new Wrapper(b); ...

Read More
Showing 741–750 of 4,498 articles
« Prev 1 73 74 75 76 77 450 Next »
Advertisements