AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Java constructor in a method10/29/2023 In the above method signature, we see that the method signature has access specifier public, non-access modifier static, return type int, method name max(), parameter list (int a, int b). When we go through the link and see the max() method signature, we find the following: We can also see the method signature of any predefined method by using the link. The max() method is a method of the Math class that returns the greater of two numbers. The print() method is a method of PrintStream class that prints the result on the console. We have used these methods directly without declaration because they are predefined. In the above example, we have used three predefined methods main(), print(), and max(). Let's see an example of the predefined method. For example, print("Java"), it prints Java on the console. It prints the statement that we write inside the method. Such as print() method is defined in the java.io.PrintStream class. When we call any of the predefined methods in our program, a series of codes related to the corresponding method runs in the background that is already stored in the library.Įach and every predefined method is defined inside a class. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. We can directly use these methods just by calling them in the program at any point. It is also known as the standard library method or built-in method. In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also possible that a method has the same name as another method name in the same class, it is known as method overloading. Multi-word method name: areaOfCircle(), stringComparision() In the multi-word method name, the first letter of each word must be in uppercase except the first word. If the method name has more than two words, the first name must be a verb followed by adjective or noun. While defining a method, remember that the method name must be a verb and start with a lowercase letter. It is enclosed within the pair of curly braces. It contains all the actions to be performed. Method Body: It is a part of the method declaration. If the method has no parameter, left the parentheses blank. It contains the data type and variable name. Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). It must be corresponding to the functionality of the method. Method Name: It is a unique name that is used to define the name of a method. If the method does not return anything, we use void keyword. ![]() It may have a primitive data type, object, collection, void, etc. Return Type: Return type is a data type that the method returns. It is visible only from the same package only.
0 Comments
Read More
Leave a Reply. |