 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.
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.%
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(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;
}
}
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 5cell 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 forloop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Math and Comparison Operators
Basic Math Operators
You've already used +
and /
to add and divide. Java also includes 
and *
for subtraction and multiplication. In addition, there's another basic math operator %
(it's called mod, and it uses the percent symbol) which is used to get the remainder after division. For example, 5 % 2
will return 1 (the remainder of 5 / 2).
Order of Operations
When you combine multiple operations in one line, Java is similar to the standard order of operations (PEMDAS):
 Parentheses
 Multiplication and Division and Mod
 Addition and Subtraction
Operations on the same level will be evaluated in order from left to right, just like the standard convention.
Comparison Operators
We've seen that =
assigns a value and shouldn't be confused with ==
which checks if two values are equal. These are all the ways to compare values:
Start with int a = 5;
symbol  meaning  true example 

==  equal to  a == 5; 
!=  not equal to  a != 3; 
>  greater than  a > 3; 
>=  greater than or equal to  a >= 5; 
<  less than  a < 6 
<=  less than or equal to  a <= 5; 
All Order of Operations
This table summarizes the order of all the operations. The higher an operation appears, the higher its precedence. (You can view the full table on the Java Tutorials.)
Name  Symbol 

not  ! 
parenthetical  ( ) 
multiplicative  * / % 
additive  +  
relational  < > <= >= 
equality  == != 
logical AND*  && 
logical OR*   
assignment  = 
*The logical operators will be covered soon.
General Tip
If you need to reference some Java quickly while learning this module, you can click on the button on the sidebar to see sample Java code.
Challenge
Given two numbers a
and b
, return the remainder when their product is divided by their sum.
For example, if given 2
and 5
, return 3
(since their product 10
divided by their sum 7
leaves a remainder of 3
).
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
What is the value of num
after the following code?
int num = 15 / (1 + 2) % 3;
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Jay Learn
Jul 23, 1:23 AMI solved this problem with first declaring sum and mul as integers. Then returning their values using modulus.
Ryan
Nov 3, 10:48 AMthe question confused me, when did i have to multiply at any point???
Learneroo
Nov 3, 11:40 PM@Ryan, the product of
a
andb
means multiplya
byb
. Would this be clearer:Multiply
a
byb
and then get the remainder from dividing that product by the sum ofa
andb
?Ryan
Nov 4, 8:19 PMYeah, thank you. The product made me think addition but i guess that would be the sum. Maybe i should have taken more math classes lol. So far this has been the best teaching program i have used
online. Some of the ?'s are somewhat difficult but thats what learning is about. Thank you for the email and reply.
Brent Jones
Jan 6, 12:16 AMExcellent tutorial thus far. It is probably hard to understand the precise syntax that is needed to pose the problems.
Denis Semendyayev
Jan 7, 3:14 PMi was so confused by the wording of product. to me it processed as the same thing as sum :/
Matt
Apr 27, 11:42 AMI read the challenge about 5 times and operated exactly what they wrote: return (a*b)%(a+b);
I hope this helps. Wording can be tricky but it's all going back to basic math terminology.
Adhurim Esati
Jul 2, 5:41 AMThe remainder of 5 % 3 could have been 1 too.
ArtTric
Aug 6, 6:50 PMThis one tripped me too. It can be done this way as well:
int product = a * b;
int sum = a + b;
return product % sum;
Mitchell
Mar 20, 6:23 PMPEMDAS is also known as BEDMAS (brackets instead of parentheses)
thales
Jul 7, 6:59 AMI am a math teacher so this wasnt to hard for me :)
Joseph Estridge
Sep 12, 3:13 PMI have a question... I understand that you can just return the value of "(a * b) % (a + b);" ... But I also tried doing this one by assigning to value to a new variable, int C. I added an int
C in public static int, and set the equation equal to C, returned C, but the code didn't work. How would I store its value to a new variable?
Eze Sunday Eze
Jan 29, 4:34 PMi got confused, i got it when i read the question again.
Ahmad Banoori
Jun 29, 5:02 PMGood way of learning..
Shandeep Kumar
Dec 23, 9:33 AMthat's really awsome man
Roberto Blanco Axelsson
Mar 7, 9:29 AMMy solutions was like ArtTric
Enoch
Mar 31, 7:37 PMI don't know where to input int product = a*b;
Int sum = a+b;
paul. kirsch
Jun 18, 2:11 AMI got it correct after 3 tries. I may be wrong but I surmise that '%'is to be put between code that identifies what variables you are working with such as 'sum' and really means: "divide and also give me the remainder".
hunter
Sep 6, 3:15 PMyeeyee
Kurt
Jan 27, 2:12 PMsomething is wrong here because how is 5*1 divided by (5+1) have a remainder of 5?
basic math says its .8 with a remainder of 2. so 5*1 is 5. well 5 divided by 6 equals 0.8 r2
Kurt
Jan 27, 2:14 PMI get the code, but it has a flaw because 5*1 divided by 6 does not have a remainder of 5. its 2
because basic math says that 5/6 is 0.8 with a remainder of 2.
lucifer
Feb 11, 1:57 AMguys i did this
//int num
//(1*5)%(6);
return (a*b)%(a+b)
noel
May 1, 6:05 PMint mul = a * b;
int sum = a+b;
int result = mul%sum;
return result;
Santo Credendino
Aug 9, 9:44 AMWhat is going on in this solution:
Applying the rules for Math and Operator Order of Precedence
1. addition is performed first because of ( )
2. multiplication is the next operation
solution:
return ( a * b % ( a + b ));