+ -
Notes for current slide
Notes for next slide

CSE 340 (Winter 2022)

Java Refresher

Slide 1 of 40

What is Java?

  • Strongly, statically typed language

    • Every variable has a type
    • This type is decided at compile time (mostly)
Slide 2 of 40

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
Slide 3 of 40

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
Slide 4 of 40

Java Basics: Primitive Types

Slide 5 of 40

Java Basics: Primitive Types

  • Boolean
boolean hasClassStarted = true;
boolean isClassOver = false;
Slide 6 of 40

Java Basics: Primitive Types

  • Boolean
boolean hasClassStarted = true;
boolean isClassOver = false;
  • Integer
int numStudents = rand.nextInt(30);
Slide 7 of 40

Java Basics: Primitive Types

  • Boolean
boolean hasClassStarted = true;
boolean isClassOver = false;
  • Integer
int numStudents = rand.nextInt(30);
  • Float
float gradePointAverage = 3.2f;
Slide 8 of 40

Java Basics: Primitive Types

  • Boolean
boolean hasClassStarted = true;
boolean isClassOver = false;
  • Integer
int numStudents = rand.nextInt(30);
  • Float
float gradePointAverage = 3.2f;
  • Double
    • Higher precision than float
double examScore = 97.362;
Slide 9 of 40

Java Basics: Primitive Types

  • Boolean
boolean hasClassStarted = true;
boolean isClassOver = false;
  • Integer
int numStudents = rand.nextInt(30);
  • Float
float gradePointAverage = 3.2f;
  • Double
    • Higher precision than float
double examScore = 97.362;
  • Byte, Short, etc.
Slide 10 of 40

Java Basics: Text

Slide 11 of 40

Java Basics: Text

  • Characters
char section = 'B';
Slide 12 of 40

Java Basics: Text

  • Characters
char section = 'B';
  • Strings
String instructor = "Jennifer Mankoff";
Slide 13 of 40

Java Basics: Text

  • Characters
char section = 'B';
  • Strings
String instructor = "Jennifer Mankoff";

All non-primitives types inherit from Object class

  • Including String; note the capitalization
Slide 14 of 40

Java Basics: Visibility Modifiers

public final String COURSE = "CSE 340";
...
private final String SSN = "123-45-6789";
Slide 15 of 40

Java Basics: Visibility Modifiers

public final String COURSE = "CSE 340";
...
private final String SSN = "123-45-6789";
  • package private
    • This is the default access if no modifier is specified
    • Accessible by all classes in the same package.
Slide 16 of 40

Java Basics: Visibility Modifiers

public final String COURSE = "CSE 340";
...
private final String SSN = "123-45-6789";
  • 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
Slide 17 of 40

Java Basics: Visibility Modifiers

public final String COURSE = "CSE 340";
...
private final String SSN = "123-45-6789";
  • 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
  • protected
    • Access restricted to self, subclasses, and package
Slide 18 of 40

Java Basics: Visibility Modifiers

public final String COURSE = "CSE 340";
...
private final String SSN = "123-45-6789";
  • 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
  • protected
    • Access restricted to self, subclasses, and package
  • public
    • The world can read/write (fields) or call (methods)
Slide 19 of 40

Java Basics: Visibility Modifiers

Slide 20 of 40

Java Basics: Visibility Modifiers

  • Generally, you want to be as restrictive as possible
    • Usually, this means private
Slide 21 of 40

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
Slide 22 of 40

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
  • Almost never use public for fields
    • Except for constants
Slide 23 of 40

Java Basics: final

Slide 24 of 40

Java Basics: final

  • Prevent value from changing after initialization
// local variable cannot be modified ever!
final double courseGrade = 95.0;
Slide 25 of 40

Java Basics: final

  • Prevent value from changing after initialization
// local variable cannot be modified ever!
final double courseGrade = 95.0;
  • Prevent subclassing
// can't subclass
// (for example to make a Student class)
public final class Person {
...
}
Slide 26 of 40

Java Basics: final

  • Prevent value from changing after initialization
// local variable cannot be modified ever!
final double courseGrade = 95.0;
  • Prevent subclassing
// can't subclass
// (for example to make a Student class)
public final class Person {
...
}
  • Prevent overriding
// can't override!
public final int getValue() {
return 0;
}
Slide 27 of 40

Java Basics: static

Slide 28 of 40

Java Basics: static

  • Use for constants or variables are shared by all instances of a particular class
final static double SALES_TAX_RATE = 0.07; // Class Constant (never changes)
static double mTotalAmount = 3.56; // Class variable can change
Slide 29 of 40

Java Basics: static

  • Use for constants or variables are shared by all instances of a particular class
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)
static String toString(int i);
// For example Integer.toString(100) => "100";
Slide 30 of 40

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
Slide 31 of 40

Java Basics: Methods

  • Methods in Java typically follow this format:
{visibility} [static] [final] returnType methodName(paramType paramName, ...) {
...
}
Slide 32 of 40

Java Basics: Methods

  • Methods in Java typically follow this format:
{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
Slide 33 of 40

Java Basics: Method Example

Summing two numbers and returning the answer as a string

public String getSumOfTwoNumbersAsString(int first, int second) {
int sum = first + second;
return Integer.toString(sum); // could also return "" + sum
}
Slide 34 of 40

Java Basics: Declaring a class

{visibility} class ClassName {
// Field declarations
// Method definitions
}
Slide 35 of 40

Java Basics: Constructing a Class

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;
}
Slide 36 of 40

Java Basics: Constructing a Class cont.

// Getter
public String getName() {
return mName;
}
// Setter
public void setName(String newName) {
if (newName == null || newName == "") {
newName = "Unknown";
}
mName = newName;
}
... // etc.
}
Slide 37 of 40

Enums

An enum type is a special data type that enables for a variable to be a set of predefined constant

public enum EssentialGeometry { INSIDE, OUTSIDE };
...
EssentialGeometry where = EssentialGeometry.INSIDE;
Slide 38 of 40

Switch Statements

A form of a conditional with different execution paths

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
}
Slide 39 of 40

More Java Resources

Slide 40 of 40

What is Java?

  • Strongly, statically typed language

    • Every variable has a type
    • This type is decided at compile time (mostly)
Slide 2 of 40
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
s Start & Stop the presentation timer
t Reset the presentation timer
?, h Toggle this help
Esc Back to slideshow