Atomic Variables in Java

Atomic Variables in Java
In Java, We have a package java.util.concurrent.atomic. Atomic variables are the one which supports multiple operations on a single variable.

Atomic class has get() method, set() method and also compareandset() method.

Let us see some examples of using Atomic Integer, which is from package java.util.concurrent.AtomicInteger.

We are going to see,

get() – Getting the atomic variable value
set() – Setting the atomic variable value
compareAndSet() – Check the atomic variable value and set it to a new value
addAndGet() – This is similar to variable++
getAndAdd() – This is similar to ++variable
getAndIncrement() – This works similar to getAndAdd, But this increment by 1. (++variable)
incrementAndGet() – This works similar to addAndGet. But increments by 1 (variable++)
getAndDecrement() – This is similar to getAndAdd, This decrements value by 1 (–variable)
decrementAndGet() – This is similar to addAndGet, This decrements value by 1 (variable–)

If we want to subtract some value, We can use addAndGet(-value) / getAndAdd(-value)

Now let us see an example of all these above methods,

Output:
op1

Read More

Detecting Internet Connection Using Java

Detecting Internet Connection Using Java
In this article, We are going to see how to check the internet connection using java. We are going to check if the internet is connected or not using Java Code.

We are going to connect to an URL using URL in java and if it gets connected then internet is connected, if not there is no internet connection.

There are various other ways to check internet connection, Here we are going to see 2 ways to check the internet connection.

Method 1:

In this code, We are trying to connect to google.com, if the connection is success – Internet is connected and we will get an output “Internet Connected”, If there is no internet connection, We will get an error message from catch block.

Output:
op1

Method 2:
In Method 2, We are going to use Process from the package java.lang.object

getRuntime() – Returns runtime object associated with the current java application. Most of the methods in runtime are instance methods and invoked with current runtime object. (Java Runtime)

process.waitFor() – Makes the thread wait until the process represented by process object is terminated. This method depends on subprocess too. If the subprocess is terminated it will return immediately, if not the calling thread will be blocked until the subprocess terminates. (Java Process)

The output will be 0(Zero) if the internet is connected and it will be 1(One) if the internet is not connected

Output:
op2

 

Read More

Convert Text to Image using Java

Convert Text to Image using Java
Today, We are going to see how to convert a text into image using Java code. But before we proceed to code, We have to understand some of the classes used in this code.

BufferedImage – The BufferedImage subclass describes an Image with an accessible buffer of image data. This class extends image and implements WritableRenderedImage, Transparency.
BufferedImage Explanation

Font – Font class extends Object and Implements Serializable.  The Font class represents fonts, which are used to render text in a visible way
Font Details

FontMetrics – FontMetrics extends Object and Implements Serializable. The FontMetrics class defines a font metrics object, which encapsulates information about the rendering of a particular font on a particular screen
FontMetrics Details

RenderingHints – The RenderingHints class defines and manages collections of keys and associated values
RenderingHints Details

Now lets us see an example code – Changing Text into an Image.

Once you run the code,

op1

 

Read More

Search and Replace Content inside a File

Search and Replace Content inside a File
Today, We are going to see how to search a file for specific content and replace the content and write to a new File.

Let us create a text document with some content in it

screen1

Now we are going to write a code to change “File 1” into “File 2”, The content will be

“Sample Content in File 2”

Once the code is executed, File2.txt will get created and content will be,

screen2

 

Read More