Both pre-increment and post-increment are used to represent the expression of increasing a value by adding 1. Their behavior are different because pre-increment (++i) increases the value before it is used in an expression, while post-increment (i++) increases it after. Below is the key-terms of pre-increment and post-increment in C/C++: Pre-increment (++i) : Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) : After assigning the value to the variable, the value is incremented. Syntax of Using Pre and Post Increment Operators The following is the basic syntax of pre-increment and post-increment ... Read More
There can be multiple ways to reverse a string in C and C++. In this article, we will learn reversing a string using different approaches with the appropriate examples. Consider the below example with input and output scenarios: Input // characters representation from left to right inp = "Tutorialspoint" Output // characters representation from right to left out = tniopslairotuT Now, we have following approaches to solve the reverse of a given approach: Using Two Pointer Using reverse() Function Using strlen() Function Reverse String ... Read More
A structure is a collection of items of different data types. It is very useful in creating complex data structures with different data type records. A structure is defined with the struct keyword. An example of a structure is as follows: struct DistanceFI { int feet; int inch; }; The above structure defines a distance in the form of feet and inches. Example of Adding Two Distances (inch-feet) Using Structure The program uses a structure named DistanceFI to represent a distance in terms of feet and inches. It creates two different ... Read More
Lexicographical order denotes the way the words are ordered in a list, based on alphabetical order according to their alphabets. For example: List of words: Harry Adam Sam Lexicographical order of words: Adam Harry Sam Sorting String Based on Lexicographical Order To implement the string in lexicographical order, use the two different iterators: one to point at the current string, and the other to compare it with the next strings in the array. If the first iterator points to a string that is greater than the one pointed to by the second iterator, then we swap ... Read More
The default and parameterized constructors are two types of Constructor in Java. The constructor is a special member of a Java class whose name is the same as the class name. It is used to assign values to a class variable at the time of object creation. In this article, we are going to discuss the difference between default and parameterized constructors. Default Constructor When we do not add a constructor to a Java class. The compiler adds a default constructor implicitly. It accepts 0 arguments. If we do not initialize the instance variables of a class, a default constructor will ... Read More
In Java, an ArrayList is a class of the List interface, which stores elements of similar data types and can be null while creating. A sub-list is the view of a portion (or part) of an ArrayList. For example, if the given array list is {1, 2, 3, 4, 5}, then the possible sub-lists can be {1, 2}, {1, 2, 3}, {2, 3, 4}, {4, 5}, etc.The List interface in Java provides a built-in method named subList(), which directly returns a sub-list from the given ArrayList. Sublist from an ArrayList using the subList() Method The subList() method of the List interface returns a portion ... Read More
In Java, a class is a datatype which defines properties (variables) and behaviors (methods) of an object. Defining an object does not consume memory; only its object or instance does.Depending on the requirement, we will create various types of classes in Java. In this article, we are going to discuss them. Types of classes in Java The Java class is classified into different types based on its methods, as shown in the list given below: Concrete class Abstract class Final class POJO class Static class Inner Class Wrapper Class Singleton Class Concrete class Any normal class which does ... Read More
Every Java object has two very important methods, equals() and hashCode(), and these methods are designed to be overridden according to their specific general contract. Since the Object class is the parent class of every class, the default implementation of the equals() and hashCode() methods is already present in each class. However, we need to override these methods based on the requirement. Let's discuss the contract between equals() and hashCode() methods in Java. But before that, we need to understand these methods. The hashCode() Method The hashCode() method returns an integer value, which is referred to as the hash code value ... Read More
In Java, interfaces are used to achieve abstraction and multiple inheritance. They can contain methods and variables, but there are specific rules about how those members should behave. For example, all variables declared in an interface are public, static, and final by default, even if you don't use these keywords while defining variables. To understand the reason behind it, we first need to understand what static and final mean in Java. What is a Static Variable in Java? The static variables are defined using the static keyword. These variables belong to the class rather than to any specific object, which ... Read More
In Java, ArrayStoreException is a public class that extends the RuntimeException class of the java.lang package. It is thrown by the Java Virtual Machine when a runtime error occurs. Since it is an unchecked exception, it does not require an explicit declaration in a method or a constructor's throws clause. Let's understand why the ArrayStoreException is thrown and how we can avoid it. Reason for ArrayStoreException in Java As mentioned earlier, ArrayStoreException is an unchecked exception, and it can occur when we try to store an object of one type in an array of a different type. Usually, one would ... Read More