Random numbers in java can be generated either by using the Random class in java.util package or by using the random() method in the Math class in java.lang package. also read: Java Tutorials Java EE Tutorials Design Patterns Tutorials Java File IO Tutorials In both these approaches, we only get a pseudo random number and …
Java
Parsing Input using Scanner
A Scanner object can be used to read text input from a number of sources. The input source can be a file, an input stream or even from a string. It internally uses the Regular Expression Api for parsing and getting the input. Let us see a simple example to parse a String object, also …
Recursively traversing files and folders using Java File API
In this section, let us see how to recursively traverse over files and folders by making use of Java File API. Whether it is a folder or a file, both are represented as a java.io.File object. For example, consider the following code snippet, also read: Java Tutorials Java EE Tutorials Design Patterns Tutorials Java File …
Querying Class Information at Runtime using Java Reflection API
Java Reflection API provides support for querying information about a class at run-time. This information includes the list of public as well as private members (methods) available for the class, the various public operations it supports etc. This section will guide you in making use of the Reflection API. Suppose we have the following test class …
Storing Application Data using Preferences API
Applications can now make use of the Java Preferences API for storing and retrieving Application related data. The location where the Data gets stored is implementation specific. Two levels of Preferences come into picture, one is the User Preferences for storing user information and the other is the System Preferences that deals with information common …
Threads Synchronization
Synchronization is done in order to protect a segment of code from being accessed by more than a single Thread at any particular instance of time. In Java, synchronization is achieved with the use of synchronized keyword. Synchronization can be applied to methods as well as to a block of code. also read: Java Tutorials …
Strategy Design Pattern In Java
Strategy design pattern falls under the category of Behavioural patterns. Assume that we have an object and its behavior is largely dependent on the state of its internal variables. Strategy pattern allows, object to behave in different ways depends on the internal state. Strategy pattern is defined in the book Gang of four is, Allows an …
HashCode and equals methods
HashTable, HashMap and HashSet are the Collection classes in java.util package that make use of hashing algorithm to store objects. In all these Collection classes except HashSet, objects are stored as key-value pairs. For the storage and the retrieval of any user-defined objects it is a good practice to override the following methods which is …