Method:
Logic written inside method in java.
Method is same as function of c,c++,python.
Method must have return type either void or String,int etc.
Method always called by object in class.
Syntax:
With void return type:
void method_anyname(){
}
OR
with int return type:
int method_anyname(){
return int;
}
OR
with String return type:
String method_anyname(){
Return String;
}
Declaration of Method:
On the basis of argument , java compiler provide two types of method:
1) Default Method
2) Parameterized Method
Default type
class demo{
//This is default method declaration
void show(){
System.out.println("Hello geeks");
}
public static void main(String args[]){
demo d=new demo();
//This is method call from object
//This is call of default method
d.show();
}
}
Output:
Hello geeks
Parameterized type
Example:
class demo{
//This is default method declaration
void show(){
System.out.println("Hello geeks");
}
//This is parametrized Method
void display(int x){
System.out.println("Parametrized Method : "+x);
}
public static void main(String args[]){
demo d=new demo();
//This is method call from object
//This is call of default method
d.show();
//This is call of parametrized method
d.display(123);
}
}
Output:
Hello geeks
Parametrized Method : 123
Method Return Type:
Method have either return type or not.
By default void print statement in compiler.
But When we use int or String return type then we need to call print statement externally.
void return type:
Syntax:
Syntax:
void method_name(){
System.out.println(“ customized output”);
}
Example
class demo{
//void is return type
//it returns value -Hello geeks to compiler by System.out.println
void show(){
System.out.println("Hello geeks");
}
public static void main(String args[]){
demo d=new demo();
//This is call of void return type of method
d.show();
}
}
Output:
Hello geeks
Int or String method return type:
Int method return type:
Syntax:
Syntax:
int method_name(){
return int x;
}
Example
class demo{
//void is return type
//it returns value -Hello geeks to compiler by System.out.println
void show(){
System.out.println("Hello geeks");
}
//int is return type
//it returns value of x but does not print on console
//Reason--System.out.println is not declare here
//For printing this value we need to call externally
int display(){
int x=123;
return x;
}
public static void main(String args[]){
demo d=new demo();
//This is call of void return type of method
d.show();
//This is method call of int type --externally
System.out.println(d.display());
}
}
Output:
Hello geeks
123
String method return type:
Syntax:
String method_name(){
return String x;
}
Example
class demo{
//void is return type
//it returns value -Hello geeks to compiler by System.out.println
void show(){
System.out.println("Hello geeks");
}
//String is return type
//it returns value of name but does not print on console
//Reason--System.out.println is not declare here
//For printing this value we need to call externally
String display(){
String name="bhanu";
return name;
}
public static void main(String args[]){
demo d=new demo();
//This is call of void return type of method
d.show();
//This is method call of String type --externally
System.out.println(d.display());
}
}
Output:
Hello geeks
bhanu