Object Oriented Programming Articles

Page 260 of 589

How to compile assert in Java?

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 719 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 972 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 740 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 488 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

Implement Runnable vs Extend Thread in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 2K+ Views

We can create Thread by either by implementing a runnable interface or by extending Thread class. Below are the detailed steps of using both ways to create Thread.Create a Thread by Implementing a Runnable InterfaceIf your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −Step 1As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. ...

Read More

Infinity or exception in Java when divide by 0?

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

Consider the following code snippet where we divide a number by 0.Examplepublic class Tester{    public static void main(String[] args) {       double d = 100;       System.out.println(d/0);    } }OutputInfinityNow consider the following code snippet.Examplepublic class Tester{    public static void main(String[] args) {       int d = 100;       System.out.println(d/0);    } }OutputException in thread "main" java.lang.ArithmeticException: / by zero at Tester.main(Tester.java:5)As you've noted, the Infinity vs ArithmeticException, a different result for similar divide by zero program. The difference lies in floating point arithmetic used in first program and integer arithmetic used in second program.

Read More

Initialize HashSet in Java

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 16K+ Views

A set is a collection which does not allows duplicate values. HashSet is an implementation of a Set. Following are the ways in which we can initialize a HashSet in Java.Using constructor − Pass a collection to Constructor to initialize an HashSet.Using addAll() − Pass a collection to Collections.addAll() to initialize an HashSet.Using unmodifiableSet() − Pass a collection to Collections.unmodifiableSet() to get a unmodifiable Set.Using add() − Using add(element) method of Set.Following is an example of using above ways.ExampleInfinityNow consider the following code snippet.Exampleimport java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class Tester{    public static ...

Read More

instanceof operator vs isInstance method in java

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

isInstance method is equivalent to instanceof operator. The method is used in case of objects are created at runtime using reflection. General practice says if type is to be checked at runtime then use isInstance method otherwise instanceof operator can be used. See the example below −Examplepublic class Tester{    public static void main(String[] args) throws ClassNotFoundException {       Integer i = new Integer(10);       System.out.println(usingInstanceOf(i));       System.out.println(usingIsInstance(i));    }    public static String usingInstanceOf(Object i){       if(i instanceof String){          return "String";       }   ...

Read More

Iterator vs forEach in Java

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 3K+ Views

Collections can be iterated easily using two approaches.Using for-Each loop − Use a foreach loop and access the array using object.Using Iterator − Use a foreach loop and access the array using object.DifferencesConcurrentModificationException − Using for-Each loop, if an object is modified, then ConcurrentModificationException can occur. Using iterator, this problem is elliminated.Size Check − Using for-Each, size check is not required. Using iterator if hasNext() is not used properly, NoSuchElementException can occur.Performance − Performance is similar for both cases.Following is an example of using above ways.Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester {    public static ...

Read More

Jagged Array in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 5K+ Views

Jagged array is a multidimensional array where member arrays are of different size. For example, we can create a 2D array where first array is of 3 elements, and is of 4 elements. Following is the example demonstrating the concept of jagged array.Examplepublic class Tester {    public static void main(String[] args){       int[][] twoDimenArray = new int[2][];       //first row has 3 columns       twoDimenArray[0] = new int[3];       //second row has 4 columns       twoDimenArray[1] = new int[4];       int counter = 0; ...

Read More
Showing 2591–2600 of 5,881 articles
« Prev 1 258 259 260 261 262 589 Next »
Advertisements