Java
Lec 01: Intro
Java Type Checking
- Java is strongly typed
- Java has static type checking: detects many problems at compile time
Python is dynamically typed, interpreted
Lec 02: Primitive Types
- each .java file defines a class
- the file name and the public class name must match
public static void main(String[] args) {
}
Compile
- translates .java files into .class files
- the .class file is hardwire independent
Primitive Data Types
- you can store only certain values: numbers, booleans, characters
Integer Primitive Data Types
- int: 32 bits / 4 bytes: + / - 2 billion
- short: 16 bits:
- long: 64 bits / 8 bytes
Floating Point Primitive Data Types
- float: 7 decimal digits (single precision)
- double: 15 decimal digits (double precision)
Truth Value Primitive Data Types
- cannot convert between integers and boolean values in Java
Character Primitive Data Types
- char: 2 bytes
Final Variables
- can assign to the variable
O
n
c
e
{\red{Once}}
Once
final int maxValue = 2147483647;
Naming Convention
- class start with a capital letter
- variables and methods start with lower case
- CamelCase
public class MyCoolClass {
int complexMathFunc(int x)
{
int localVar;
}
}
- constants are all capital letters: underscore is used to separate words
- can use lower case letters in final variable
Numeric Gotcha
- S l i e n t {\blue{Slient}} Slient type conversion
- S l i e n t {\blue{Slient}} Slient integer overflow
- S l i e n t {\blue{Slient}} Slient floating-point rounding
Input Comes as Strings
- you can use
args
to get command line input:
public static void main(String[] args) {...}
Comments
- one line comments start with //
- multi-line comment is delinated by /…/
- JavaDoc comments delineated by /…*/**
/**
* HW2 Solution to compute check digit.
* @author Layla Li
* /
Lec 03: Java Classes
Classes
- a class is a template definition ( b l u e p r i n t {\blue{blueprint}} blueprint)
- it defines the d a t a {\blue{data}} data contained in objects describing object’s state
- if defines the m e t h o d s ( o p e r a t i o n s ) {\blue{methods(operations)}} methods(operations) that can be performed on those data
public class PlayerInfo{
String firstName;
String lastName;
String age;
String position;
PlayerInfo spouse;
public PlayerInfo() {
}
public static void main(String[] args) {
PlayerInfo kyrie = new PlayerInfo();
kyrie.firstName = "Kyrie";
kyrie.lastName = "Irving";
kyrie.age = "30";
kyrie.position = "point guard";
}
}
- you can reference instances of your own type
Instances
- the new operator creates instances
Constructors
- to instantiate a new instance (object), invoke a constructor of the class using the new operator
- must have the same name as the class itself
- the default constructor
- if you write your own constructor(s) in a class, Java DOES NOT generate the no-argument constructor
- advantanges
- you control the initialization of the object
- if you provide constructors, users must call it
Accessing via Reference Variables
- newly created objects/instances are allocated in the memory
- instance variables (firstName of PlayerInfo is a field) are “fields” in an object
- instance variables are fields that are unique to each instance of a class
Static vs. Non-Static
Inheritance
- a class(subclass) can extend a class(superclass)
- subclass inherits accessible fields and methods from Superclass
- subclass may add more fields
- subclass may add more methods
- subclass may override some methods of superclass
- subclass provide different constructors
public class NetsInfo extends PlayerInfo {
// new field
string number;
// new instance method
void birthday() {
age = age + 1;
}
// override superclass method
@override
void print()
{
super.print();
System.out.println("number: " + number);
}
}
Must Call the Super Class Constructor
- the first line of a subclass constructor must be a call to the superclass constructor, otherwise you’d get an error if it has args if not there
- if the superclass has a no-arg constructor, Java will automatically generate the call of super()
- constructor chaining
Super keyword
- can be used to call super class method
super.print();
Lec 04: Reference Types
Access Modifiers
- 4 access modifiers
- E n c a p s u l a t i o n {\blue{Encapsulation}} Encapsulation : declare ** instance variables to be private**
- when in doubt, use private!
- Protect your data
Override vs Overload
- overloading: same method name but different parameters in the same class
- overriding: same method signature in both super and child class
toString() method
- can call toString() on any object
Polymorphism
- a superclass reference variable can reference instances of a subclass
Shape s1 = c;
Shape s2 = r;
Object o1 = new Square(3);
Dynamic Binding
- binding: the process of mathcing a method call with the correct method definition
- dynamic binding means binding happens at runtime:
- overriden methods
- it is the instance’s type that determines which method is called,not the variable’s type
- goes from the bottom of the class hierarchy of the instance’s type
Casting
- no explicit cast when going up the class hierarchy
Square s = new Square();
Object o = s;
- explicit cast is required going down the class hierarchy
Shape shape = shapeMaker();
// if shape is really rectangle, it works
// if not, ClassCastException at runtime
Rectangle r = (Rectangle) shape;
// better to test first if not sure
if (shape instance of Rectangle) {
Rectangle r = (Rectangle) shape;
}
Garbage Collection
- Java keeps “track” of all references to instances
- instances with “no references” are “freed”
Comments
- class comment must have an @author tags
- method comment must have @param and @return tags
Difference b/w Primitive type and a Reference type
- primitive types use fixed storage
- vairables of primitive types contain their values
- reference types are instances that use varied amounts of storage
- amounts that are different for each instance
- variables of refernce type reference (point to) instances of their type