toString() method is used to convert an object to its String representation. You can call this method on a String, a StringBuffer, a java.sql.Connection object or an object of your own custom class. This method is available to all the classes because it is written in java.lang.Object class, which is the super class of all classes. Default toString() method You might have noticed that if you call toString() on an object, then it prints something like Student@4aa8fd04, where Student is the name of class. This is because when you call toString() on an object, it invokes toString() method of java.lang.Object class by default, whose implementationRead More →

You can check for the presence of vowels(a, e, i, o, u) in a string and list them using a java program. There are following two ways in which this can be done. Method 1: Iteration over the string Follow the below steps to write a program to check the vowels in a string using iteration. Initialize a string with all the vowels. Iterate over the characters of the string in which you want to check the vowels. In very iteration, check if the current character is from among the characters of string in Step 1(all vowels). If it is, then it is a vowelRead More →

What is a clone A clone is an exact copy of something. Object cloning is required when you want to create a new object with the same values of properties and containing all methods as an existing object. A practical scenario would be when creating a new entity whose most of the property values are common as all other entities except for a couple of properties. Instead of copying each property value one by one, you can clone the object and then change the properties having different values. This article will explain how to make a clone of an object along with the concepts ofRead More →

A file and directory in java can be deleted by using delete method on java.io.File object which points to the underlying physical file or directory. delete method will delete a directory only if it is empty otherwise it would not remove the directory. This post will discuss different methods to delete a non-empty directory in java. Method 1: Iterative method Create a java.io.File object which points to the directory to be deleted. Get a list of all files in this directory using listFiles method. This method returns an array of java.io.File objects each of which represents a file in the directory. Iterate over the filesRead More →

In a multi-threaded application where multiple threads are accessing a resource at the same time, it becomes necessary to handle concurrent modifications. Suppose a file is being written by a thread and at the same time it is being read by another thread. In such case, the thread reading the file will not have the updated contents. Similarly, if two threads are writing a file at the same time, then there are chances of the file becoming corrupt. To avoid such situations, there should be a mechanism that locks the file while writing or reading in java. Fortunately, java NIO has the facility to lockRead More →

Why java version at runtime Sometimes you need to execute some code conditionally based on the current executing java version. For example, there is some functionality whose code was different on an earlier java release but on later releases it was optimized as per the new features introduced in java itself. This requires that you determine the version of java during program execution and execute code according to it. This post will list down different ways in which this is possible Using java.version system property Java provides a property java.version which returns the version of java runtime, that is, the environment in which the currentRead More →

Almost every java developer would have encountered the below error sometime or the other while executing a java program. Exception in thread “main” java.lang.UnsupportedClassVersionError: com/codippa/HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 java.lang.UnsupportedClassVersionError means that the version of JDK on which the java file was compiled was a higher version and the JRE version on which it is being executed is of a lower version. Example, a file compiled on JDK 8 is being executed on JRE 6. And that is alsoRead More →

You can easily determine the jdk version with which a given java file was compiled using the below command. javap –verbose <class file name> The above command should be executed at the location where the class file is placed or otherwise the entire path of class file needs to be given. Open the command prompt on windows or terminal on linux/mac systems and navigate to the location of class file and execute the above command. Example, E:\com\codippa>javap -verbose HelloWorld.class Note the folders com/codippa is the package of this class since a java file when compiled is located inside the folders that match its package. ThisRead More →

Many times we need to check the version of java installed on the system due to the following reasons Check if java is installed even or not. Know if the installed java will support some feature or it contains newly added classes or not. How to check java version from command line Open command prompt on windows or terminal on linux and mac systems, type the following command and hit enter. java -version If java is added to the system’s classpath, then it will show following output. C:\>java -version openjdk 14-jpackage 2020-03-17 OpenJDK Runtime Environment (build 14-jpackage+1-8) OpenJDK 64-Bit Server VM (build 14-jpackage+1-8, mixed mode,Read More →