JShell is a REPL interactive tool introduced in Java 9 to execute and evaluate simple Java programs like variable declarations, statements, expressions, and programs without using the main() method.In this article, we will learn to create scratch variables in JShell in Java 9. Variables in JShell In Java 9, JShell allows for the declaration and use of variables without the need for a surrounding class or method structure. One may be able to explicitly declare the variable with a type, or else it might be declared implicitly using var for type inference. C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 ... Read More
When working with floating-point numbers in C++, it's important to know how to control the number of decimal places displayed in the output. By default, cout may not always print the expected number of digits after the decimal point.How to Print Correct Number of Decimal Points with cout?To display numbers with a specific number of decimal places using cout, you can use the setprecision() function, which accepts an integer value representing the number of digits after the decimal to be printed and returns a stream manipulator that formats the number with the specified precision. The setprecision() function belongs to the ... Read More
Leading whitespace refers to any spaces, tabs, or other blank characters that appear at the start of a string. These characters come before the first visible text and can affect how the string is displayed. We can remove all leading whitespace from a string in Python using the lstrip() function. Using the lstrip() Function The lstrip() function removes all whitespace characters from the beginning (left side) of a string (leading whitespace). It does not affect any whitespace at the end or in the middle of the string. There are also two related functions i.e. rstrip() and strip() - ... Read More
We can convert all uppercase letters in a string to lowercase in Python using the lower() function. You can also use a for loop or list comprehension to change each character to lowercase one by one. This is helpful when you want to compare strings without worrying about letter cases. Using lower() Function The Python lower() function returns a new string with all uppercase letters converted to lowercase, keeping the lowercase letters unchanged. It does not modify the original string and is commonly used when comparing strings or handling user input. Example In the following example, we convert an entire string ... Read More
In Python, you can replace all occurrences of a substring within a string using the replace() method. This method returns a new string where every occurrence of the given old substring is replaced with a new substring. To match the string with case-insensitivity, you need to use regular expressions. Using the replace() Method The Python replace() method returns a new string where all occurrences of the old substring are replaced with the new substring. It does not modify the original string because strings in Python are immutable. string.replace(oldvalue, newvalue, count) where, ... Read More
A const member variable in a C++ class is a data member whose value must be initialized during object construction and cannot be modified. Initializing Const Member Variable in C++ Class The const member variable must be initialized. To initialize a const member variable, you can use a member initializer list using a constructor, . The member initializer list is the special way to initialize the values to class variables while creating an object before the constructor block runs. Note: If the member is static and of an integer type, you can directly initialized with the class definition. ... Read More
In this article, we are going to focus on how to check if the text is empty (spaces, tabs, newlines) in Python. Checking if text is Empty using isspace() method The isspace() method determines whether a string contains only spaces or contains any other characters. If the given string is only made up of spaces, this method returns true; otherwise, it returns false. Even if the string contains characters like t and n, the method returns true. Example In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the isspace() ... Read More
A dictionary in Python is a mutable, unordered collection of key-value pairs.Inserting keys/values into a Python dictionaryWe can add new key-value pairs to an existing dictionary simply by assigning a value to a new key. In this article, we are going to explore the different ways to insert new key values into a Python dictionary. Following is an example, which creates a dictionary with 4 key-value pairs and displays the dictionary - # Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod) # Displaying ... Read More
The loss of type and dimensions of an array is known as array decay. It occurs when we pass the array into a function by pointer or by value. Only the address of the first element is sent, which is treated as a pointer. As a result, the original array's size and type information are lost. Because of this decay, sizeof no longer gives the full size of the array but instead returns the size of the pointer. This can cause incorrect behavior in functions that depend on knowing the number of elements. Example of Array ... Read More
In C++, cin.ignore() is a built-in function that belongs to the istream class and is defined in the header. It is used to skip characters left in the input buffer, especially the newline character (''), which can cause issues with input operations like getline(). In this article, we will explain the use of cin.ignore() in C++. Syntax of cin.ignore() Below is the syntax of the cin.ignore() function. cin.ignore(numeric_value, delimiter_char); // Example: cin.ignore(1000, ''); Here, numeric_value is the maximum number of characters to skip, and delimiter_char is the character at which the input should stop ... Read More