Home Java

Java

  • The Scanner is a class in java.util package. This class is responsible for reading data from a string, the keyboard, a file, or a network socket. This article focuses solely on reading the keyboard input and displaying the output in a terminal window. Reading data from a file or a network channel is done similarly.

  • With the help of examples, you will learn how to utilize the switch statement in Java to regulate the flow of your program’s execution. The switch statement lets us choose several different ways to run a code block. The switch statement in Java has the following syntax:

  • 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.

  • 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.

  • In Java, getting the current timestamp is simple. We’ll learn how to acquire the timestamp using the Date and Timestamp classes in this lesson. This article provides some Java examples for obtaining the current date, time, or timestamp. In addition, Java 8 has been included.

  • Java

    TreeMap in Java

    by Lucy

    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.

  • Java

    Encapsulation in Java

    by Lucy

    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.

  • The Java ternary operator allows you to build a single-line if statement. True or false can be the result of a ternary operator. Whether the statement evaluates to true or false, it returns a certain result.

  • 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.

  • In Java, a priority queue is a special form of the queue in which all components are ordered either by their natural ordering or by a custom Comparator provided at the time of construction. Before talking about priority queues, let’s look at what a regular queue is.

  • 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.

  • With the help of examples, we will learn about Java methods, define them, and utilize them in Java programs in this article. A method is a piece of code that accomplishes a specific goal. Generally, a method is a collection of statements or statements organized together to conduct a particular task or action. It’s a technique for making code more reusable. We create a method once and then use it repeatedly.