Found 593 Articles for Java Programming

Inter thread communication in Java

Arjun Thakur
Updated on 23-Jun-2020 15:17:00
If you are aware of interprocess communication then it will be easy for you to understand interthread communication. Interthread communication is important when you develop an application where two or more threads exchange some information.There are three simple methods and a little trick which makes thread communication possible. All the three methods are listed below −Sr.No.Method & Description1public void wait()Causes the current thread to wait until another thread invokes the notify().2public void notify()Wakes up a single thread that is waiting on this object's monitor.3public void notifyAll()Wakes up all the threads that called wait( ) on the same object.These methods have ... Read More

instanceof operator vs isInstance method in java

Chandu yadav
Updated on 23-Jun-2020 15:18:19
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 −Example Live Demopublic 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

instanceof Keyword in Java

George John
Updated on 23-Jun-2020 15:18:47
This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as −( Object reference variable ) instanceof (class/interface type)If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is an example -Example Live Demopublic class Test {    public static void main(String args[]) {       String name = "James"; // following will return true since name is ... Read More

instance variable hiding in Java

Ankith Reddy
Updated on 23-Jun-2020 15:20:01
A local variable of same name will hide the instance variable. In order to use the instance variable, we should use the this operator. See the example below −Example Live Demopublic class Tester{    int a = 1;    public static void main(String[] args) {       Tester t = new Tester();       t.show();       t.show1();    }    public void show(){       int a = 2;       System.out.println(a);    }    public void show1(){       int a = 3;       System.out.println(this.a);    } }Output2 1

Instance variable as final in Java

Arjun Thakur
Updated on 23-Jun-2020 15:20:34
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

Inner class in Java

George John
Updated on 23-Jun-2020 14:52:48
Nested ClassesIn Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.SyntaxFollowing is the syntax to write a nested class. Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.class Outer_Demo { class Inner_Demo { } }Nested classes are divided into two types −Non-static nested classes − These are the non-static members of a class.Static nested classes ... Read More

Initialize HashSet in Java

Ankith Reddy
Updated on 23-Jun-2020 15:00:21
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

Infinity or exception in Java when divide by 0?

Arjun Thakur
Updated on 23-Jun-2020 15:00:54
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.

Implement Runnable vs Extend Thread in Java

Chandu yadav
Updated on 23-Jun-2020 15:04:27
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

How to swap or exchange objects in Java?

George John
Updated on 23-Jun-2020 15:05:26
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
Previous 1 ... 3 4 5 6 7 ... 60 Next
Advertisements