Adding Two int Values Using Integer Objects - Java Language Basics

Java examples for Language Basics:int

Description

Adding Two int Values Using Integer Objects

Demo Code

public class Main {
  public static Integer add(Integer a, Integer b) {
    int aValue = a.intValue();
    int bValue = b.intValue();
    int resultValue = aValue + bValue;
    Integer result = Integer.valueOf(resultValue);
    return result;
  }/*from   www.  j  a  v  a  2  s  .c o m*/

  public static void main(String[] args) {
    int iValue = 2;
    int jValue = 3;
    int kValue; /* will hold result as int */

    // Box iValue and jValue into Integer objects
    Integer i = Integer.valueOf(iValue);
    Integer j = Integer.valueOf(jValue);

    // Store returned value of the add() method in an Integer object k
    Integer k = Main.add(i, j);

    // Unbox Integer object's int value into kValue int variable
    kValue = k.intValue();

    System.out.println(iValue + " + " + jValue + " = " + kValue);
  }
}

Result


Related Tutorials