Friday, 30 March 2018

Java - Convert String to boolean

There are two ways to convert a String to boolean in Java, first, by using Boolean.parseBoolean() method and second, by using Boolean.valueOf() method.The parseBoolean() method returns an equivalent boolean value of given String, for example, if you pass "true" it will return the primitive boolean value true. Similarly, if you pass "false" it will return false. The good thing about this method is that it is case insensitive, which means if you pass "true", "TRUE", or "True" you will still get a true boolean value. Another good thing about this method is that it doesn't throw an exception if you pass any String value other than true and false. For example, if you pass "YES" it will return false, which is not obvious but that's still better than throwing an exception like NumberFormatException.

Wednesday, 28 March 2018

Java Program to Subtract two Binary Numbers

Binary subtraction is very similar to binary addition which we have learned in the last article. In this tutorial, you will learn how to subtract two binary numbers. Similar to the last article, we'll see two ways, first by converting binary String to a binary number and then doing subtraction. You can use the Java API Integer.toString(number, radix) for that. On second solution you will learn to develop the logic to perform binary subtraction in Java program by using rules you might have learned in your computer classes. Here is a little summary of how binary subtraction works.

Monday, 26 March 2018

How to Compare Two Dates in Java? Check If They Are Equal, Later or Earlier

There are multiple ways to compare two dates in Java, it also depends on upon what exactly comparison means. If you are looking to compare two dates to find out whether they are equal or not, then you can simply use equals() method of java.util.Date class. This method will return true if both dates are equal, precisely both thave same millisecond value. If you are looking to find out whether a date comes before or after another date then you have 3 choices, you can use compareTo() method of java.util.Date class, or you can use before() and after() method of Date class, or you can use before() and after() method of Calendar class. I suggest leveraging before() and after() method of Date class, they are more readable and easy to use.

Friday, 23 March 2018

Spring Data JPA Example with Spring Boot

1. Introduction


In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database for this lesson.

Spring Data JPA offers a set of very powerful and highly-abstracted interfaces which are used to interact with any underlying database. Databases can be MySQL, MongoDB, Elasticsearch or any other supported database. Other advantages for Spring Data JPA include:

Wednesday, 21 March 2018

How Volatile in Java works? Example of volatile keyword in Java

How to use Volatile keyword in Java


What is volatile variable in Java and when to use  the volatile variable in Java is a famous multi-threading interview question in Java interviews. Though many programmers know what is a volatile variable but they fail on second part i.e. where to use volatile variable in Java as it’s not common to have a clear understanding and hands-on on volatile in Java. In this tutorial, we will address this gap by providing a simple example of the volatile variable in Java and discussing some when to use the volatile variable in Java. Anyway,  the volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in an int or a boolean variable then  you can declare them as volatile variable.

Friday, 16 March 2018

SMPP Java Example(Client)

This post provides SMPP java example by creating a simple SMPP client that sends short messages to mobile subscriber.Using this client either we can make simple submit to send message to a single mobile subscriber or can broadcast a message to multiple mobile subscribers in one shot.Also, we will verify the delivery receipt. For the client purpose we will be using existing java SMPP client library – jSMPP

Wednesday, 14 March 2018

AES Encryption and Decryption in Java(CBC Mode)

Transmitting confidential data such as plain text password through wire is always vulnerable to security.It is always recommended to encrypt such information and use SSL to transmit those confidential data.Java provides multiple encryption algorithm for this.In this post, we will be discussing about AES(Advanced Encryption Standard) symmetric encryption algorithm in java with CBC mode which is faster and more secure than 3DES.

Monday, 12 March 2018

JDBC - Difference between PreparedStatement and Statement in Java

JDBC API provides three types of Statement for wrapping an SQL query and sending for execution to the database, they are aptly named as Statement, PreparedStatement, and CallableStatement. First one, Statement is used to execute normal SQL queries e.g. select count(*) from Courses. You can also use it to execute DDL, DML and DCL SQL statements. The second one, PreparedStatement is specialized to execute parameterized queries e.g. select * from Courses where courseId=?, you can execute this SQL multiple times by just changing the course parameters. They are compiled and cached at database end, hence quite fast for repeated execution. The third member of this family is CallableStatement, which is there to execute or call stored procedures stored in the database.

Friday, 9 March 2018

A Simple Example to Check if File is Empty in Java

One of the readers of my blog Javarevisited emailed me yesterday about how to check if a file is empty in Java and that's why I am writing this post to show that using an example. The common approach for checking if a file is empty or not is to first check if the file exists or not and then check if it contains any content, but Java has done that hard work for you. Well, it's pretty easy to check emptiness for a file in Java by using length() method of the java.io.File class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist. Which means you don't need to check if the file exists or not.

Wednesday, 7 March 2018

How to implement Radix Sort in Java - Algorithm Example

The radix sort, like counting sort and bucket sort, is an integer based algorithm (i.e. the values of the input array are assumed to be integers). Hence radix sort is among the fastest sorting algorithms around, in theory. It is also one of the few O(n) sorting algorithm along with a bucket and counting sort. The particular distinction for radix sort is that it creates a bucket for each cipher (i.e. digit); as such, similar to bucket sort, each bucket in radix sort must be a growable list that may admit different keys.

Sunday, 4 March 2018

Mergesort in Java - Algorithm Example

The Merge sort algorithm is a divide and conquers algorithm. In the divide and conquer paradigm, a problem is broken into small problems where each small problem still retains all the properties of the larger problem -- except its size. To solve the original problem, each piece is solved individually; then the pieces are merged back together. For example, imagine you have to sort an array of 200 elements using the selection sort algorithm. Since selection sort takes O(n^2) time, it would take about 40,000-time units to sort the array. Now imagine splitting the array into ten equal pieces and sorting each piece individually still using selection sort. Now it would take 400-time units to sort each piece; for a grand total of 10*400 = 4000.