- Trending Categories
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 593 Articles for Java Programming

712 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

253 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

769 Views
There are three ways to remove an element from a ArrayList in Java.Using remove(index) - This method takes the index of ArrayList and remove the required element from the ArrayList.Using remove(Object) - This method takes the object of ArrayList and remove it from the ArrayList.Using Iterator.remove() - This method removes the element without causing ConcurrentModificationException.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester{ public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); ... Read More

331 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

460 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

897 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

156 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

1K+ Views
The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.This legacy interface has been superceded by Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes such as Vector and Properties, is used by several other API classes, and is currently in widespread use in application code.The methods declared by Enumeration are summarized in the following table −Sr.No.Method & Description1boolean hasMoreElements( )When implemented, it must return true while there are still more elements to ... Read More

15K+ 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

2K+ Views
This articles covers how to encode and decode JSON objects using Java programming language. Let's start with preparing the environment to start our programming with Java for JSON.EnvironmentBefore you start with encoding and decoding JSON using Java, you need to install any of the JSON modules available. For this tutorial we have downloaded and installed JSON.simple and have added the location of json-simple-1.1.1.jar file to the environment variable CLASSPATH.Mapping between JSON and Java entitiesJSON.simple maps entities from the left side to the right side while decoding or parsing, and maps entities from the right to the left while encoding.JSONJavastringjava.lang.Stringnumberjava.lang.Numbertrue|falsejava.lang.Booleannullnullarrayjava.util.Listobjectjava.util.MapOn decoding, ... Read More