Java Archive

Difference between Thread vs Runnable interface in Java

As we discussed in earlier (Link) multithreading article we can create a thread by using two mechanisms: By Extending the Thread class By Implementing the Runnable Interface By using #1 (Extend Thread class) approach we cannot extend another class because java does not support multiple inheritance. On another side if we use #2 Runnable interface

Java Multithreading

Multithreading is a very popular feature of java. By using multithreading we can run a program in concurrent pieces. We can say this in another way that multithreading allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part is called a thread. This is a lightweight process.

Java Daemon Thread

Daemon threads are low priority threads and run in the background. There are many java daemon threads running automatically in background e.g. garbage collection, finalizer etc. You can see these threads by using jconsole in your system. Java Source Code: package com; public class JavaDaemonThreads extends Thread {                   public void run() {                            

Java Volatile Keyword

To make class thread safe we used volatile. By making a class thread-safe instance of class and method can use by multiple threads at a time. If more than one threads are using any class instance and doing modification in class variables. In that case, every thread has its own memory space so any change

Transient variable in java

Transient variable used in case of non-serialization. If we are using serialization and any variable which we don’t want to serialize than we use transient. At the time of serialization if we don’t want to save the value of some variables in a file, then we use transient keyword. When JVM see transient keyword, it ignores the

Java Nested Interface

An interface inside the class or interface is called nested interface. We can’t access nested interface directly, we can access by using outer interface or class. Nested interface is static by default. Who use Nested Interface? To increase encapsulation. Logically grouping of interfaces. To create more readable and maintainable code. Example Source: interface AI{ interface

JAVA Lambda inner class

We can also use Lambda inner class in place of anonymous inner class. Lambda inner class has some limitation if your inner class has more than one method than we can’t use lambda inner class. public class A { // Some code or main method B b = () -> { }; } @FunctionalInterface interface

JAVA Nested static Class

Static inner class is a nested class. This class can’t access non static member variables and methods. We can access this class by outer class name not by an object. This class can access static member variables and methods of outer class including private. Source Code: package com;  public class A {                 static class

Java Member inner class

A non-static class inside a class is called member inner class. This class should not inside a method. Source Code: package com; public class A { class B{ } } Example Java Code: package com; public class InnerClass { public static void main(String args) { InnerMemberClass innerMemberClass = new InnerMemberClass(); innerMemberClass.setPersonName("Siddhant");   InnerMemberClass.B obj =

Java Anonymous inner class

Java anonymous classes are without name. They are local classes. We create anonymous classes to override a method of interface or class. Anonymous inner class by using a class: Java Source Code: package com; abstract class A { abstract void display(); } public class B { public static void main(String args) { A a =

Java Inner Classes

Java inner class is a class inside a class or interface. To handle user interface events, we should know how to use inner classes, because the event-handling programming makes extensive use of them. Sample Source Code: package com; public class A {                 class B{                 } } Advantages to create an inner class: By

Difference between the instanceof operator and isInstance () method

Difference between the instanceof operator and isInstance () method: Both uses for the same purpose means instanceof operator and isInstance () method. We can check the class of the object by these two. If we want to check the class of object dynamically, in this case, isInstance () method will work. There is no way we can do this by the instanceof operator.

Difference between arithmetic + (Plus) operator and ++ (Increment) operator.

Difference between arithmetic + (a+1) operator and ++ (a++) operator. Here is some example to understand both operators. Source 1: package com; public class DiffBtwArthOperAndIncreOperator { public static void main(String args) { byte b = 2; b = b+1; System.out.println("Print a value "+b); } }   Response 1: Compile Time Error: Exception in thread "main"

Sum of the Even Numbers in an Array

Sum of the Even Numbers in an Array: Source: package com; public class EvenNumberSum { public static void main(String args) { int intArr = { 4, 7, 1, 2, 8, 9, 56, 12, 1, 34, 2, 56 }; int sum = 0; for (int i = 0; i <= intArr.length-1; i++) { if (intArr[i] %

Example Java Program for Odd and Even Numbers

Please see below java program to display Odd and even numbers between two numbers. Source: package com; public class OddNumbers { public static void main(String args) { int i; System.out.println( "######################### Odd numbers between any two numbers (In following example two numbers are 1 and 100###############)"); for (i = 1; i <= 100; i++) {

How to sort array by single for loop in java

Please see below example to sort number types of arrays by using single for loop. The code should be self-explanatory. Example Code: import java.util.Arrays; public class SortArray { public static void main(String args) { int intArray = new int { 67, 6, 7,88, 1, 6,7,3, 2, 4, 5, 11, 22 }; System.out.println(“**************int Array before sorting************”); for

How to Sort an Array in java

Please see below example to sort different types of arrays in java. The code should be self-explanatory. Example Code: import java.util.Arrays; import java.util.Collections; public class SortArray { public static void main(String args) { int intArray = new int { 67, 43, 88, 1, 3, 2, 4, 5, 11, 22 }; String strArray = new String

How can we compare String creation performance?

In this article we will discuss performance wise what is the best way to create String object. There are two ways to create String object: By string literal String str = “10Article”; When we create string literal then JVM checks Heap string constant pool first. If the string already exists in the pool, a reference

How to reverse each word of string?

How to reverse each word of string. Problem Statement: I have a string with multiple words. I want to reverse each word of this string. Reverse each word of string. Source Code: package com; public class Test { public static void main(String args) { String str1 = "Java and J2EE Technology Servlet"; String str2 =

How to count the occurrences of a Sub String in another String?

How to count the occurrences of a Sub String in the given String > String search Problem Statements: I have a long String, I want to find out the occurrence of sub string in this. How to find out a duplicate sub string in a long String. I want to print multiple occurrence count of