View on GitHub

Notes

reference notes

Java Basics Cheat Sheet

Class Declaration and Object Creation

import javax.swing.*;

class Ch2Sample1 {
    public static void main(String[] args) {
        JFrame myWindow;
        myWindow = new JFrame();
        myWindow.setSize(300, 200);
        myWindow.setTitle("My First Java Program");
        myWindow.setVisible(true);
    }
}

Object Declaration

JFrame myWindow;
Account customer;
Student jan, jim, jon;
Vehicle car1, car2;

Object Creation

myWindow = new JFrame();
customer = new Customer();
jon = new Student("John Java");
car1 = new Vehicle();

Identifiers

Java Standard Naming Convention

Robot robot;

Declaration vs. Creation

Customer customer;
customer = new Customer();
  1. The identifier customer is declared, and space is allocated in memory.
  2. A Customer object is created, and the identifier customer is set to refer to it.

Sending a Message

myWindow.setVisible(true);
account.deposit(200.0);
student.setName("john");
car1.startEngine();

Standard Input

import java.util.Scanner;

Scanner scanner;
scanner = new Scanner(System.in);
String input;
System.out.print("Enter your input(s): ");
input = scanner.nextLine();
System.out.println("I entered: " + input);

Standard Output

System.out.print("I Love Java");
System.out.println("How do you do?");
System.out.println("My name is Jon Java.");

String

String text = "Espresso";
text.substring(6, 8);
text.length();
text.indexOf("gram");

Date

import java.util.Date;
Date today;
today = new Date();

SimpleDateFormat

import java.text.SimpleDateFormat;

SimpleDateFormat sdf1, sdf2;
sdf1 = new SimpleDateFormat("MM/dd/yy");
sdf2 = new SimpleDateFormat("MMMM dd, yyyy");
sdf1.format(today);
sdf2.format(today);