Found 7430 Articles for Java

Initializing multiple variables to the same value in Java

divya srivastava
Updated on 23-Sep-2024 19:09:57

707 Views

In this article, we will learn to initialize multiple variables to the same value in Java. What is a variable? A variable is a name given to a space reserved in the memory. Every variable has a type which specifies what kind of data it holds. Initializing multiple variables to the same value Multiple variables can be initialized to the same value in one statement as: variable1 = variable2 = variable3 = value This means that the value is assigned to variable3, then variable3 is assigned to variable2, and finally, variable2 is assigned to variable1. Input 1 a ... Read More

Can an int be null in Java?

kavan singh
Updated on 19-Sep-2024 23:01:21

334 Views

No, In Java int cannot be null. There are some primitive data types and int is the one of the primitive data types in Java programming. The default value for an int data type is 0 and it cannot not be null. The different primitive data types have their different default values, but the objects in Java can be null. There is no null reference concept for the primitive data types (such as int, float, etc.). Example For example, if you try to assign null to an int variable. It will generate an error. int myInt = null; How ... Read More

Java program to find the top and bottom elements of a given stack

Shubham B Vora
Updated on 23-Sep-2024 19:34:09

2K+ Views

In this tutorial, we will be looking at how to find the top and the bottom elements in a given stack using Java. When we look at the stack, it represents a linear dataset following the Last In, First Out (LIFO) principle, hence the elements are added and removed in the same place. We will further explore two approaches for finding a given stack's top and bottom elements, i.e. via iteration and recursion. Problem Statement We will be given a stack array of n elements and the task is to find the 1st and nth element of the stack without ... Read More

Java: Get month Integer from Date

Nickey Bricks
Updated on 10-Sep-2024 11:56:12

140 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

Maximum Subarray Sum in Java: Kadane’s Algorithm

Mohammed Shahnawaz Alam
Updated on 09-Sep-2024 01:16:26

247 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 count all stack elements

Shubham B Vora
Updated on 10-Sep-2024 09:50:50

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

Check if stack elements are pairwise consecutive in Java

Prasanna D
Updated on 19-Sep-2024 22:00:31

112 Views

Stacks are a fundamental data structure in computer science, often used for their Last-In-First-Out (LIFO) properties. One interesting problem that can be encountered while working with stacks is checking whether the elements of a stack are pairwise consecutive. In this article, we will learn to solve this problem using Java, ensuring an efficient and clear solution. Problem Statement Given a stack of integers, the task is to determine if the elements of the stack are pairwise consecutive. Two elements are considered consecutive if they differ by exactly 1. Input 4, 5, 2, 3, 10, 11 Output Are elements pairwise consecutive?true ... Read More

Java program to delete all even nodes from a singly linked list

John Wick
Updated on 18-Sep-2024 21:54:29

297 Views

In this article, we will learn to delete all even nodes from a singly linked list in Java. This Java program demonstrates how to create and manage a singly linked list, including adding nodes, deleting nodes with even values, and printing the list. You will see how to insert nodes, remove nodes with even values, and display the remaining nodes. A singly linked list consists of nodes where each node has two parts: one part stores the data, and the other part holds the address of the next node. This setup allows traversal in only one direction, as each node ... Read More

Java program to remove duplicates from a given stack

Anh Tran Tuan
Updated on 14-Aug-2024 18:45:36

409 Views

In this article, we’ll explore two methods to remove duplicate elements from a stack in Java. We’ll compare a straightforward approach with nested loops and a more efficient method using a HashSet. The goal is to demonstrate how to optimize duplicate removal and to evaluate the performance of each approach. Problem statement Write a Java program to remove the duplicate element from the stack. Input Stack data = initData(10L); Output Unique elements using Naive Approach: [1, 4, 3, 2, 8, 7, 5] Time spent for Naive Approach: 18200 nanoseconds Unique elements using Optimized Approach: [1, 4, 3, 2, ... Read More

Subarray with given sum in Java with different approaches

Areeba Rashid
Updated on 14-Aug-2024 22:07:51

170 Views

Finding a subarray with a given sum is a common problem that often appears in coding interviews and competitive programming. This problem can be solved using various techniques, each with its own trade-offs regarding time complexity and space complexity. In this article, we'll explore multiple approaches to solving the problem of finding a subarray with a given sum in Java. Problem Statement Given an array of integers and a target sum, find a continuous subarray in the array that adds up to the given sum. The problem can be divided into two main variants: Subarray ... Read More

Previous 1 ... 3 4 5 6 7 ... 743 Next
Advertisements