name: inverse layout: true class: center, middle, inverse --- # CSE 340 (Winter 2021) ## Java Refresher --- layout: false # What is Java? - Strongly, statically typed language - Every variable has a type - This type is decided at compile time (mostly) -- - Compiled, class-based, Object-oriented -- - Platform agnostic - __Write once__, _run anywhere_ without recompilation - Especially useful for Android --- ## Java Basics: Primitive Types -- .left-column50[ - Boolean ```java boolean hasClassStarted = true; boolean isClassOver = false; ``` ] --- ## Java Basics: Primitive Types .left-column50[ - Boolean ```java boolean hasClassStarted = true; boolean isClassOver = false; ``` - Integer ```java int numStudents = rand.nextInt(30); ``` ] --- ## Java Basics: Primitive Types .left-column50[ - Boolean ```java boolean hasClassStarted = true; boolean isClassOver = false; ``` - Integer ```java int numStudents = rand.nextInt(30); ``` - Float ```java float gradePointAverage = 3.2f; ``` ] -- .right-column50[ - Double - Higher precision than float ```java double examScore = 97.362; ``` ] -- .right-column50[ - Byte, Short, etc. ] --- ## Java Basics: Text -- - Characters ```java char section = 'B'; ``` -- - Strings ```java String instructor = "Jennifer Mankoff"; ``` -- All non-primitives types inherit from `Object` class - Including `String`; note the capitalization --- ## Java Basics: Visibility Modifiers ```java public final String COURSE = "CSE 340"; ... private final String SSN = "123-45-6789"; ``` -- .left-column50[ - `package private` - This is the default access if no modifier is specified - Accessible by all classes in the same package. ] --- ## Java Basics: Visibility Modifiers ```java public final String COURSE = "CSE 340"; ... private final String SSN = "123-45-6789"; ``` .left-column50[ - `package private` - This is the default access if no modifier is specified - Accessible by all classes in the same package. - `private` - Kept secret, can only be read/written by `self` - Cannot be accessed by subclasses ] -- .right-column50[ - `protected` - Access restricted to `self`, subclasses, and package ] --- ## Java Basics: Visibility Modifiers ```java public final String COURSE = "CSE 340"; ... private final String SSN = "123-45-6789"; ``` .left-column50[ - `package private` - This is the default access if no modifier is specified - Accessible by all classes in the same package. - `private` - Kept secret, can only be read/written by `self` - Cannot be accessed by subclasses ] .right-column50[ - `protected` - Access restricted to `self`, subclasses, and package - `public` - The world can read/write (fields) or call (methods) ] --- ## Java Basics: Visibility Modifiers -- - Generally, you want to be as restrictive as possible - Usually, this means `private` -- - Create getter/setter methods to modify the member variables -- - .red[__Almost never use__ `public`] for fields - Except for constants --- ## Java Basics: `final` -- .left-column50[ - Prevent value from changing after initialization ```java // local variable cannot be modified ever! final double courseGrade = 95.0; ``` ] --- ## Java Basics: `final` .left-column50[ - Prevent value from changing after initialization ```java // local variable cannot be modified ever! final double courseGrade = 95.0; ``` - Prevent subclassing ```java // can't subclass // (for example to make a Student class) public final class Person { ... } ``` ] -- .right-column50[ - Prevent overriding ```java // can't override! public final int getValue() { return 0; } ``` ] --- ## Java Basics: `static` -- - Use for constants or variables are shared by all instances of a particular class ```java final static double SALES_TAX_RATE = 0.07; // Class Constant (never changes) static double mTotalAmount = 3.56; // Class variable can change ``` -- - Methods that can be called without an class instance (instantiating an object) ```java static String toString(int i); // For example Integer.toString(100) => "100"; ``` --- # Naming Conventions - class names are PascalCased - local variables and method names are camelCased - class or instance variables begin with a 'm' (for member), such as mTotalAmount - constants are UPPER_SNAKE_CASED --- ## Java Basics: Methods - Methods in Java typically follow this format: ```java {visibility} [static] [final] returnType methodName(paramType paramName, ...) { ... } ``` -- - `static` and `final` are optional, special modifiers - `visibility` is one of `public`, `private`, `protected`, or empty for package private --- ## Java Basics: Method Example Summing two numbers and returning the answer as a string ```java public String getSumOfTwoNumbersAsString(int first, int second) { int sum = first + second; return Integer.toString(sum); // could also return "" + sum } ``` --- ## Java Basics: Declaring a class ```Java {visibility} class ClassName { // Field declarations // Method definitions } ``` --- ## Java Basics: Constructing a Class ```java public class Student { // Class (static) variables - public static final String STUDENT_KEY = "STUDENT"; private static final String ID_PREFIX = "S"; // Instance Variables private String mIdNumber; private String mName; // Constructors - used to create an instance Student(String name, String idNumber) { this.name = mName; this.idNumber = mIdNumber; } // Methods public String getPrefixedIdNumber() { return ID_PREFIX + mIdNumber; } ``` --- ## Java Basics: Constructing a Class cont. ```java // Getter public String getName() { return mName; } // Setter public void setName(String newName) { if (newName == null || newName == "") { newName = "Unknown"; } mName = newName; } ... // etc. } ``` --- # Enums An enum type is a special data type that enables for a variable to be a set of predefined constant ```java public enum EssentialGeometry { INSIDE, OUTSIDE }; ... EssentialGeometry where = EssentialGeometry.INSIDE; ``` --- # Switch Statements A form of a conditional with different execution paths ```java public enum EssentialGeometry { INSIDE, ON_EDGE, OUTSIDE }; ... EssentialGeometry where = EssentialGeometry.INSIDE; switch (where) { case ON_EDGE: // do the edgy things break; case INSIDE: // do the inside things but also fall through // and do the OUTSIDE things because no break statement; case OUTSIDE: // do the outside things break; default: // do default things // automatically falls through } ``` --- ## More Java Resources - Java Documentation (https://docs.oracle.com/en/java/javase/13/docs/api/) - __Online Java Practice Problems__: - http://codingbat.com/java - https://practiceit.cs.washington.edu/problem/list