- Introduction
- Super Simple Formula
- Your First Program
- Variables, Methods and Parameters
- Variables in Programming and Algebra
-
Math and Comparison Operators - If Statement
- While and For Loops
- Loopy Thinking
-
Data Types - Booleans
- Logical Operators (and Booleans)
- Output and Printing
- Printing and Loops
-
Arrays - Arrays and Loops
- Array Loop Practice
-
What's Next? - Quick Reference
Quick Reference
Quickly lookup Java basics, and load code examples directly into editor.
Method Basics
The header of the method declares what the method will return (such as an
Syntax note: Every line of code in Java needs to end with a
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.
Variables and Data types
Operations Booleans and Logic
Math operations are performed in the standard order. Note that division of integers will round down to nearest integer. The mod
%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if else and while
The if statement will execute code only if a condition is true:
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
if(condition){
//doSomething
}
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i < 10){
System.out.println(i);
i = i + 1;
}
}
Printing and Arrays
Print a newline
The above prints text and add a newline afterwards.
This code creates an empty 5-cell array:
This shortcut creates an array with numbers 0 to 4:
You can use for-loop to print values of array:
System.out.println("");
The above prints text and add a newline afterwards.
System.out.print
prints without a newline, but you can add in spaces " " to separate text.
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets []
. This code creates an empty 5-cell array:
int[] ar1 = new int[5];
This shortcut creates an array with numbers 0 to 4:
int[] nums = {0,1,2,3,4};
You can use for-loop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Quick Reference
Collapse Content
Show Content
Below is a quick reference on the Java covered in this module. You can also reference it on the sidebar on each page.
Quick Reference
Method Basics
The header of the method declares what the method will return (such as an
Syntax note: Every line of code in Java needs to end with a
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.
Variables and Data types
Operations Booleans and Logic
Math operations are performed are performed according to the standard order of operations. Note that division of integers will round down to nearest integer. The mod
booleans are used to store true/false values, such as the results of comparisons:
%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if else and while
The if statement will execute code only if a condition is true. The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i<10){
System.out.println(i);
i = i + 1;
}
}
Printing and Arrays
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets
System.out.println
will print text and add a newline afterwards. System.out.print
will just print text, but you can add in spaces " ".
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets
[]
.
//create an empty 5-cell array
int[] ar1 = new int[5];
//create an array with numbers 0 to 4
int[] nums = {0,1,2,3,4};
//use for-loop to print values of array
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Comments
tovit krakowski
Oct 29, 10:05 PMI like this quick reference!
Justin Finete
Feb 20, 6:34 PMREally Cool!
Amboss
Mar 4, 3:20 PMnice one
Yueh-Wen Cheng
Apr 5, 4:09 AMthanks :)
sayccz
Apr 21, 2:48 AMthanks :)
bhavana
May 5, 9:17 AMwhat next?
Learneroo
May 5, 9:28 AM@bhavana, please see the previous node, whats next
許友誠
May 5, 12:06 PMthanks :)
Angel
May 18, 8:26 PMnice one
CY
May 30, 10:47 PMnice
qingyang.xu
Apr 28, 5:04 PMgood