Basic Concept of Method In java
Introduction to the method
A method is a set of code which is referred to by name and can be called
(invoked) at any point in a program simply by utilizing the method's
name. It is also called as sub-program/function it often returns value.
The method has its own name. When that name is called in a program, the execution of the program goes inside the body of that
method. When the method is finished executing, it returns to the area of
the program from which it was called, and the program continues on to the next
line of code.
Process of calling method is given below: -
Good programmers write in a modular fashion which allows for
several programmers to work independently on separate concepts which can be
assembled at a later date to create the entire project. The use of
methods will be our first step in the direction of modular programming.
Methods are time savers; in that, they allow for the repetition of
sections of code without retyping the code. In addition, methods can be
saved and utilized again and again in newly developed programs.
You are using the method when
you write the code
System.out.print( ) and System.out.println( ).
System.out.print( ) and System.out.println( ).
There are two basic
types of methods:
Built-in: Build-in methods are part of
the compiler package, such as System.out.println( ) and System.exit(0).
|
User-defined: User-defined methods
are created by the programmer. These methods take-on names that programmer
assign to them and perform tasks that they are created for.
|
How to invoke (call) a method (method invocation):
When a method is called(invoked), a request is made to perform
some action, such as setting a value, printing statements, returning an answer,
etc. The code to invoke the method contains the name of the method to be
executed and any needed data that the receiving method requires. The
required data for a method is specified in the method's parameter list.
Consider this method that we have already been using from Breezy;
String str = str1.toUpperCase();
//Converts all string contained in str1 to Upper class and store in variable
str.
The method name is .toUpperCase() which is defined in the class “String”. Since the method is
defined in the class String, we must call String class before coding in
current class. This particular method returns all the lowercase letter
contained in the str1 variable is converted in uppercase and stored in str
variable.
You invoke (call) a method by writing down the calling object
followed by a dot, then the name of the method, and finally a set of
parentheses that may (or may not) have information for the method.
Comments
Post a Comment