Found 34484 Articles for Programming

How are parameters passed in Java?

Shriansh Kumar
Updated on 20-Jul-2023 21:19:58

792 Views

The most frequent query asked by beginner programmers is that how are parameters passed in Java. Generally, the programming languages use pass by value and pass byreference for passing parameters to a method. However, Java does not support both approaches rather it uses pass by value to pass both primitive and reference type values. In this article, we are going to understand passing parameters by value through exampleprograms. Passing Parameters to a Method in Java Let’s start this discussion by understanding the storage mechanism of Java. The referencevariables, names of methods and classes are stored in stack and their values ... Read More

Different Ways to Create the Instances of Wrapper Classes in Java

Shriansh Kumar
Updated on 20-Jul-2023 21:14:47

203 Views

To encapsulate or represent a primitive datatype within an object, Java provides theconcept of Wrapper classes. All eight wrapper classes are as follows Double, Float, Long, Integer, Short, Byte, Character, and Boolean. These classes have a variety of in-builtmethods that allows us to integrate primitives into their respective instances as well asdisintegrate instances into their respective primitive datatypes. This article aims to explainthe different ways to create instances of Wrapper Classes. Creating Instances of Wrapper Classes The following approaches are available in Java to create Instances of Wrapper Classes − With the help of Constructor Although we can create an ... Read More

Getting Least Value Element From a Set by Using Sorting Logic on TreeSet in Java

Shriansh Kumar
Updated on 20-Jul-2023 21:06:28

42 Views

TreeSet is a class of Java Collection Framework that implements the SortedSet Interface.It stores elements in ascending order and does not allow duplicate values therefore, theaccess and retrieval time becomes faster. Because of this excellent feature, TreeSet isfrequently used to store large amounts of information that need to be searched quickly.We will use the Comparable Interface to sort the given TreeSet and then using in-builtmethod named ‘first()’, we try to get the least value element from that TreeSet. Java Program to get Least value element from TreeSet Before jumping into the program, let’s familiarize ourselves with a few concepts − ... Read More

Getting Highest and Lowest Value Element From a Set by using Sorting Logic on TreeSet in Java

Shriansh Kumar
Updated on 20-Jul-2023 21:02:54

54 Views

TreeSet is a class of Java Collection Framework that implements the SortedSet Interface.It stores elements in ascending order and does not allow duplicate values therefore, theaccess and retrieval time becomes faster. Because of this excellent feature, TreeSet isfrequently used to store large amounts of information that need to be searched quickly.We will use the Comparable Interface to sort the given TreeSet and then using in-builtmethods, we try to get the highest and lowest value elements from that TreeSet. Java Program to get Highest and Lowest value elements from TreeSet Before jumping into the program, let’s familiarize ourselves with a few ... Read More

How to Add Custom Class Objects to the TreeSet in Java?

Shriansh Kumar
Updated on 20-Jul-2023 20:14:00

618 Views

TreeSet is a class of Java Collection Framework that implements the SortedSet Interface. Remember that it stores elements in ascending order and does not allow duplicate values. We need to stick with this condition while adding custom class objects to the TreeSet otherwise we will encounter a ClassCastException. Here, custom class objects mean userdefined objects that are created with the help of a constructor. Program to add Custom Class Objects to the TreeSet Earlier in the previous section, we discussed that if we failed to follow the condition of TreeSet, we will get a ClassCastException. To avoid this, we need ... Read More

How do I generate random integers within a specific range in Java?

Shriansh Kumar
Updated on 20-Jul-2023 20:07:50

558 Views

Suppose we are in a situation where we need to generate random integer numbers within a specific range through Java programs. For the given scenario, there are two distinct ways available in Java. We can use either the Random class or random() method. Let’s discuss them in the next section. Generate Random Integers within a Specific Range We are going to use the following class and method − Random Class We create an object of this class to return pseudorandom numbers within a given range.We will customize this object and apply our own logic to generate any random valueswithin the ... Read More

Get Unique Values from ArrayList in Java

Shriansh Kumar
Updated on 20-Jul-2023 19:55:09

871 Views

ArrayList is a class of Java Collection Framework that implements List Interface. It is a linear structure that stores and accesses each element sequentially. It allows the storage of duplicate elements however, there are a few approaches that may help to get unique values from an ArrayList. In this article, we are going to see the practical implementation of those approaches through Java example programs. Java Program to get Unique Values from ArrayList Before jumping to the solution program for the given problem, let’s discuss the following concepts of Collection Interface − HashSet It is a class of Java Collection ... Read More

How can I modify an element of a PriorityQueue in Java?

Shriansh Kumar
Updated on 20-Jul-2023 19:50:54

354 Views

Generally, the Queue follows the First in First out (FIFO) approach but a PriorityQueue follows a priority based approach while accessing elements. Each element of the queue has a priority associated with it. The elements are prioritized based on natural sorting order. However, we can provide custom orders using a comparator. The elements of PriorityQueue are not actually sorted, they are only retrieved in sorted order. This feature allows us to modify an element of PriorityQueue easily. Java Program to modify an element of a ProrityQueue Before jumping into the program, let’s familiarize ourselves with a few in-built methods of ... Read More

Getter and Setter in Java

Shriansh Kumar
Updated on 20-Jul-2023 19:35:47

1K+ Views

Getter and setter are two special methods in Java that allow accessing and modifying data members' values. They are mostly used in encapsulation and data hiding to protect our sensitive data from unauthorized access. In encapsulation, we group related data and behavior together in a class and hide the implementation details from the outside world. Data hiding means preventing direct access of the members of class from the internal state of an object. In this article, we will explain what getter and setter methods are in Java and how they are useful in data hiding. Getter and Setter methods The ... Read More

Producer Consumer Solution using BlockingQueue in Java Thread

Shriansh Kumar
Updated on 20-Jul-2023 19:30:21

820 Views

Producer Consumer is the most common problem of Java concurrency and multi-threading. It arises during the synchronization that helps to manage multiple threads trying to access a shared resource. This article will help us to find Producer Consumer Solution using BlockingQueue in Java Thread. Producer Consumer Problem and BlockingQueue Understanding Producer Consumer Problem Producer and Consumer are two distinct entities or processes that use a shared queue. This queue is of a fixed size buffer. The producer generates pieces of information and stores them in the queue. The consumer consumes the given information and removes them from the queue. The ... Read More

Advertisements