Intro to Objects - Computer Math
Java Compilation & Basics
Java applications require a
public static void main(String[] args)method to be runnable.Source code (
.java) is translated into bytecode (.class) by thejavaccompiler, making the code platform-independent so it can run on UNIX, Windows, or Mac.The compiler ignores comments starting with
//, while theimportkeyword is used to make external classes and packages accessible.
Classes, Objects, and Inheritance
The
newkeyword is used to instantiate a new object, which makes the object an instance of a specific class.A base class that defines what objects can do is called a superclass (e.g.,
Robot), while a class that adds more functionality is a subclass (e.g.,Athlete).The greatest advantage of creating a subclass like
Athleteis inheritance, which is implemented using theextendskeyword.
Default Properties & Instantiation
When created with the default constructor, a
Robotstarts at position (1, 1) facingDisplay.EASTwith 0 beepers.When created with the default constructor, an
Athletestarts at position (1, 1) facingDisplay.NORTHwithDisplay.INFINITYbeepers.You can instantiate objects with custom starting data, such as
Robot john = new Robot(4, 1, Display.NORTH, 5);.
Syntax and Identifiers
The Java class name must perfectly match the filename, and blocks of code are grouped using matching curly braces.
Methods are always marked by parentheses, as seen in commands like
move();orputBeeper();.Identifiers (used for naming classes, objects, or methods) are case-sensitive, cannot be keywords, and must only contain letters, numbers, and underscores.