How to use Math.random() method in Java

How to use Math.random() method in Java

The Math.random() method is one of Java’s most commonly used methods for generating a random numeric number. The java.lang class is a built-in class in Java.Math with several mathematical procedures, one of which is Math.random(). Because the Math.random() method is a static method, it can be invoked/called without first constructing a math class instance—the java.lang.Math class offers several methods for performing basic numeric operations such as the exponential, logarithm, square root, and trigonometric functions.

How to format a Date to String in Java

How to format a Date to String in Java

Changing the date format of a given Date or String is a typical Java programming task. For example, suppose you have “2022-05-22 12:00:00” and want to convert it to “2022-05-22,” or convert from dd-MM-YY to MM-dd-YY, or any other format you like, as long as it is a valid date format as defined by Java. How will you accomplish this? It’s not that difficult, after all. It’s a simple two-step procedure.

TreeMap in Java

TreeMap in Java

Besides the AbstractMap Class, Java’s TreeMap discreetly implements the Map interface and NavigableMap. Depending on which constructor is used, the map is sorted either by the natural ordering of its keys or by a Comparator specified at map creation time.

Encapsulation in Java

Encapsulation in Java

In Java, encapsulation combines data (variables) and code that acts on the data (methods) into a single unit. Encapsulation means that a class’s variables are concealed from other classes and can only be accessed through its current class’s methods. Consequently, it’s also known as data concealment.

HashMap in Java with examples

HashMap in Java with examples

Since Java 1.2, HashMap has been part of the Java collection. The java.util package contains this class. It implements the Java Map interface in its most basic form. It holds data as (Key, Value) pairs that can be accessed using a different index type (e.g., an Integer). A key (index) refers to another object (value). Trying to insert the duplicate key overwrites the associated key’s element.

Final Keyword in Java explained with examples

Final Keyword in Java explained with examples

We know that Java has 48 reserved keywords, one of which is the final keyword. It’s a Java keyword that can be used to limit the use of entities. It can be used for classes, methods, and variables. For various entities, the Java final keyword is used in multiple ways. The final keyword serves a different purpose for class, variable, and methods.