Given a square whose length of all its sides are l, write a Java program to find its area. A square is a rectangle whose length and breadth are same. Therefore, area of this type of rectangle is the square of its length. To calculate the area of square in Java, you simply need to multiply the given length of square with the length itself and store the result in another variable. Java Program to Find The Area of a Square A Java program that illustrates how to find the area of the square is shown below − public class ... Read More
In this article, we are going to use various approaches for formatting the months using different libraries of Java programming language. There are several ways to display months. Sometimes the months are written as numbers, sometimes the months are written in long form or they are written in short forms. Using java.time.Month In this approach, the months are printed by specifying the month number that starts from the number 1. For instance -> Month.of(1) will give JANUARY. Example In this Java program, we are printing all 12 months name. import java.time.Month; public class ShowMonth { ... Read More
To find the smallest from three given numbers using ternary operator, create a temporary variable to store the result of first comparison operation between two numbers. Then, perform second comparison operation between the result of first operation (i.e. temp) and third number to get the final result. Let's understand the problem statement with an example − Example Scenario: Input: nums = 20, 35, 10; Output: res = 10 What is Ternary Operator? The conditional operator in Java is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal ... Read More
For a given string, say "str", write a Java program to check if it contains each character from a specified character array. If it contains print "found" otherwise "not found". A String in Java is a class that represents a contiguous block of characters. Example Scenario: Input 1: String str = "pqrst"; Input 2: char[] chSearch = {'p', 'q', r'}; Output: res = Character p, q and r found in given string In the string "pqrst", we are searching for the character set {'p', 'q', r'}. Using Iteration Here, use for loop to iterate till the length of the ... Read More
In this problem statement, our task is to write a JavaScript program by which we can simply filter an associative array with the help of another array. Before discussing the solution for the given problem, let's familiarize ourselves with associative array first. What is an associative array? An associative array is a data structure which is used to store the key and value pairs. In this each key is associated with a value given to the key. The keys in this array are unique identifiers that can be used to retrieve their respective values. In an associative array the keys ... Read More
As per the problem statement we have to write a Java program to replace each element of the array with its next element. In Java, the array is an object. It is a non-primitive data type which stores values of similar data types. Before discussing the solution for the given problem, let's understand the problem statement with an example − Example Scenario: Input: arr[] = {12, 23, 11, 64} Output: updated array = {23, 11, 64, 12} Algorithm Follow the steps below to replace each element of the array with its next element − Step 1 − Declare ... Read More
Given a string and its substring(s) of length k, write a Java program to compare and find whether the substrings are equal or not. Substring is a small portion of characters from a large string. In Java, a String is a class that represents a contiguous block of characters. Using compareTo() Method The compareTo() method belongs to the String class of java.lang package. It compares two strings based on the Unicode value of each character contained in the strings. It returns 0 if the specified strings are equal. Example In this example, we are using the compareTo() method to ... Read More
Suppose an integer number is given as an input, our task is to write a Java program to count the number of digits in that integer. For this problem, create a counter variable and initialize it with 0. Divide the given integer value with 10 till the integer becomes 0 and increment the counter variable for each turn. Using while Loop A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. For the given problem, we use this loop to check whether the specified integer value is ... Read More
A block of code that is associated with the static keyword is called as static block. This block executes when classloader loads the class. Remember, if your code contains any static block, it will be invoked before the main() method. In this article, we will learn how to create and invoke static blocks in Java along with its use case. But, before that let's understand the static keyword. What is Static Keyword? The static keyword in Java is a non-access modifier. This keyword is used with variables, methods, blocks of code and classes. A class, method or variable declared with ... Read More
The special characters, also known as metacharacters, in Java Regex holds a specific meaning within the regex syntax and must be escaped if you want to use them as regular characters. Here, we will demonstrate escaping characters in Regex through Java Program. But, before diving deep into the topic, let us get familiar with the term Regex in Java. What is Regex? It is an acronym for a Regular expression. It is an API that offers users to define String patterns that are useful for finding, modifying, and editing strings. A couple of areas of strings where Regex is ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP