Articles on Trending Technologies

Technical articles with clear explanations and examples

Sort subset of array elements in Java

George John
George John
Updated on 11-Mar-2026 5K+ Views

The java.util.Arrays.sort() method can be used to sort a subset of the array elements in Java. This method has three arguments i.e. the array to be sorted, the index of the first element of the subset (included in the sorted elements) and the index of the last element of the subset (excluded from the sorted elements). Also, the Arrays.sort() method does not return any value.A program that demonstrates this is given as follows −Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int arr[] = { 1, 9, 7, 3, 2, 8, 4, ...

Read More

Increment a Date using the Java Calendar Class

Samual Sam
Samual Sam
Updated on 11-Mar-2026 392 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the date using the add() method and Calendar.DATE constantcalendar.add(Calendar.DATE, 2);The following is the complete exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing date by 2       calendar.add(Calendar.DATE, 2);       System.out.println("Updated Date = " + calendar.getTime());    } }OutputCurrent Date ...

Read More

Java Program to decrement a Date using the Calendar Class

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 235 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the date using the add() method and Calendar.DATE constant. Set a negative value since we are decrementing the datecalendar.add(Calendar.DATE, -3);The following is an exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing date by 3       calendar.add(Calendar.DATE, -3);       System.out.println("Updated ...

Read More

Get the declared method by name and parameter type in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 1K+ Views

The declared method can be obtained by name and parameter type by using the java.lang.Class.getDeclaredMethod() method. This method takes two parameters i.e. the name of the method and the parameter array of the method.The getDeclaredMethod() method returns a Method object for the method of the class that matches the name of the method and the parameter array that are the parameters.A program that gets the declared method by name and parameter type using the getDeclaredMethod() method is given as follows −Examplepackage Test; import java.lang.reflect.*; public class Demo {    public String str;    private Integer func1() {       ...

Read More

Increment a Month using the Calendar Class in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 561 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the month using the add() method and Calendar.MONTH constant −calendar.add(Calendar.MONTH, 2);The following is an exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing Month by 2       calendar.add(Calendar.MONTH, 2);       System.out.println("Updated Date (+2 Months) = " + calendar.getTime());    } ...

Read More

Demonstrate getting the immediate superclass information in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 202 Views

The immediate superclass information of any entity such as an object, class, primitive type, interface etc. can be obtained using the method java.lang.Class.getSuperclass().A program that demonstrates this is given as follows −Examplepackage Test; import java.lang.*; class Class1{ } class Class2 extends Class1{ } public class Demo { public static void main(String args[]) { Class1 obj1 = new Class1(); Class2 obj2 = new Class2(); Class c; c = obj2.getClass(); ...

Read More

Reverse words in a given String in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 5K+ Views

The order of the words in a string can be reversed and the string displayed with the words in reverse order. An example of this is given as follows.String = I love mangoes Reversed string = mangoes love IA program that demonstrates this is given as follows.Exampleimport java.util.regex.Pattern; public class Example {    public static void main(String[] args) {       String str = "the sky is blue";       Pattern p = Pattern.compile("\s");       System.out.println("The original string is: " + str);       String[] temp = p.split(str);       String rev = ""; ...

Read More

Decrement a Month using the Calendar Class in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 280 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementingcalendar.add(Calendar.MONTH, -2);The following is an exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing Month by 2       calendar.add(Calendar.MONTH, -2);       System.out.println("Updated Date ...

Read More

Check whether String is an interface or class in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 693 Views

The method java.lang.Class.isInterface() is used to find if String is an interface or class in Java. This method returns true if String is an interface, otherwise it returns false.A program that demonstrates this is given as follows −Examplepackage Test; import java.lang.*; public class Demo {    public static void main(String[] args) {       Class c = String.class;       boolean interfaceFlag = c.isInterface();       System.out.println("This is an Interface? " + interfaceFlag);    } }OutputThis is an Interface? falseNow let us understand the above program.The isInterface() method is used to find if String is an interface ...

Read More

Why does C++ require a cast for malloc() but C doesn't?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

In C language, the void pointers are converted implicitly to the object pointer type. The function malloc() returns void * in C89 standard. In earlier versions of C, malloc() returns char *. In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using explicit casting.The following is the syntax of allocating memory in C language.pointer_name = malloc(size);Here, pointer_name − Any name given to the pointer.size − Size of allocated memory in bytes.The following is an example of malloc() in C language.Example#include #include int main() {    int n = 4, i, ...

Read More
Showing 29721–29730 of 61,297 articles
Advertisements