next up previous
Next: Variables: main properties Up: Unit 02 Previous: Static methods

Variables

Example:

public class Java1 {
  public static void main(String[] args) {
    System.out.println("java".toUpperCase());
    System.out.println("java".toUpperCase());
  }
}

Note that the expression "java".toUpperCase() is evaluated twice. To avoid this, we could store the result of the evaluation of this expression in a variable and reuse it for printing.

public class Java2 {
  public static void main(String[] args) {
    String line;
    line = "java".toUpperCase();
    System.out.println(line);
    System.out.println(line);
  }
}

In the Java2 program, line is a variable of type String to which we assign the value of "java".toUpperCase(), which is then printed twice.

A variable represents a memory location that can be used to store the reference to an object.


next up previous
Next: Variables: main properties Up: Unit 02 Previous: Static methods