Java Articles

Page 193 of 450

Java program to Count the number of digits in a given integer

Shriansh Kumar
Shriansh Kumar
Updated on 11-Sep-2024 23K+ Views

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

Java program to Count the number of digits in a given integer

Shriansh Kumar
Shriansh Kumar
Updated on 11-Sep-2024 23K+ Views

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

Static blocks in Java with example

Shriansh Kumar
Shriansh Kumar
Updated on 11-Sep-2024 6K+ Views

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

Java Program to Illustrate Escaping Characters in Regex

Shriansh Kumar
Shriansh Kumar
Updated on 11-Sep-2024 1K+ Views

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

Java Interview Questions on Constructors

Shriansh Kumar
Shriansh Kumar
Updated on 11-Sep-2024 1K+ Views

There could be numerous interview questions on Constructors, it is not possible to cover all in just one article. However, we have researched and assorted the most popular Java interview questions on Constructors. In most of the Java interviews, the interviewers always start by asking basic questions. They can test one's knowledge in just a few minutes of the interviews. Therefore, it is essential to get thorough with the fundamental concepts of Java such as class, object and constructor. In Java interviews, the first question one might encounter is to define constructors. So, let's start our discussion with this question. ...

Read More

Java: Get month Integer from Date

Nickey Bricks
Nickey Bricks
Updated on 10-Sep-2024 393 Views

Problem Statement Extraction of the month as an integer from a date object is an essential skill for developers. This is because you may encounter a task that requires you to generate monthly reports, filter date by month, and schedule events among other similar tasks. In this article, we will attempt to familiarize ourselves with Java’s robust date and time API. Prerequisite Let us dive right into the Java task at hand by ensuring you consider the following points. Ensure you are familiar with basic Java syntax, the date and time API especially. You will work with the syntax like the ...

Read More

Java program to count all stack elements

Shubham B Vora
Shubham B Vora
Updated on 10-Sep-2024 3K+ Views

In this tutorial, we will learn to count the number of elements in the stack using various approaches. In Java, a Stack is a fundamental data structure that follows the Last-In-First-Out (LIFO) principle, meaning whatever element is recently added to the stack, will be accessed first. The real-time applications of the stack are function call management, expression evaluation, etc. In such scenarios, we might need to count the number of stack elements. For example, counting the total number of function calls while using the stack for function call management and the total number of operations to be performed while evaluating ...

Read More

Java program to find missing and additional values in two lists

AmitDiwan
AmitDiwan
Updated on 09-Sep-2024 2K+ Views

In this article, we will learn to find missing and additional values in two lists in Java. By the end of this program, you'll be able to detect elements that exist in one list but not in the other, helping you better manage and analyze data in list comparisons. Problem Statement Write a program in Java to find missing and additional values in two lists. Below is the demostration − Input 101, 90, 34, 34, 67, 90 Output The missing element is : 101The new element in the list is : 67 Steps to find missing and additional values in two ...

Read More

Maximum Subarray Sum in Java: Kadane’s Algorithm

Mohammed Shahnawaz Alam
Mohammed Shahnawaz Alam
Updated on 09-Sep-2024 499 Views

Here, we are going to learn how to find the maximum subarray sum using Kadane's Algorithm in Java? Problem Statement Given an array with size N, write a Java program to find the maximum subarray sum using Kadane's Algorithm. Example Input: n = 5 arr[] = 1, 2, 3, -2, 5 Output: Maximum Subarray sum is: 9 What is Kadane's Algorithm Kadane's Algorithm helps us to find the Maxumum Subarray Sum in time complexity of O(n). Steps to Find Maximum Subarray Sum Using Kadane's Algorithm Initialize 2 variables named currentSum=Integer.MIN_VALUE and maxSum= ...

Read More

Java program to create a HashMap and add key-value pairs

karthikeya Boyini
karthikeya Boyini
Updated on 05-Sep-2024 2K+ Views

In this article, we will write a Java program to create a HashMap and add key-value pairs. We will be using the HashMap class we can import HashMap class form java.util package. A HashMap is a collection that stores data in the form of key-value pairs, allowing quick retrieval of values based on their keys. We'll also see how to display the elements stored in the HashMap using an iterator in our given program. Problem Statement Write a program in Java to create a HashMap and add key-value pairs − Output Belt: 600Wallet: 700Bag: 1100 Steps to create a HashMap and add ...

Read More
Showing 1921–1930 of 4,498 articles
« Prev 1 191 192 193 194 195 450 Next »
Advertisements