Value types and reference types

Figure 330. Value vs. reference type variables Slide presentation
Value type: Holding data in associated memory location
  • byte

  • short

  • int

  • long

  • float

  • double

  • char

  • boolean

Reference type: Holding a reference to an object

Array or class instances i.e. String, java.util.Scanner etc.


Figure 331. Different behaviour! Slide presentation
Value type
int a = 1;
int b = a;
a = 5;
System.out.println("a=" + a);
System.out.println("b=" + b);
a=5
b=1
Reference type
StringBuffer r = new StringBuffer("Joe");
StringBuffer s = r;
r.append(" Simpson");
System.out.println("r=" + r);
System.out.println("s=" + s);
r=Joe Simpson
s=Joe Simpson

Figure 332. Value variable Details Slide presentation

Figure 333. Reference variable Details Slide presentation

Figure 334. Only «call-by-value» in Java Slide presentation
public static void main(String[] args) {
  int value = 3;
  System.out.println(
    "Before printDuplicateValue: "
    + value);
  printDuplicateValue(value);
  System.out.println(
    "After printDuplicateValue: "
     + value);
}
static void printDuplicateValue(int n) {
  n = 2 * n;
  System.out.println(
  "printDuplicateValue: " + n);
}
Before printDuplicateValue: 3
printDuplicateValue: 6
After printDuplicateValue: 3

Figure 335. «call-by-value» details Slide presentation

Figure 336. «call-by-reference» for objects? Slide presentation
public static void main(String[] args) {
  StringBuffer buffer = new StringBuffer("My");
  System.out.println("Before duplicateString: "
    + buffer);
  duplicateString(buffer);
  System.out.println("After duplicateString: "
    + buffer);
}
static void duplicateString(StringBuffer b) {
    b.append(b); // Append self
}
Before duplicateString: My
After duplicateString: MyMy  

Figure 337. «call-by-reference» details Slide presentation

Figure 338. No «call-by-reference» in Java! Slide presentation
public static void main(String[] args) {
  StringBuffer buffer = new StringBuffer("My");
  System.out.println("Before duplicateString: "
    + buffer);
  replaceString(buffer);
  System.out.println("After duplicateString: "
    + buffer);
}
static void replaceString(StringBuffer b) {
  b = new StringBuffer("Replacement");
}
Before duplicateString: My
After duplicateString: My

Figure 339. No «call-by-reference» details Slide presentation

Figure 340. C++ reference operator & Slide presentation
int a = 1;

int &b = a;  

cout << a << " : " << b << endl;

a = 5;

cout << a << " : " << b << endl;
1 : 1
5 : 5

Figure 341. C++ offers «call-by-reference» by virtue of & Slide presentation
// Passing a reference
// to variable n
void printDuplicateValue(int& n) {
  n = 2 * n;
  cout << "duplicateValue: " << n 
       << endl;
}
int main() {
  int value = 3;
  cout << "Before call: "
    << value << endl;
  printDuplicateValue(value);
  cout << "After call: "
    << value << endl;
}
Before call: 3
duplicateValue: 6
After call: 6

Figure 342. C++ «call-by-reference» details Slide presentation