Return type in java:
- Basically return type is used in java methods.
- Method signature includes this return type.
- public int show(){ // }
- 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.
- {
- sample obj= new sample();
- int x=obj.add(1,2);
- System.out.println(x);
- }
- }
Best website to clear doubts.
ReplyDeleteaweswome
ReplyDeletethanks
ReplyDeleteit simple logic and easy way of finding solutions,super
ReplyDelete