Value types and reference types

Figure 297. Value vs. reference type variables Slide presentation Create comment in forum
Value type: Holding data within own memory location
  • byte

  • short

  • int

  • long

  • float

  • double

  • char

  • boolean

Reference type: Holding a reference to an object

All class instances i.e. String, java.util.Scanner etc.


Figure 298. Different behaviour! Slide presentation Create comment in forum
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 299. Value variable Details Slide presentation Create comment in forum

Figure 300. Reference variable Details Slide presentation Create comment in forum

Figure 301. Only «call-by-value» in Java Slide presentation Create comment in forum
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 302. «call-by-value» details Slide presentation Create comment in forum

Figure 303. «call-by-reference» for objects? Slide presentation Create comment in forum
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 304. «call-by-reference» details Slide presentation Create comment in forum

Figure 305. No «call-by-reference» in Java! Slide presentation Create comment in forum
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 306. No «call-by-reference» details Slide presentation Create comment in forum

Figure 307. C++ offers true «call-by-reference» Slide presentation Create comment in forum
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 308. C++ «call-by-reference» details Slide presentation Create comment in forum