1. Class abstraction and encapsulation

class abstraction
Figure 1. Class abstraction separates class implementation from the use of the class.

"Class abstraction is the separation of class implementation from the use of a class. The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation."[1];

  • class abstraction

  • class’s contract

  • class encapsulation

  • abstract data type

1.1. Example: BMI class

bmi uml
Figure 2. The BMI class encapsulates BMI information.

2. Class relationship association

Association is a general binary relationship that describes an activity between two classes. For example a student taking a course is an association between the Student class and the Course class.

3. Primitive data types and wrapper class types

  • Two constructors (from a primitive type or a String)

  • Constants (e.g. MAX_VALUE)

  • Conversion methods (e.g. doubleValue returning a double from a Integer or Double)

  • compareTo method

  • static valueOf and parsing methods

  • objects of wrapper classes are immutable

Figure 3. The wrapper classes provide constructors, constants, and conversion methods data types

3.1. Automatic conversion between primitive types and wrapper class types

Integer[] intArray = {1, 2, 3}; (1)
System.out.println(intArray[0] + intArray[1] + intArray[2]); (2)
1 The primitive types 1,2 and 3 are automatically boxed into objects new Integer(1), new Integer(2) and new Integer(3) by the compiler.
2 The objects intArray[0], intArray[1] and intArray[2] are automatically unboxed into int values that are summed up.

4. The String class

4.1. Constructing a String

String literal are treated as a String object
String message = "Welcome to Java";
Create string from an array of characters
char[] charArray = {'G', 'o', 'o', 'd', ' ', 'D', 'a', 'y'};
String message = new String(charArray);

4.2. String objects are immutable

A String object is immutable: Its content cannot be changed once the string is created. However, since String variables are - like any Object variable - reference variables it is possible to let a String variable refer to another String object (see the figure below).

immutable strings
Figure 4. Strings are immutable; once created, their contents cannot be changed

Because strings are immutable and are ubiquitous in programming, the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string.

interned strings
Figure 5. s1 == s2 is false, while s1 == s3 is true

1. Liang, Y Daniel. Intro to Java Programming, Comprehensive Version, Global Edition, 10th Edition. Pearson (Intl), 11/2014. VitalBook file.