This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: return type in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
we will use methods to do a particular task after completion of task if we want to return something to the calling place these return types will be used.
Based on the type of data to be returned will mention it as int , char , float double etc as return type in method signature and return statement should be the last statement of the method body.
Type of declaration of methods based on return type and arguments:
1.Method with out return type and without arguments.
package com.instanceofjava;
class sample{
public void add(){
int a=40;
int b=50;
int c=a+b;
System.out.println(c);
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
obj.add();
}
}
2.Method with out return type and with arguments.
package com.instanceofjava;
class sample{
public void add(int a, int b){
int c=a+b;
System.out.println(c);
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
obj.add(13,24);
}
}
3.Method with return type and without arguments.
package com.instanceofjava;
class sample{
public int add(){
int a=40;
int b=50;
int c=a+b;
return c;
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
int x=obj.add();
System.out.println(x);
}
}
4.Method with return type and with arguments.
package com.instanceofjava;
class sample{
public int add(int a, int b){
int c=a+b;
return c;
}
public static void main(String args[]) // ->method prototype.