● Object – characterized by state, attributes, and behavior.
o Instance of a class
● All OOP (Object-Oriented Programming) languages try to represent an object as a variable or an instance in a program.
● Class – Software blueprint
o Implement Objects of a specific data type
● Data fields –
o Also known as instance variables
o Methods
▪ Provide behaviors of the object
▪ Provide operations which manipulate the object
● Encapsulation – Combining data and method into a single unit
o Class declaration which suggests that the class can be used by all client programs
o Public methods – any client program can access these methods
o Information hiding in Java – restricting access to data and methods
o Private methods and variables – Only accessible by methods from that class
o Java does allow public instance variables but we only have private instance variables in the AP Java Subset.
o Static variable – class variable
▪ Shared through all instances of a class
▪ Static – the memory allocation can only occur once
o Uses of a static variable
▪ Accumulation of a total
▪ Providing a new identity for each instance or object in a class
▪ Keeping track of data of the objects in a class
o Static final variable (also known as constant)
▪ Cannot be changed
▪ Often declared as public
▪ Keyword static indicates that the single value applies to the entire class and not just a new instance for an object in the class.
● Method headers look like this:
o Except constructors and static methods
o public void deposit (String password, double amount)
o public – access specifier
o void – return type
o withdraw – method name
o (Strong password, double amount) - list of parameters
o Instance methods
▪ Constructors, accessors, and mutators
o Constructors
▪ Creates an object or a new instance of the class
▪ Default constructor – No arguments
● Provides initial values for each new object
▪ Constructor with parameters have instance variables set to the values of the parameters.
▪ Object variables store the values and addresses of their respective objects.
o Accessors
▪ Access a class object without modifying an object
▪ Returns information about the object
▪ . (dot) operator signals a method of a class
o Mutators
▪ Alters the state of an object
▪ Changes at least one of the instance variables of the object
▪ Invoked similar to an accessor in a client program
o Static Methods
▪ Performs an operation for the whole class
▪ Not individual objects
▪ Sometimes known as a class method
o Static methods vs. Instance methods
▪ Static methods use the keyword static for its implementation.
▪ No implied objects in static method (there is in instance methods)
▪ A static method can use a static variable in the code.
o Overloaded methods are two or more methods which are in the same class and have the same name but different inputs or parameter lists
o Compiler decides which method to call by looking at the method’s signature
o Method signature – consists of method’s name, parameter types
o Return type is irrelevant for overloaded methods
▪ Two methods can have similar method signatures but not different return types (compiler will give you an error)
o Scope of a variable or method means that it’s in an area where the identifier is visible and can be easily accessed
o What does a scope consist of?
▪ Instance variables
▪ Static variables
▪ Methods of a class
o From the opening brace to the closing brace of a class.
o Inside a method
o Can be defined in a statement
o Scope extends from the beginning to the end of the block it is defined in
o Block – code enclosed in {}
o The memory of a local variable or method is recycled when a block is exited
o Higher precedence over instance variables
o Using same names creates ambiguity for programmers and coders (Leads to more errors)
● String – an object of this type is just a sequence of characters.
● All string literals are implemented as instances of a class.
o String literals have zero or more characters in them.
o Can also include escape sequences
o String literals are surrounded by double quotes.
▪ Quotes are not part of the string object.
● String Objects – immutable
o No methods can change them after they are constructed
● You can create a new String object which can be a modified form of an existing String object.
● String objects can be initialized like a primitive data type.
● Concatenation operator (+)
o Used on String objects
o Allows you to combine two or more String operands
● Characters in the String objects
o Compared based on their position in the ASCII chart.
● Don’t use (==) for testing or comparing String Objects.
● String objects can be initialized like a primitive data type.
o Using the equals method
▪ If (string1.equals(string2))…
o Using the compareTo method
▪ Int compareTo (string otherStringYouAreComparing)
▪ stringOne. compareTo (stringTwo) < 0
● stringOne precedes stringTwo in a dictionary
▪ stringOne. compareTo (stringTwo) > 0
● stringTwo precedes stringOne in a dictionary
▪ stringOne. compareTo (stringTwo) == 0
● both Strings are identical
o Remember that Java is case-sensitive
o This wraps a value of int type in any object
o Only contains one instance variable with the type int
o Int methods you should know for the exam
▪ Integer (int value)
● To construct an integer
▪ int compareTo (Integer other)
▪ int intValue()
▪ boolean equals (Object obj)
● Overrides equals in class Object.
o This wraps a value of double type in any object
o double (double value)
o double doubleValue()
o int compareTo (Double other)
o boolean equals (Object obj)
o String toString()
● Both Integer and Double objects contain no mutator methods in their classes.
● Methods you need to know in the Math Class
o static int abs(int x)
o static double abs(double x)
o static double pow(double base, double exp)
o static double sqrt(double x)
o static double random()