Found 9321 Articles for Object Oriented Programming

Java Program to Allocate and Initialize Super Class Members using Constructor

Shriansh Kumar
Updated on 25-Apr-2023 18:04:07

262 Views

In java, super refers to the parent class and to inherit one class to another class we use extends keyword. Before making a java program to allocate and initialize super class members using constructor, let’s go through some concepts we are going to use in this article. What is Constructor? Constructor is very similar to methods but the difference is that methods define the behavior of an object but constructor is used to initializing those objects. We can provide any name of our choice to methods but a constructor must have same name as class name. Also, methods ... Read More

Java Program to Add two Numbers without using Arithmetic Operator

Shriansh Kumar
Updated on 25-Apr-2023 18:03:03

2K+ Views

Arithmetic operators such as +, -, *, / are used to perform mathematical operations like addition, subtraction, multiplication, modulo and division. We all have done addition of two numbers using + operator but in this article, we are going to see a few java programs that can add two numbers without using arithmetic operators. The following operators best serve this purpose − Increment operators (++) − They are used to increment value of a variable by 1. It is of two types pre increment and post increment. Pre increment operator first increases the value and then, uses it for ... Read More

Java Program to Calculate difference between two Time Periods

Shriansh Kumar
Updated on 25-Apr-2023 18:02:07

484 Views

To work with date and time in java, we need to import java.time, java.util and java.text packages. We are going to use the following classes and methods provided by these packages to calculate difference between two time periods in java − SimpleDateFormat class Date class LocalDate class Period class parse() method between() method As we move further in this article, we will understand the uses of these classes and methods. Approach 1: Using SimpleDateFormat and Date class SimpleDateFormat − It is a class in java that allows us to convert date to string (formatting) and convert string to ... Read More

Java Program to Add Characters to a String

Shriansh Kumar
Updated on 31-May-2024 14:39:39

6K+ Views

Java provides different ways to add characters to a string. We can add characters in the beginning or at the end or at any position according to the need. In this article we are going to see three approaches to do so − Using ‘+’ operator Using methods of StringBuffer and StringBuilder class Using substring() method Approach 1: Using ‘+’ Operator In this section, we will add a character to a string using the concatenation operator. Example public class Main { public static void main(String[] args) { String ... Read More

Java program to check whether the number is divisible by 5

Tapas Kumar Ghosh
Updated on 20-Apr-2023 12:02:43

3K+ Views

In mathematics, the divisibility rule of 5 states that if the number ends with 0 or 5 then it will divisible by 5. There is another way to identify the divisibility rule of 5, if the remainder comes to 0 it returns the number is divisible by 5. The mod(%) operator is normally used in programming when it comes to divisibility. Let’s take an example of this. Given number is 525, the number ends with 5 and it is divisible by 5. Given number is 7050, the number ends with 0 and it is divisible by 5. Given number ... Read More

Java Program to Show Inherited Constructor Calls Parent Constructor By Default

Rudradev Das
Updated on 12-Apr-2023 17:56:38

220 Views

What are the constructors? Constructors are used to initialize the values of a particular object. The default constructor is a constructor which has no parameters. These constructors are used to create objects, which don't have any specific value as the initial. In java, there is a keyword super(). This method is widely used in Java environment when the inheritance applied on a Java code. This super() class is used to call to the constructor of the parent class. The parent class must contain two public constructors which takes two int parameters. As we know the constructors cannot be inherited in ... Read More

Java Program to Show Different Access Levels

Rudradev Das
Updated on 13-Apr-2023 11:54:48

177 Views

Access modifiers are used to set the feature of visibility of some particular classes, interfaces, variables, methods, constructors, data members, and the setter methods in Java programming language. In a Java environment we have different types of access modifiers. Default - If we declare a function, it will visible only within a particular package. Private - If we declare a function, it will visible only within a particular class only. Protected- If we declare a function, it will visible only within a particular package or for all sub classes. Public - If we declare a function, it will visible ... Read More

Java Program to Set Minimum and Maximum Heap Size

Rudradev Das
Updated on 12-Apr-2023 17:51:06

518 Views

The Java heap is a particular memory area which is used to store the objects and represent them as or by an instance in Java Virtual Machine. The Java heap can be shared between two threads as long as the environment is occupied by some running applications. The heaps are sorted in a stack memory and follow the Last In First Out (LIFO) method after the creation of an object in JVM. When the size of a heap memory is compared to stack, the spare objects are cleared by the GarbageCollector automatically. The heap memory are divided into three parts ... Read More

Java Program to Separate the Individual Characters from a String

Rudradev Das
Updated on 12-Apr-2023 17:40:29

4K+ Views

In the field of computer science, the string is a collection of a continuous character data sets. Here in this particular problem we will use some spaces to execute the logic. The individual characters present here in the string can be accessed through its index to perform the code. Here is a general flow to build a logic code to separate the individual characters from a string in a Java environment. Define a string with characters. Print the individual characters present from a given string. Set an initial to zero. Print the string by using the function string.charAt(i). Iterate ... Read More

Java Program to Sort the Array Elements in Descending Order

Rudradev Das
Updated on 12-Apr-2023 17:29:02

2K+ Views

Array is a collection of same data types stored at some contiguous memory locations. The arrays are a class present in java.until package which provides pre-defined sorting with a static manner and no return value. Here is the syntax of the Arrays.sort() method mentioned below − public static void sort(int[] ar, int from_index, int to_index) Here in the above syntax we have ar - short of the array name from_index - a parameter we can use as optional, where the sorting takes a run. to_index - an optional parameter donates the index of the element. Here is ... Read More

Advertisements