- 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]+" ");
}
What's Next?
Collapse Content
Show Content
This module focused on the essentials of programming. You can now practice the Logic and Loops Practice Challenges or the Array Practice challenges.
Next, you should learn about Objects in the next module:
Object-Oriented Programming on Your Computer
Or skip to the following ones:
You can also view a chart of the Java content and challenges.
Comments
Christine
Dec 28, 2:24 AMthisis good!
calo
Jan 14, 3:15 AMhappy to finish and continue
Ayesha
Jan 14, 10:41 PMAm so glad i found this site.. good work. thanks a lot.
vaggelas
Jan 16, 12:05 PMOne of the best interactive tutorials i have seen until now.Good job!
adrian apostol
Jan 31, 1:58 AMfinished
Robin Elliott
Feb 3, 10:29 PMThis was truly a great intro to Java programming. The challenges were engaging and rewarding, and the material conceptually explained in a way that a beginner like myself could grasp. It's a step forward for learning.
Shadov
Mar 23, 2:58 AMYay! Basics are cleared now. This was VERY basic.
Ed Morin
Apr 1, 4:57 AMPretty neat! I was looking for something my son could try and this seems like a good possibility for him to try. Thanks!
Atiim01
May 27, 3:58 PMWell that was fast
Davin
Jun 13, 2:07 AMPretty neat site. Love it!
qingyang.xu
Apr 28, 5:05 PMdone