Object Oriented Programming Articles

Page 344 of 589

How to create immutable class in Java?

Rishi Raj
Rishi Raj
Updated on 28-Jan-2025 1K+ Views

An immutable class object's properties cannot be modified after initialization. The state of an immutable object remains constant throughout its lifetime. To achieve immutability, the class is designed such that its fields are initialized only once through a constructor, and no methods are provided to modify those fields. For example: String is an immutable class in Java. We can create an immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. ...

Read More

Static methods vs Instance methods in Java

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 28-Jan-2025 3K+ Views

In Java, the behaviour of any variable or method is defined by the keyword used in front of its declaration. One of the non-access modifiers is static, which can be used with both methods and variables. The static methods are defined at the class level and can be accessed without creating an instance of the class, while instance methods require an object of the class for accessibility. Static methods exist as a single copy for the entire class, whereas instance methods exist as multiple copies, depending on the number of instances created for that particular class. Static methods cannot directly ...

Read More

Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 28-Jan-2025 535 Views

The length of the longest consecutive 1s in the binary representation of a given integer involves converting the integer to its binary form and then identifying the longest run of contiguous 1s. Here, we can represent each integer in binary as a series of 0s and 1s. Bitwise operations allow efficient bit manipulation, making it easy to count and update the longest sequence of consecutive 1s. Consider special cases such as when the integer is 0 (binary representation 0) or when it contains only 1s (e.g., for the number 7, binary 111). To understand the concept clearly, Let's go through ...

Read More

Java program for longest subsequence of a number having same left and right rotation

Shubham Vora
Shubham Vora
Updated on 23-Jan-2025 310 Views

In this tutorial, we will learn how to find the maximum length of the subsequence having the same left and right rotations in Java. We find the maximum length of such subsequence. Finding the longest subsequence with identical or alternating digits We will solve the problem based on a particular observation. The strings can only have the same left and right rotations if all digits of strings are equal or it contains the two digits alternatively, and the string length is even. Step 1: Initialize Variables: Initialize the len variable with the string length and ...

Read More

Java Program for Arithmetic Operations Between BigDecimal and Primitive Data Types

Mr. Satyabrata
Mr. Satyabrata
Updated on 23-Jan-2025 447 Views

In this article, we will explore how to perform arithmetic operations between BigDecimal and Primitive data types by using Java. For instance, Addition: The sum of a BigDecimal object with a value of 10.50 and an int primitive with a value of 5 is a BigDecimal object with a value of 15.50. 10.50 + 5 = 15.50 SyntaxThe syntax doubleValue() method returns the value of a BigDecimal object as a double primitive type. double doubleNum1 = num1.doubleValue(); Where “num1” is a BigDecimal object. What is BigDecimal in Java? BigDecimal is a class in Java's java.math package that ...

Read More

Java Program to count inversions of size three in a given array

Rudradev Das
Rudradev Das
Updated on 23-Jan-2025 372 Views

In this article, we will learn how to count inversions of size three in a given array. The goal is to find how many triplets (i, j, k) exist such that i < j < k and arr[i] > arr[j] > arr[k].Understanding Inversion Count Inversion count is a step-counting method by which we can calculate the number of sorting steps taken by a particular array. It is also capable of counting the operation time for an array. But, if we want to sort an array in a reverse manner, the count will be the maximum number present in that array. Array: ...

Read More

Java DatabaseMetaData getTimeDateFunctions() method with example

Vikyath Ram
Vikyath Ram
Updated on 23-Jan-2025 159 Views

In this article, we will learn how to retrieve the list of time and date functions in the database using the getTimeDateFunctions() method of the DatabaseMetaData class in Java. getTimeDateFunctions() The getTimeDateFunctions() method retrieves the list of time and date functions supported by the current database. The names returned by this method are the Open CLI time and date function names. Steps to Retrieve Time and Date Functions in Database To get the list of the time and date functions supported by the underlying database − Step 1: Register the Driver: Make sure your database is ...

Read More

Java sql.Time setTime() method with example

Vikyath Ram
Vikyath Ram
Updated on 23-Jan-2025 745 Views

In this article, we will learn the usage of the setTime() method in Java with a complete example. Handling time-related data is an essential aspect of many Java applications, especially when interacting with databases. The java.sql.Time class, designed to handle SQL TIME values. What is setTime() method The setTime() method of the java.util.Time class accepts a variable of long type, representing the number of milliseconds from the epoch time (January 1, 1970, 00:00:00.000 GMT) to the required time, and sets the specified time value to the current Time object. //Setting time time.setTime(time_value_in_long); Use Cases of setTime() Following are the use ...

Read More

Java program to convert millisecond to readable string

Alshifa Hasnain
Alshifa Hasnain
Updated on 22-Jan-2025 984 Views

In this article, we will learn to convert milliseconds to readable strings in Java. Converting milliseconds into a human-readable format such as hours, minutes, seconds, and milliseconds is a common programming task, especially in time-sensitive applications. Problem Statement The challenge is to convert a given time in milliseconds into a readable string that shows hours, minutes, seconds, and remaining milliseconds. Input long millis = 5000000; Output  Hours = 1 Minutes = 23 Seconds = 20Milliseconds = 0 1 hr(s) 23 min(s) 20 sec(s) 0 ms Approaches to convert millisecond to readable string Following are the two different approaches to converting milliseconds ...

Read More

Java program to generate n distinct random numbers

Alshifa Hasnain
Alshifa Hasnain
Updated on 22-Jan-2025 1K+ Views

In this article, we will learn to generate n distinct random numbers within a specified range, ensuring that no number is repeated in Java. Generating random numbers is a common problem in various applications such as simulations, games, and testing. Problem Statement In this problem, we are given a number n, and we need to generate n unique random numbers within a defined range, ensuring that no number is repeated. The range can be specified by the user, or it can be a default range based on the problem's requirements. Input int n = 10; Output [4, 6, 9, 1, ...

Read More
Showing 3431–3440 of 5,881 articles
« Prev 1 342 343 344 345 346 589 Next »
Advertisements