Return type / return statement in java example

Return type in java: return statement 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.
  • In Java, the return statement is used to exit a method and return a value to the calling method. The value returned can be of any data type that is specified in the method's return type. 
  • For example, if a method has a return type of int, it can return an integer value. Here's an example of a simple method that uses the return statement to return an int value:


public int add(int a, int b) {
    int sum = a + b;
    return sum;
}

  • In this example, the add method takes two int parameters, a and b, and returns the sum of the two values. The method uses the return statement to return the value of the sum variable back to the calling method.
  • You can also use return statement without returning any value, in this case method should have void return type.


public void printHelloWorld() {
    System.out.println("Hello, World!");
    return;
}

  • In this case the method printHelloWorld doesn't return any value and the calling method doesn't need to assign the return to any variable, just invoking the method will do the job.
  • It is also worth noting that when a return statement is executed, it immediately exits the current method, regardless of where it is in the method's execution flow. So, any code after a return statement will not be executed.

Type of declaration of methods based on return type and arguments:

1.Method with out return type  and without arguments: return statement in java


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(){
  5.  
  6. int a=40;
  7. int b=50;
  8. int c=a+b;
  9. System.out.println(c);
  10.  
  11. }

  12. public static void main(String args[]) // ->method prototype.
  13.  
  14. sample obj= new sample();
  15. obj.add();
  16.  
  17.  
  18.  }
  19. }

2.Method with out return type and with arguments.

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(int a, int b){
  5.  
  6. int c=a+b;
  7. System.out.println(c);
  8.  
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. obj.add(13,24);
  14.  
  15.  
  16.  }
  17. }

3.Method with return type  and without arguments.


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(){
  5. int a=40;
  6. int b=50;
  7. int c=a+b;
  8. return c;
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. int x=obj.add(); 
  14. System.out.println(x);
  15.  
  16.  }
  17. }

4.Method with return type and with arguments.

 

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(int a, int b){
  5.  
  6. int c=a+b;
  7. return c;
  8. }

  9. public static void main(String args[]) // ->method prototype.
  10.  
  11. sample obj= new sample();
  12.  
  13. int x=obj.add(1,2);
  14. System.out.println(x);
  15.  
  16.  }
  17. }


If Else Statment in Java

If statement :
  • if statement in java is decision making statement in java.
  • if statement will have a condition and if that condition is true then the corresponding block will be executed.
  • if(condition){ //  }
If condition syntax:



  1. if(condition){
  2. //  statements
  3. }
 Sample program on if statement:


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8. if(true){
  9.  
  10.  System.out.println("if condition executed");
  11.  
  12. }
  13.  
  14. }

Output:
  1. if condition executed

Java program to compare two numbers in java


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class simpleIfExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y)
  12.     System.out.println(x+ " is equal to " + y);
  13. }
  14.  
  15. if(x>y){
  16.     System.out.println(x+ " is greater than " + y);
  17. }
  18.  
  19. if(x<y){
  20.     System.out.println(x+ " is less than " + y);
  21. }
  22. }

Output:
  1. 37 is equal to 37



 Else Statement:

  •  if(condition){ //} else { // }

Java program to compare two numbers in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y){
  12.     System.out.println(x+ " is equal to " + y);
  13. }else if(x>y){
  14.     System.out.println(x+ " is greater than " + y);
  15. }else{
  16.     System.out.println(x+ " is less than " + y);
  17. }
  18. }



Output:
  1. 37 is equal to 37


Java program to find leap year or not in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.     int year = 2015;                      
  9.   //if year is divisible by 4, it is a leap year

  10.  if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
  11.  System.out.println("Year " + year + " is a leap year");
  12.  else
  13.       System.out.println( year + " is not a leap year");
  14.   
  15.  
  16. }

Output:
  1. 2015 is not a leap year


Java program to find even or odd using if else statment


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseEvenOddExample { 
  5.  
  6. public static void main(String[] args) {
  7.   
  8. int a=10;
  9. if((a%2)==0){
  10.  
  11.  System.out.println(a+"is even number");
  12.  
  13. }else{ 

  14.     System.out.println(a+"is odd number");
  15.  
  16. }
  17.  
  18. }

Output:
  1. 10 is even number

Top 25 Java Basic Interview Questions for Freshers

Imp Note: Openings for 2024 & 2025 pass outs (MNC) in india. Send your profile to Instanceofjava@gmail.com subject should be: yourName_fresher_resume

Java Freshers Interview Questions



1.What is the most Important feature of java?

  • Java is platform independent language.

2.What do you mean by Platform Independence?

  • Platform Independence  you can run and compile program in one platform and can execute in any other platform.


3.What is JVM(Java Virtual Machine)?
  • JVM is Java Virtual Machine which is a run time Environment for the compiled java class.

4.What is JIT Compiler?

  • Just-In-Time(JIT) compiler is  used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time,

5.Are JVM's Platform Independent?

  •  JVM'S are not platform Independent. JVM'S are platform Specific.

6.What if main method declare as private?

  • No,It will compile fine but in run time it will error like main method should be in public.cannot find main method.

7.What is platform?

  • A platform is basically the hardware or software environment in which a program runs.
  • There are two types of platforms software and hardware.
  • Java provides software-based platform.

8.What all memory areas are allocated by JVM?

  • Heap, Stack, Program Counter Register and Native Method Stack

9.What is the base class of all classes?

  • java.lang.Object

10.What is javac ?

  • It produces the java byte code from *.java file.
  • It is the intermediate representation of your source code that contains instructions.

11.Can we mark constructors final?

  • No, Constructor cannot be declared final.

12.What are two different ways to call garbage collector?

  • System.gc() OR Runtime.getRuntime().gc().

13.Can we override a static method?

  • No, we cannot override a static method. Static means class level.

14.Use of finalize() method in java?

  •  finalize() method is used to free the allocated resource.

15.Is it possible to overload main() method of a class?

  • Yes, we can overload main() method as well. 
  • But every time public static main(String[] args) will be called automatically by JVM.
  • Other methods need to call explicitly.

16. Does Java support operator overloading?

  • Operator overloading is not supported in Java.

17.Can I declare a data type inside loop in java?

  • Any Data type declaration should not be inside the loop. Its possible but not recommended.


18.List two java ID Es?

  • 1.Eclipse, 2.Net beans and 3.IntelliJ

19.Can we inherit the constructors?

  • No, we cannot inherit constructors. 
  • We can call super class constructors from subclass constructor by using super() call.

20.Can this keyword be assigned null value?
  • No, this keyword cannot have null values assigned to it.
21.Basic java interview programs for freshers 
22.Java Quiz for freshers

23. Top 25 java interview Questions for freshers  

24.Top 15 oops concepts interview questions and answers

25. Top 40 Java interview Programs asked in interview for freshers

You Might Like:



Java programming interview questions
  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. simple java interview questions on Super keyword
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Long Wrapper Class

Long Wrapper Class:

  • Long Class is presented in java.lang package.
  • java.lang.Long class is used to represent primitive Long Value to Long object.

Long Class :

Float Class Definition

  1. public final class Long
  2. extends Number 
  3. implements Comparable<Long>



Constructors:

1.Long(long value):
  • The Constructor Long(long value) represents the specified long argument.

2.Long(String s):
  • The Constructor Long (String s) represents the long value indicated by the String parameter.

Methods:

1.public static int bitCount(long i); Program:

  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class LongExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8.   long l = 4561; 

  9.  System.out.println("Number = " + l);
  10.  System.out.println("Binary = " + Long.toBinaryString(l));
  11.  System.out.println("Number of one bits = " + Long.bitCount(l));
  12.  
  13. }
  14. }

Output:
  1. Number = 4561 
  2. Binary = 1000111010001 
  3. Number of one bits = 6

2.public byte byteValue():

This method returns value of long as byte.

Program:

 

  1. package com.instanceofjava;
  2.  import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {

  5.  Long obj = new Long(30);

  6.  byte b = obj.byteValue();
  7.  System.out.println("Value of b = " + b);
  8.  
  9.  }
  10. }


Output:
  1. Value of b = 30

3.public int compareTo(Long anotherLong):

Program:

  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {
  5.  
  6.    Long a = new Long(63255);
  7.   Long b = new Long(71678);
  8.  
  9.    int result=  a.compareTo(b);
  10.    ifresult> 0) {
  11.    System.out.println("a is greater than b");
  12.    }
  13.    else ifresult< 0) {
  14.    System.out.println("a is less than b");
  15.    }
  16.    else {
  17.    System.out.println("a is equal to b");
  18.    }
  19.  
  20.    }
  21. }




Output:
  1. a is less than b

4.public static Long decode(String nm) throws NumberFormatException:

Program:


  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample{

  4.  public static void main(String[] args) {

  5.    Long l = new Long(10);
  6.    String str = "57820";
  7.    System.out.println("Number = " + l.decode(str));
  8.  
  9.   }
  10. }

Output:
  1. 57820




Float Wrapper Class

  • Float class is presented in java.lang package
  • java.lang.Float class is used to represent primitive float value to Float object.

Float Class Definition

  1. public final class Float
  2. extends Number
  3. implements Comparable<Float>

Float Class Constructors

1.public Float(double value)


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float(22.56d);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


2.public Float(float value)


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float(22.56f);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


3.public Float(String s) throws NumberFormatException


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float("22.56f");
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56

Float Class Methods:

1.public static Float valueOf(String s) throws NumberFormatException 

  • This method used to convert string value to float value. If the string contains non parsable value then it throws NumberFormatException 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="56.32";

  8.  Float f = Float.valueOf(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 56.32

2.public String toString()
  • This method returns String value from Float Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.   
  7.  Float f = new Float("12.3")
  8.  String str=f.toString();

  9.   System.out.println(str);
  10.  
  11.  
  12. }
  13. }



Output:
  1. 12.3

3.public static float parseFloat(String s) throws NumberFormatException

  • This method returns float value from string object. If string doesnt contains parsable float value then throws NumberFormatException 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="20";

  8.  Float f = Float.parseFloat(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 20.0



Character Class

  • Character class is presented in java.lang package
  • java.lang.Character class is used to represent primitive character value to character object.

Character Class Definition:

  1. public final class Character
  2.  extends Object 
  3. implements Serializable, Comparable<Character>

Character Class Constructors:

1. public Character(char value)
  • Character class having only one constructor which accepts char primitive value.

Java Program to convert char primitive value to Character Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Character ch = new Character ('a');
  8.   System.out.println(ch);
  9.  
  10.  
  11. }
  12. }

Output:
  1. a

 Character Class Methods:

1.public char charValue()

  • This method returns the primitive char value from the Character object.

Java code to get char primitive value from Character Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterCharValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Character ch = new Character ('a');
  8.   char c=ch.charValue();

  9.  System.out.println(c);
  10.  
  11. }
  12. }

Output:
  1. a

2.public static String toString(char c) :

  • toString(char c) method present in Character class accepts "char" primitive values and returns String objects representing that character.
Java code to convert char primitive value to String object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharactertoStringDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='I';
  7.  
  8.   String str="";
  9.  
  10.   str=Character.toString(ch);
  11.  
  12.   System.out.println(str);
  13. }
  14. }

Output:
  1. I

3.public static Character valueOf(char c)

  • valueOf(char c)  is static method present in Character class used to convert char value to Chraracter object.
  • It accepts char primitive value and returns Character object

Java code to convert char primitive value to String object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharactervalueOfDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='S';
  7.  
  8.   Character chobj='';
  9.  
  10.   chobj=Character.valueOf(ch);
  11.  
  12.   System.out.println(chobj);
  13. }
  14. }

Output:
  1. S
4.public static char toUpperCase(char ch)

  • toUpperCase(char ch) static method present in Character class used to convert a character to uppercase.
  • It accepts a character and returns same character in uppercase.


Java code to convert char primitive value to uppercase

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacteruppercaseDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='a';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }



Output:
  1. A

5.public static char toLowerCase(char ch) 

  •  toLowerCase(char ch) static method present in Character class used to convert a character to lowercase.
  • It accepts a character and returns same character in lowercase.


Java code to convert char primitive value to lowercase

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterlowercaseDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='L';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }

Output:
  1. l



Integer Wrapper class

  • Integer  class is present in java.lang package.
  • java.lang.Integer  is used to represent integer primitive value in form of object.

Class Definition

  1. public final class Integer
  2. extends Number 
  3. implements Comparable<Integer>

Integer Class Constructors

1.public Integer(int value):
  • It takes int primitive value as an argument.
Java code to convert int primitive to Integer object


  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerDemo {
  4.  
  5. public static void main(String[] args) {
  6.  


  7.  Integer obj=new Integer (37);
  8.  
  9.  System.out.println(obj);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 37

2.public Integer(String s)  throws NumberFormatException:

  • It takes String value as an argument. 

Java code to convert String to Integer object


  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerDemo {
  4.  
  5. public static void main(String[] args) {
  6.  


  7.  Integer obj=new Integer ("123");
  8.  
  9.  System.out.println(obj);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 123

Integer Class Methods:

3.public static int parseInt(String s) throws NumberFormatException :
  • This method takes String as an argument and converts to the int value
  • Static method present in Integer Class used to parse the string to the corresponding int value.in
  • We can use this method by class name Integer.parseInt("number").
  • If the String contains non numeric value then this method throws NumberFormatException 

 Java Program to parse a string or convert String to Integer value

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerParseIntDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  int x= Integer.parseInt("34");
  8.   System.out.println(x);
  9.  
  10.  int y=  Integer.parseInt("56");
  11.    System.out.println(y);
  12.  
  13.  int z=  Integer.parseInt("98");
  14.  System.out.println(z);
  15.  
  16. }
  17. }

Output:
  1. 34
  2. 56
  3. 98
4.public int intValue()

  • This method returns the int value from the Integer Object.
  • obj.intValue()- returns a int value of Object obj.



Java Program to get int value from a Integer Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerIntValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Integer obj= new Integer(25);

  8.  
  9.  int y= obj.intValue();
  10.  
  11.  System.out.println(y);
  12.  
  13. }
  14. }

Output:
  1. 25

3.public int compareTo(Integer anotherInteger) 

  •  This method is used to compare two Integer Objects and returns int value
  •  0- Integer is equal to the argument Integer
  • < 0 if this Integer is numerically less than the argument Integer
  • > 0 if this Integer is numerically greater than the argument

Java Program to compare two Integer Objects.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegercompareToDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("34");
  8.    Integer obj2 = new Integer("08");
  9.  
  10.   int val =  obj1.compareTo(obj2);
  11.  
  12.    if(val > 0) {
  13.  
  14.    System.out.println("obj1 is greater than obj2");
  15.  
  16.    }
  17.    else if(val < 0) {
  18.  
  19.    System.out.println("obj1 is less than obj2");
  20.  
  21.    }
  22.    else {
  23.  
  24.    System.out.println("obj1 is equal to obj2");
  25.  
  26.    }
  27. }
  28. }

Output:
  1. obj1 is greater than obj2


4.public float floatValue()

  • This method used to convert integer value to float value.

Java Program to convert Integer to float

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerfloatValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("34");
  8.    Integer obj2 = new Integer("23");
  9.    
  10.     float f 1= obj1.floatValue();
  11.     float f 2= obj2.floatValue();
  12.  
  13.    System.out.println(f1);
  14.    System.out.println(f2); 

  15. }
  16. }

Output:
  1. 34.0
  2. 23.0

5.public String toString()

  • This method used to convert Integer to string object.

Java Program to convert Integer to String Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerfloatValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("12");
  8.    Integer obj2 = new Integer("34");
  9.    
  10.     String  str1= obj1.toString();
  11.     String  f 2= obj2.toString();
  12.  
  13.    System.out.println(str1);
  14.    System.out.println(str2); 

  15. }
  16. }

Output:
  1. 12
  2. 34
Select Menu