Found 9326 Articles for Object Oriented Programming

Different name reusing techniques in Java

Shriansh Kumar
Updated on 02-Aug-2023 17:06:44

121 Views

In Java, there exist different name reusing techniques for various types of entities, sucha s variables, methods, datatypes or packages. These techniques affect the accessibility and behavior of the entities according to their need and use. In this article, we will discuss four common ways to reuse a name in Java: overriding, hiding, overloading and shadowing Name reusing techniques in Java Shadowing This technique allows a local variable to have the same name as another field or member of the enclosing class. In this case, the previous implementation of the member gets shadowed by the declaration of a new variable. ... Read More

Different Method Calls in Java

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

83 Views

Java provides different method calls techniques that we can use according to the need andscenario in our program. Here, methods mean a block of code that can be reused multipletimes to perform a single operation. It saves our time and also reduces the size of code.The method call is referred to as invocation of methods. To use the functionality of amethod, it must be invoked by some means. This article aims to explain how methods arecalled in Java User-defined Method in Java Before discussing the method call, let’s familiarize ourselves with syntax of the userdefined methods Syntax accessSpecifier nonAccessModifier return_Type ... Read More

Heap and Stack Memory Errors in Java

Shriansh Kumar
Updated on 21-Jul-2023 11:06:46

167 Views

In Java, every interface, class, object, variable and method of a running program is stored in distinct reasons of computer memory. The heap is the part of memory area where values of variables, methods and classes are stored at runtime. Its allocation happens dynamically and can grow or shrink depending on the application's needs. On the other hand, the reference variables, names of methods and classes are stored in the stack memory area. However, if for some reason their allocation is not handled properly then, it may lead to memory errors that we are going to discuss in this article. ... Read More

How are parameters passed in Java?

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

776 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

195 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

37 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

588 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 to add an element to an Array in Java?

Shriansh Kumar
Updated on 20-Jul-2023 20:10:49

9K+ Views

Array is a linear data structure that is used to store a group of elements with similar datatypes. It stores data in a sequential manner. Once we create an array we can’t change its size i.e. it is of fixed length. Adding an element to a given array is a vastly common operation. In this article, we will discuss how to add an element to an Array through Java example programs. Adding an element to an Array in Java Let’s understand the operation with an example first − We will add a new element ‘50’ at the end ... Read More

Advertisements