![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
1K+ Views
final is a non-access modifier for Java elements. The final modifier is used for finalizing the implementations of classes, methods, and variables. A final instance variable can be explicitly initialized only once.A final instance variable should be initialized at one of the following occasions −At time of declaration.In constructor.In instance block.Compiler will throw error, it a final variable is not initialized at all using any of the above methods. Following examples showcases example of instance variables as final.Example Live Demopublic class Tester{ final int A = 1; final int B;{ B = 2; } ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
14K+ 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.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class Tester{ public ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
254 Views
Consider the following code snippet where we divide a number by 0.Example Live Demopublic class Tester{ public static void main(String[] args) { double d = 100; System.out.println(d/0); } }OutputInfinityNow consider the following code snippet.Example Live Demopublic 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.
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
1K+ 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
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
321 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 −Example Live Demopublic 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 ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
484 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 Singleton Live Demoimport 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
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
677 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 Singleton Live Demoimport 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
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
1K+ 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 cloning, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonpublic class Tester{ public static void main(String[] args) throws CloneNotSupportedException { A a = A.getInstance(); A b = (A)a.clone(); System.out.println(a.hashCode()); System.out.println(b.hashCode()); } } ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
209 Views
If a object is no more referenced by a live reference then it becomes eligible for garbage collection. See the example below −Examplepublic class Tester{ public static void main(String[] args) { test(); } public static void test(){ A a = new A(); } } class A {}When test() method complete execution, the a object is no more referenced and is eligible for garbage collection. Java garbage collector will deallocate the object when it runs.To prevent garbage collection, we can create a static reference to an object and then ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
18K+ Views
In order to find occurence of each character in a string we can use Map utility of Java. In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before. Now when a character repeats during insertion as key in Map increase its value by one. Continue this for each character untill all characters of string get inserted.Examplepublic class occurenceOfCharacter { public static void main(String[] args) { String str = "SSDRRRTTYYTYTR"; ... Read More