0
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int a = 1;
    Integer b = new Integer("1");
    Integer c = a;
    int result = b + a;
    int[] f = new int[1];


    System.out.println(result);
    check(b);
    check1(f);
    Integer K = new Integer(10);
    int J = K.intValue();
    System.out.println(b + " " + f[0] );
    if(b.equals(a)) 
        System.out.println(a);
}

static void check(Integer d)
{
    d = 20;
}

static int[] check1(int[] a)
{
    a[0] = 20;

    return a;
}

first I'm curious to use primitive variable and reference variable.

let me know how to show

System.out.println(b)

output

20

array is reference type, and int is primitive type.

and Integer is reference type. but It is not changed from 1 to 2

Devzone
  • 73
  • 8
  • The fact that `Integer` is a reference type does not mean that it is passed by reference - in fact, everything is passed by value in Java; including references. (Note: Passing a reference by value is not the same as passing by reference!). – Jesper Nov 05 '18 at 10:51
  • as you said, means having a difference with refence and value reference. please let me know how to get output as 20 – Daniel CHOI Nov 05 '18 at 10:58
  • In the method `check()` you could do the same trick as you did in `check1()` by using an array - there is no way to modify the `Integer` as you do now in `check()` because Java passes by value, and not by reference. – Jesper Nov 05 '18 at 12:59

0 Answers0