Suppose you want to know how many objects of a class have been created during a program execution. How would you do that? Using constructor When an object of a class is created, its constructor is always called. Thus, if we keep track of how many times the constructor of a class is called, we directly know number of its objects that have been created. Thus, we need to add some counter in a constructor and increment its value by 1 in every invocation. This counter should be a class level variable. Since class level variables have different values for each object, we have toRead More →

String is the most commonly used data type in java. Often you need to perform operations on a string but before any operation it is necessary to check for an empty(or zero length) string and also whether the string is null to avoid java.lang.NullPointerException. To know more about the most commonly faced NullPointerException, refer this article. This post will explain different methods in which a string can be tested for being empty and null. Method 1: Using isEmpty method java.lang.String class has an isEmpty method which returns true if the length of the string is 0 and false if the string has at least oneRead More →

Generation of random sequence of characters is often required when you want to display a unique transaction id or as a random password generator which provides a temporary password for user registering for the first time on a website or while creating captcha for preventing automated input etc. Java provides many different ways to program a random string generator application and this post will explain most of those. Method 1: Using UUID java.util.UUID class can be used to generate a random string. Its static randomUUID method acts as a random alphanumeric generator and returns a String of 32 characters. If you want a string ofRead More →

java.io.InputStream represents a stream of bytes. Often when reading binary files or from a network, data is returned in an input stream format and we need to read that data and convert it to a string format so that it becomes meaningful. This post will list out various methods in which the content of an input stream can be converted to a java.lang.String. All the methods assume that you have a source input stream available. Method 1: Using Scanner class java.io.Scanner class has a constructor which takes a java.io.Inputstream as argument. Once an object of scanner is created, use its hasNext and next methods toRead More →

What is JSON? JSON stands for JavaScript Object Notation and has become the most popular format for transfer and storage of data since due to its light weight nature. Data between client and server can only be transferred in the form of a string. A json object can be easily converted to a string and vice-versa. Also, it is pretty easy to traverse across a json object for retrieving values. A json string or json object stores data in key-value format and it can also represent complex parent-child relationships thus making it a preferred way to store and transmit data. Why String to json objectRead More →

If you are connecting to MySQL version 8 from your java application, there are bright chances that you might have come across the following error java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed and if you are accessing a MySQL database from a web or application server such as Wildfly or Tomcat, then you would have encountered this error java.sql.SQLException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed) Solution Add allowPublicKeyRetrieval connection option of MySQL with a value of true to the JDBC connection string. Thus, your connection string should be modified as jdbc:mysql://<database server ip>:3306/databaseName?allowPublicKeyRetrieval=true Reason Since you are connecting to a database, youRead More →

Determining the number of words in a file is an important aspect of programming. It is required while solving programming problems and it is also utilized in real world applications. This post shall detail out different methods to count the number of words in a file. All of the methods involve the logic to read a file, thus you should know how to read a text file in java. This post will definitely help you out on that. Method 1: Using BufferedReader Read a file line by line using java.io.BuffereredReader. Break this line into words by splitting over it using split method of java.lang.String. ThisRead More →

What is sorting? Sorting means to arrange elements of a collection in an order which can be either ascending or descending. Collection can be an array, a list, keys or values of a map etc. Usually when sorting techniques are discussed, the collection is sorted in ascending order of its elements and the collection picked up is an array. There are many approaches available to sort an array. This post will focus on Bubble sort. What is Bubble sort? It is a sorting algorithm in which each array element is compared with the element next to it. If the element being compared is greater thanRead More →

What is MAC address MAC(Media Access Control) address of a device is a unique identity assigned to it. It is used to identify a computer over a network. Similar to IP address, MAC address of a device is also unique. A MAC address is assigned during the manufacture process of hardware and remains constant throughout its life. It is also referred to as device address or physical address. MAC address is only assigned to Network Interface Controllers. These are hardware devices that connect to a network. Since these devices connect a computer to a network, they act as an interface between the two, hence calledRead More →