Found 2616 Articles for Java

Different Ways to Create the Instances of Wrapper Classes in Java

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

198 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

Different Ways to Print First K Characters of the String in Java

Shriansh Kumar
Updated on 20-Jul-2023 21:09:15

83 Views

A string is a class in Java that stores a series of characters enclosed within double quotes.Those characters are actually String-type objects. The string class is available in the‘java.lang’ package. Suppose we have given a string and a positive integer ‘k’. Now, thejob is to print that string's first 'k' characters in Java. Also, check whether the length ofgiven string is less than or not, if so print the original string. Java Program to Print First K Characters of the String Let’s understand the given problem with a few examples − Instance String st1 = “TutorialsPoint”; String st2 = “Tutorial”; ... 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

40 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

49 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

601 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

551 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

850 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

342 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

Difference between SAX Parser and DOM Parser in Java

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

2K+ Views

Both SAX and DOM are a type of XML parser APIs. Here, API stands for Application Programming Interface and Parser is used to read and extract content from an XML document in desired format. From this line, it is clear that SAX and DOM are used to read XML documents. APIs are a modern way to migrate real time information on the Web. In this article, we will discuss the difference between SAX and DOM Parser in Java. Type of XML Parser Before going further in this article, let’s briefly discuss XML and its type. XML Its full form is ... Read More

Getter and Setter in Java

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

981 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

Advertisements