Built in classes
- The JDK
- Classes in Java are grouped into packages. The classes
String
, Math
, System
, Integer
, Double
, Boolean
, and Object
are all from the java.lang
package, whose classes are always available in all Java programs.
- The only class we use from another package is
ArrayList
which needs to be imported from java.util
.
- Application program interfaces (APIs) and libraries provide classes we can use in our programs.
- A class’s documentation describes the attributes, constructors and methods defined in the class.
The System
class
System.out.print
and System.out.println
display information on the computer monitor.
System.out.println
moves the cursor to a new line after the information has been displayed, while System.out.print
does not.
The Math
class
- The
Math
class is part of Java and is available in all Java programs.
- The
Math
class contains only static methods.
- You should know how to use the methods
Math.abs
, Math.pow
, Math.sqrt
, and Math.random
. (You should also know about the variable—not a method—Math.PI
whose value is the double
value that best approximates the value of 𝜋.)
- The method
int abs(int x)
returns the absolute value of an int
value.\note{Except for when x
is Integer.MIN_VALUE
in which case it just returns the same value since the absolute value of MIN_VALUE
cannot be represented as an int
.}
- The method
double abs(double x)
returns the absolute value of a double
value
- The method
double pow(double base, double exponent)
returns the value of the first parameter raised to the power of the second parameter.
- The method
double sqrt(double x)
returns the positive square root of a double
value.
- The method
double random()
returns a (pseudo)randomly generated double
value greater than or equal to 0.0 and less than 1.0.
- Values returned from
Math.random
are in the range 0.0 to 1.0 but can be manipulated to produce a random int or double in any desired range by scaling (multiplying or dividing) and shifting (adding or subtracting).
Access modifiers
- The keywords
public
and private
control what code can access class members.
- The keyword
private
restricts access to only code within the declaring class, while the keyword public
allows access from any code.
- Classes cannot be marked
private
and are typically marked public
.\note{There are other access levels than public and private but they are not covered in the AP curriculum. Occasionally you will see a class with neither a public nor private modifier. One rule of Java is that there can be only one public class per .java file so if you have more than one top-level class in a file all but one of them will have to leave off the public
modifier.}
- Access to variables should usually be kept internal to the class. Therefore, instance variables are usually made
private
.
- Constructors are typically made
public
but sometimes classes will have private
constructors that are only used within the class, either from other constructors or from methods that construct new instances of the class.
- Methods are equally likely to be
public
or private
. The public
methods define what we can do with instances of a class but private
methods are useful for breaking up big methods into sub-methods that are only useful within a class.
- Code within a class can access the
private
variables and methods of any instance of the class.
Encapsulation
- Encapsulation is a technique in which implementation details of a class are kept hidden from code outside the class.
- When designing a class, programmers make decisions about what parts of its state to make accessible and modifiable from an external class. Data can be either accessible, modifiable, both, or neither.
- State can be encapsulated by storing it in
private
instance variables and access controlled by what getter, setter, and other mutator methods are provided.
this
- Within a non-
static
method or a constructor, the keyword this
is a name for the reference to the current object—the object on which the method was called or the object being constructed.
- The keyword
this
can be used to pass a reference to the current object as an argument to a method or constructor or as a value to be assigned anywhere a value of the type of the current class is needed.
Comments
- Comments are ignored by the compiler and are not executed when the program is run.
- Three types of comments in Java include
/* */
, which generates a block of comments, //
, which generates a comment on one line, and /** */
, which are Javadoc comments and are used to create API documentation.
Invariants
- Invariants are things that must be true if the program is working correctly. They help us think about how different parts of a program work together.
- A precondition is a kind of invariant that defines a condition that must be true before a method is called.
- A postcondition is kind of invariant that defines a condition that must always be true after a method returns successfully. Postconditions describe the outcome of the execution in terms of the value returned and/or any changes to the state of the world.
- The job of a method is to satisfy its postconditions assuming its preconditions have been met. If the preconditions are not met, it is a bug in the code that called the method. If the preconditions were met and the postconditions are not satisfied it is a bug in the method. Methods may or may not actually check that their preconditions have been met.
Overloading
- Constructors are said to be overloaded when there are multiple constructors in the same class with different numbers or types of parameters.
- Methods are said to be overloaded when there are multiple methods in the same class (or inheritance hierarchy) with the same name but different numbers or types of parameters.
- When an overloaded constructor or method is called, which one is invoked is determined by finding the constructor or method whose parameter types match the types of the arguments in the call.
- Overloading of methods doesn’t allow you to do anything you couldn’t do by just giving the different methods different names.