Learn Java Programming

Java Basics

Java Synchronized Method not working – Troubleshoot

Synchronized method is not working in java because the lock on the synchronised method is not attached to a single object. When the Java method is synchronised with the synchronised keyword, the threads are synchronised to access the Java method If the java synchronised method lock is not properly attached

Synchronized Block not working in Java – Troubleshoot

Synchronized block is not working in java because the lock on the synchronised block is not attached to a single object. When the Java code block is synchronised with the synchronised keyword, the threads are synchronised to access the Java code block. If the java synchronised block lock is not

No enclosing instance of type is accessible

The java No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type (e.g. x.new A() where x is an instance of ). exception happens when an instance is created for an inner class in the outer class static method. The static method attempts

Local variable name defined in an enclosing scope must be

The Local variable name defined in an enclosing scope must be final or effectively final exception occurs where the local variable used in the lamda expression is not a final or effectively final variable. In the lamda expression, the local variable can only be used if the variable is final

Remove Element from an Array in Java

Removing an element from the java array is done by locating the item index and shifting the rest of the items to the index position. The Java array can’t be modified. We can’t remove a specific item from the Java array. The alternative is to shift the rest of the

Convert String to Int in Java

How to Convert String to Int in Java – we can convert String to int value using Integer.parseInt() method. We can also convert String to Integer object using Integer.valueOf() method. You can use the Integer.parseInt() function to get the original int value from String. The Integer.valueOf() method returns the Integer

Generate Random Integers within a Specific range in Java

Java generate random number in range – Java Random int value generation classes such as ThreadLocalRandom, Random, and SplittableRandom are used to generate random integers within a specific range in Java. It allows you to generate pseudo random numbers between a certain range in Java. The classes with package names

How to Iterate through a HashMap in Java

In Java, there are several methods for iterating over a HashMap. A for loop, foreach, and iterators are the best ways to traverse a HashMap. The lambda expression is used to simplify iterator code when iterating a HashMap with the stream apis in Java 8 and Java 10. You can

How do I convert a String to an int in

To convert a String to an int in Java, you can use the Integer.parseInt() method. In Java, we can use the Integer.valueOf() method to convert a String to an Integer. We can use the Integer.decode() method to convert a String containing any decimal, such as hexadecimal or octal, into an

How to declare and initialize an array in Java

Array Declaration in Java The declaration of a Java array is similar to that of any other variable declaration in Java. The array is specified with square brackets, either with datatype or variable. Array Initialization in Java The declared array is initialised with the new keyword, followed by the datatype