Java

What are predefined methods in java

In Java, a method is a set of statements that are used to perform a certain task. Java methods ensure the reusability of the code and a method comes into action only when someone calls it. In Java, there are some in-built methods that can be used anytime, anywhere, and at any point in a program known as predefined methods. Moreover, java programming allows us to create our own methods where we can specify our own logic depending upon the specific task. Such types of methods are referred to as user-defined methods.

This write-up will cover the below-mentioned concepts of the Java predefined methods:

  • What are Predefined Methods in Java
  • How to use Predefined Methods in Java
  • How Predefined Methods Work in Java
  • Examples

So, let’s begin!

What are Predefined Methods in Java

Java offers a wide range of methods that are already defined in the java class libraries, referred to as java predefined or built-in methods. In simple words, we can say that the methods that don’t need to be created by the user/programmer are called predefined methods in java.

How to use Predefined Methods in Java

The predefined methods are ready-to-use methods, which means these methods can be invoked directly anywhere in the program. Java provides numerous predefined methods such as equals(), max(), length(), print(), and so on.

How Predefined Methods Work in Java

When we invoke a Java predefined method, a block of code(already defined in the library) associated with that method runs behind the scene and produces the output accordingly. In Java, every predefined method belongs to some specific class, for instance, the length() method belongs to the Java String class, the print() method belongs to the PrintStream class, and so on.

Let’s consider some example programs to understand how predefined methods work in java:

Example1

In this program, we will utilize the println() method to print some text on the console/screen:

public class PredefinedMethods {
    public static void main(String[] args) {
        int number = 572;
        String string = "Welcome to LinuxHint";
        float floatNumber = 5.14f;
        System.out.println("Numeric Value: "+ number);
        System.out.println("String Value: "+ string);
        System.out.println("Floating Point Value: "+ floatNumber);
    }    
}

In this program we utilized println() method to print different type of data i.e. integer, string, and float:

In this way, we can print any type of data on the console using println() method.

Example2

In this example program, we will find the square root of a number using a predefined method of Java Math class named sqrt():

public class PredefinedMethods {
    public static void main(String[] args) {
        int number = 572;
        System.out.println("Square Root of the given number: "+ sqrt(number));
    }    
}

The sqrt() method will produce the following output:

The output verifies that the sqrt() method generates the appropriate result.

Example3

Let’s consider the below code block to understand the working of another predefined method named equals():

public class PredefinedMethods {
  public static void main(String[] args) {
    String string1 = "Welcome to Linuxhint";
    String string2 = "Linuxhint.com";
    String string3 = "Welcome to Linuxhint";
    System.out.println("String1 equals to String2: " + string1.equals(string2));
    System.out.println("String2 equals to String3: " + string2.equals(string3));
    System.out.println("String1 equals to String3: " + string1.equals(string3));
  }
}

In the above code snippet, we performed strings comparison using the equals() method:

The above snippet verified the working of the equals() method.

Example4

In this example we will find a maximum number using the max() method:

public class PredefinedMethods {
  public static void main(String[] args) {
        int number1 = 472;
        int number2 = 572;
        System.out.println("Maximum Number: " + Math.max(number1, number2));
    }
}

The max() method will compare two numbers, and consequently, it will return the maximum number:

The output verified that the max() method provided accurate results.

This is how we can use any of the predefined methods depending upon the scenario.

Conclusion

In java, the methods that are ready-to-use are known as the predefined methods. These methods come into action only when someone calls them and can be invoked directly anywhere in the program. Java provides numerous predefined methods such as equals(), max(), length(), print(), and so on. All these methods serve different functionalities such as the println() method to print some text on the console, the max() method compares the two numbers and returns a maximum number, etc. This write-up explained various aspects of java predefined methods with the help of some relevant examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.