Table of Contents
Vocab
-
User Input
-
Useful Java Tools
Code Examples
- Starter Program Example
- Variables and Updating Values Example
- Strings and Concatenation Example
- User Input Example
- Comparison Operators Example
- If Statement Example
- If / Else Example
- If / Else If / Else Example
- Compound Booleans Example
- De Morgan's Law Example
- For Loop Example
- While Loop Example
- Arithmetic and Modulus Example
- Method with Parameters Example
- Method that Returns a Value Example
- Scope Example
- Method Overloading Example
- Class with Instance Fields Example
- Constructor and Object Creation Example
- Dot Notation and Non-Static Method Example
Vocab
Basics
algorithm
Definition: A step-by-step process used to complete a task or solve a problem.
Java example:
int total = price1 + price2;
System.out.println(total);
sequencing
Definition: The order in which instructions are completed. Changing the order can change the result.
variable
Definition: A named storage location that holds a value that can change.
Java example:
int score = 10;
score = 15;
variable declaration
Definition: Creating a variable by giving Java its data type and name.
Structure / syntax:
dataType variableName;
Java example:
String weather;
variable initialization
Definition: Giving a variable its first value when it is created.
Structure / syntax:
dataType variableName = value;
Java example:
String weather = "sunny";
assignment
Definition: Using = to store or replace a value in a variable.
Java example:
weather = "rainy";
data types
Definition: Categories that tell Java what kind of value a variable can store.
Java example:
String name = "Alex";
int age = 16;
double height = 5.8;
boolean hasPermit = true;
String
Definition: A Java type used to store text.
Java example:
String playerName = "Alex";
int
Definition: A Java type used to store whole numbers.
Java example:
int lives = 3;
double
Definition: A Java type used to store numbers that may contain decimals.
Java example:
double price = 12.99;
boolean
Definition: A Java type that stores only true or false.
Java example:
boolean isLoggedIn = false;
console / output
Definition: The console displays information produced by a program. Information sent out by a program is output.
Java example:
System.out.println("Hello World!");
concatenation
Definition: Combining Strings together with the + operator.
Java example:
String fullName = firstName + " " + lastName;
type conversion / casting
Definition: Changing a value from one data type to another.
Java example:
int age = Integer.parseInt("16");
double price = 19.99;
int wholePrice = (int) price;
Casting a double to an int removes the decimal portion; it does not round.
Methods
method
Definition: A reusable block of code that performs a task.
Java example:
static void sayHi() {
System.out.println("Hi!");
}
method declaration
Definition: The code that defines a method's name, return type, parameters, and body.
Java example:
static void greet(String name) {
System.out.println("Hello, " + name);
}
method call
Definition: An instruction that tells Java to run a method.
Java example:
greet("Jordan");
parameter
Definition: A variable listed in a method declaration that receives information.
Java example:
static void greet(String name) {
System.out.println("Hello, " + name);
}
argument
Definition: A value supplied to a method when it is called.
Java example:
greet("Jordan");
void
Definition: A method return type that means the method does not send a value back.
Java example:
static void printMessage() {
System.out.println("Hello");
}
return type
Definition: The data type written before a method name that tells Java what type of value the method returns.
Java example:
static int getScore() {
return 100;
}
return statement
Definition: Sends a value back from a method and ends that method.
Java example:
static int add(int a, int b) {
return a + b;
}
scope / local variable
Definition: Scope is where a name can be used. A local variable exists only inside the method or block where it is declared.
Java example:
static void example() {
int score = 10;
System.out.println(score);
}
// score cannot be used here
method overloading
Definition: Creating multiple methods with the same name but different parameter lists.
Java example:
static void show(int number) { }
static void show(String text) { }
User Input
Scanner
Definition: A Java class commonly used to read keyboard input.
Java example:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
user input
Definition: Information entered into a program by the user while the program is running.
Java example:
System.out.print("Enter your age: ");
int age = input.nextInt();
Scanner input methods
Definition: Methods used to read different kinds of input with a Scanner.
Java example:
String name = input.nextLine();
int age = input.nextInt();
double price = input.nextDouble();
nextLine() reads text, nextInt() reads an integer, and nextDouble() reads a decimal number.
Operators
arithmetic operators
Definition: Operators used for math: +, -, *, and /.
Java example:
int total = 5 + 2;
int product = 5 * 2;
double quotient = 5.0 / 2.0;
modulus (%)
Definition: Returns the remainder after division.
Java example:
System.out.println(10 % 3); // 1
increment / decrement
Definition: ++ increases by 1. -- decreases by 1.
Java example:
score++;
lives--;
compound assignment
Definition: Combines an operation with assignment, such as +=, -=, *=, and /=.
Java example:
score += 5;
health -= 10;
comparison operators
Definition: Compare two values and produce true or false: ==, !=, <, >, <=, >=.
Java example:
System.out.println(score >= 70);
System.out.println(lives != 0);
Control Structures
conditional statement
Definition: A statement that makes a decision based on a boolean condition.
Java example:
if (temperature < 50) {
System.out.println("Wear a jacket.");
}
if statement
Definition: Runs a block of code only when its condition is true.
Java example:
if (score >= 90) {
System.out.println("A");
}
if / else
Definition: Chooses between two paths.
Java example:
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
if / else if / else
Definition: Checks multiple conditions in order and runs the first matching branch.
Java example:
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("Below B");
}
nested if statement
Definition: An if statement placed inside another conditional statement.
Java example:
if (age >= 16) {
if (hasPermit) {
System.out.println("Can practice driving");
}
}
logical operators
Definition: Operators used with booleans: && means AND, || means OR, and ! means NOT.
Java example:
boolean canDrive = age >= 16 && hasPermit;
boolean canEnter = hasID || hasPass;
boolean unlocked = !isLocked;
compound boolean expression
Definition: A boolean expression made by combining two or more conditions.
Java example:
boolean eligible = age >= 16 && hasPermit;
De Morgan's Law
Definition: Rules for rewriting negated compound boolean expressions.
Java example:
!(A && B) // same as !A || !B
!(A || B) // same as !A && !B
for loop
Definition: A loop commonly used when initialization, condition, and update can be written together.
Structure / syntax:
for (initialization; condition; update) {
// repeated code
}
Java example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
while loop
Definition: A loop that repeats while its condition remains true.
Java example:
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
}
infinite loop
Definition: A loop that never stops because its condition never becomes false.
Java example:
int count = 0;
while (count < 5) {
System.out.println(count);
// count never changes
}
Classes and Objects
class
Definition: A blueprint that describes the data and behaviors objects of that type can have.
Java example:
public class Friend {
}
object
Definition: A specific object created from a class.
Java example:
Friend jasmine = new Friend("Jasmine", 15);
instance
Definition: Another name for an object created from a class.
Java example:
Friend jasmine = new Friend("Jasmine", 15);
instance field
Definition: A variable declared in a class that stores information for each object.
Java example:
public class Friend {
String name;
int age;
}
constructor
Definition: A special part of a class that runs when a new object is created and usually gives it starting values.
Structure / syntax:
public ClassName(parameters) {
// setup code
}
Java example:
public Friend(String friendName, int friendAge) {
name = friendName;
age = friendAge;
}
constructor parameter
Definition: A parameter in a constructor that receives a value when a new object is created.
Java example:
public Friend(String friendName, int friendAge) {
name = friendName;
age = friendAge;
}
new keyword
Definition: The keyword used to create a new object from a class.
Java example:
Friend jasmine = new Friend("Jasmine", 15);
dot notation
Definition: Using a dot after an object name to access one of its fields or methods.
Java example:
System.out.println(jasmine.name);
jasmine.greeting();
static vs. non-static methods
Definition: A static method belongs to the class. A non-static method belongs to an object and can directly use that object's instance fields.
Java example:
static void classMessage() {
System.out.println("Class method");
}
void greeting() {
System.out.println("Hello, " + name);
}
abstraction
Definition: Using a simpler name or interface without handling all internal details every time.
Java example:
Friend jasmine = new Friend("Jasmine", 15);
System.out.println(jasmine.name);
Useful Java Tools
Math class
Definition: A built-in Java class containing useful static math methods and constants.
Java example:
double root = Math.sqrt(25);
double power = Math.pow(2, 3);
double absolute = Math.abs(-7);
Math.random()
Definition: Returns a random double from 0.0 up to, but not including, 1.0.
Java example:
int randomNumber = (int) (Math.random() * 10) + 1;
This example creates a random integer from 1 through 10.
Code Examples
Starter Program Example
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Variables and Updating Values Example
int score = 10;
score = 15;
System.out.println(score);
Strings and Concatenation Example
String firstName = "Dev";
String lastName = "Cat";
System.out.println(firstName + " " + lastName);
User Input Example
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
Comparison Operators Example
int score = 85;
System.out.println(score == 85);
System.out.println(score != 100);
System.out.println(score >= 70);
If Statement Example
if (score >= 90) {
System.out.println("A");
}
If / Else Example
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
If / Else If / Else Example
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("Below B");
}
Compound Booleans Example
boolean canDrive = age >= 16 && hasPermit;
boolean canEnter = hasID || hasPass;
boolean lockedOut = !hasAccess;
De Morgan’s Law Example
!(A && B) // same as !A || !B
!(A || B) // same as !A && !B
For Loop Example
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
While Loop Example
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
}
Arithmetic and Modulus Example
int total = 7 + 3;
int product = 7 * 3;
int remainder = 10 % 3;
Method with Parameters Example
static void greet(String name) {
System.out.println("Hello, " + name);
}
greet("Jordan");
Method that Returns a Value Example
static int add(int a, int b) {
return a + b;
}
int total = add(4, 6);
Scope Example
static void example() {
int score = 10;
}
// score cannot be used here
Method Overloading Example
static void show(int number) { }
static void show(String text) { }
Class with Instance Fields Example
public class Friend {
String name;
int age;
}
Constructor and Object Creation Example
public class Friend {
String name;
int age;
public Friend(String friendName, int friendAge) {
name = friendName;
age = friendAge;
}
public static void main(String[] args) {
Friend jasmine = new Friend("Jasmine", 15);
}
}
Dot Notation and Non-Static Method Example
public void greeting() {
System.out.println("Hello, " + name);
}
Friend jasmine = new Friend("Jasmine", 15);
System.out.println(jasmine.name);
jasmine.greeting();
Notebook Style Guide
Keep your notebook useful for you. Add examples, corrections, and notes as you learn.
- Use
##for major sections. - Use
###for categories and examples. - Use bold for important ideas.
- Use
inline codefor short Java syntax. - Use fenced Java code blocks for multi-line examples:
```java
System.out.println("Hello World!");
```
- Add comments when they explain why something is happening.
- Keep examples small enough that you can quickly understand them later.
- When you make a mistake, add the corrected example if it will help you avoid the same mistake again.
Bottom Line
Definitions tell you what something means. Code examples show you how to use it. Your notebook should have both. `````