Posts

Showing posts with the label Java

Java Synchronized Keyword

The synchronized is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the synchronized keyword indicates the following :   ** The synchronized keyword may be applied to statement block or to a method. **  The synchronized keyword provides the protection for the crucial sections   that are required only to be executed by a single thread once at a time. **   The synchronized keyword avoids a critical code from being executed by more than one thread at a time. Its restricts other threads to concurrently access a resource. ** If   the synchronized keyword is applied to a static method, as we will show it with a class having a method SyncStaticMethod through an example below, the entire class get locked while the method under execution and control of a one thread at a time. **   When the synchronized keyword is applie...

Java Singleton Class

The Singleton class’s default constructor is made private,  which prevents the direct instantiation of the object by other classes using the new keyword. The Singleton is a useful Design Pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created.

SwapNumbersWithoutThirdVariable - JAVA

public class SwapNumbersWithoutThirdVariable { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); //add both the numbers and assign it to first num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("After Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); } }