JShell is a Read-Evaluate-Print Loop (REPL) that evaluates declarations, statements, and expressions as we have entered and immediately shows the results. This tool is run from the command prompt. Below, we can define expressions, variables, and methods in JShell. Expression The combination of variables, operators, and methods is known as an Expression in Java. They perform some logic that evaluates to a single value. int a=5, b=10, c=0; c=a+b; Here, "a+b" is an expression that evaluates the value of c as 15. Expression in JShell We can type any valid Java expression in JShell. The expression ... Read More
In this article, we will learn about the use of the Cleaner class in Java 9. Below, we will first tell the basic usage and then we will know how we can use the Cleaner method practically. Garbage Collector An Object that has been created during the program execution is automatically removed by the Garbage Collector (GC). When an object not referenced by any thread and when JVM determines that this object can't be accessed, then it can be eligible for garbage collection. The finalize() method The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the ... Read More
Java 9 provides factory methods to create immutable lists, sets, and maps. It can be useful to create empty or non-empty collection objects. In Java 8 and earlier versions, we can use collection class utility methods like unmodifiableXXX to create immutable collection objects. If we need to create an immutable list, then use the Collections.unmodifiableList() method. What does Immutable mean? An object is considered immutable if its state cannot change after it is constructed. Any modification creates a new object instead. String name = "TutorialsPoint"; name = name + "10"; Here, "TutorialsPoint" is an immutable string, and we cannot modify it directly. Instead, we use the ... Read More
Java Shell or JShell is an official REPL(Read-Evaluate-Print-Loop) introduced with Java 9. It provides an interactive shell for quickly prototyping, debugging and without the need for the main() method or the need to compile the code before executing it. JShell is easily started by typing "jshell" in the command prompt. What are Snippets? JShell allows the execution of Java statements, variables, methods, class definitions, imports, and expressions. These pieces of Java code are referred to as snippets. Every snippet is assigned an ID(unique) that we can use later to reference it. Example Below is an example to represent a snippet ... Read More
In this article, we will learn to create an unmodifiable Set in Java 9. We will be learning about Set and unmodifiable Set, and will be learning different ways of creating an unmodifiable Set(Set.of() Method and unmodifiableSet() Method). Set In Java, a Set is an interface that inherits the Collection interface and contains no duplicate elements. A Set interface has methods to add, delete, and check the existence of an element. Some of the implementations of the Set interface are: HashSet TreeSet LinkedHashSet What is an ... Read More
In this article, we will learn to use the ofNullable() method of Stream in Java 9. We will first go through the Stream class, after that we will learn about the ofNullable() method along with its syntax and real-world use cases. Stream Class Stream was introduced in Java 8, the Stream API is used to process collections of objects. It represents a sequence of elements that support various operations to perform computations in parallel. Creating an instance of a stream: Stream stream; A stream is not a type of data structure, it takes input from Collections, Arrays, etc. The ofNullable() ... Read More
Python provides a built-in module called zipfile that allows us to create, read, write, and extract ZIP archives. When we want to extract only specific files, such as all .txt files, then we can perform filtering of the file names using string methods such as endswith(). A ZIP file is one of the archive formats which is used to compress one or more files into a single file to easily store and transfer data. It reduces the file size and keeps all related files compressed together for sharing over the internet and for saving disk space. Steps involved in ... Read More
An arc of a circle is a segment or portion of the circumference of a circle. In other words, a curve between a pair of distinct points. The length of the arc refers to the length between two points along the arc (two points along a section of a circle or curve). It is a percentage of the circumference of the circle. When two lines intersect each other, the common meeting point is called a vertex and the geometrical figure between the two arms/lines is called an angle. Following diagram will help you to understand the arc and its length: ... Read More
In this article, we will explain the dominating set problem and implement it's solution in C++. First of all, let's understand what a dominating set is. Dominating Set of a Graph A dominating set for a graph is a subset of the set of all the vertices. Every vertex that is not in the dominating set should be adjacent of at least one vertex in the dominating set. To understand this clearly, consider the following graph: In the above graph, the set of vertices {B, D} is one of the dominating sets, because: ... Read More
The subsets of a set refers to all the possible combinations of choosing elements from the set. For example, if we have a set {1, 2}, the subsets are {}, {1}, {2}, and {1, 2}. In this article, we will learn how to generate a random subset for a set using coin flipping technique in C++. // Set of elements int arr[] = {1, 2, 3}; // Subsets of the above set {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Coin Flipping Technique The coin flipping technique is a simple way ... Read More