Tutorial_ Java Documentation - Basics _ CodeHS
Tutorial_ Java Documentation - Basics _ CodeHS
Java Tutorial
By Zach Galant
Co-Founder of CodeHS
// Prints a line but doesn't end the line. the next call to print or println will continue o
System.out.print(str);
Variables
You can create an integer variable with the keyword int like so
https://codehs.com/tutorial/zach/java-documentation-basics 1/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS
Methods
Methods can take in values, called parameters.
The method below takes in a parameter called input and prints it.
The method below takes in a value, adds two to it, and returns it.
User Input
You can collect user input in programs with a Scanner
Initializing a Scanner
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
https://codehs.com/tutorial/zach/java-documentation-basics 2/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS
Read a string
Read an integer
Read a double
Read a boolean
Comparison Operators
Comparison operators return booleans (true/false values)
x == y // is x equal to y
x != y // is x not equal to y
x > y // is x greater than y
x >= y // is x greater than or equal to y
x < y // is x less than y
x <= y // is x less than or equal to y
if (x == y)
{
System.out.println("x and y are equal");
}
https://codehs.com/tutorial/zach/java-documentation-basics 3/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS
if (x > 5)
{
System.out.println("x is greater than 5.");
(/home) Curriculum (/curriculum) Tutorials (/tutorial/ ) Java Documentation - Basics
}
Math
Operators
+ // Addition
- // Subtraction
* // Multiplication
/ // Division
% // Modulus (Remainder)
() // Parentheses (For order of operations)
Examples
int z = x + y;
int w = x * y;
x++
x--
Shortcuts
x = x + y; x += y;
x = x - y; x -= y;
x = x * y; x *= y;
x = x / y; x /= y;
RELATED TUTORIALS
https://codehs.com/tutorial/zach/java-documentation-basics 4/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS
https://codehs.com/tutorial/zach/java-documentation-basics 5/5