- 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 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]+" ");
}
Variables in Programming and Algebra
Now that you know what a variable is, we will clarify a common area of confusion between algebra and programming.
In algebra, you can have a statement like this:
x = 2 + 3
This is a "truth-statement" that the variable x
is equal to 2+3 or 5.
Similarly, in programming, you can write int x = 2 + 3
and the variable x
will end up as the integer 5.
However, there's a big difference between the two statements, as will now be seen.
What if you faced the following two statement together?
x = 2 + 3
x = 11
In algebra, you would immediately recognize that this is impossible. x
cannot be both 2+3
and 11
since they're not equal. However, in programming, =
is not a statement of truth, it's an action that assigns a value to a variable.
So you can assign 2+3
to x
and then afterwards assign 11
to x
, and x
will now be set to 11
instead of 5.
Equality Check
If you wanted to check if something was equal in programming, you use ==
. For example:
int x = 2 + 3; //assignment
x == 11; //this would be false
Review this:
=
assignment==
equality check
Challenge
After the following code runs, what number will sum
equal?
int x = 2;
int y = 5;
x = 100;
int sum = x+y;
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Matthew
Oct 17, 4:26 PMEasy I'm liking this website.
Ricky
Dec 22, 1:02 AMEnter Integer or Decimal Answer: I expect int sum == answer
Jamison Norwood
Dec 26, 2:03 AMI love this site. Now every other book is looking like a reference sheet.
Brent Jones
Jan 6, 12:05 AMYou say "In the following code, what number will sum equal?"
It would be better to say "In the following code, what number will sum become?"
Also, you should give an extra example of multi-line assignment of variables before testing student understanding
int x = 1;
int y = 333;
int total = x+y \ total is a container containing the number 334
int x = 5; \ x is now assigned the value of the number 5
int total = x+y \ total is a container which has been changed to contain the number 338
Perhaps using sum or total as variable names is not a good idea. Maybe int z = x + y
For beginners, operators can be easily confused with variables
heya
Mar 4, 1:14 PMThis is amazing thank you so much
Kenneth Au
Mar 27, 8:20 AMThanks to this website, I dont feel like a dumb as.. anymore!
xiaochayou
Mar 31, 1:39 AMso good, I like it so much!
Good site so far :)
Santineida Oliveira Ribeiro
May 7, 11:45 AMI'm not able to answer.
Derpybunneh
Sep 1, 7:33 PMIt's easy. Forget the int x = 2 and go to x = 100 and add the y to the x.
Paul Lee
Mar 10, 4:16 PMI have increased my programming knowledge by 500% in the last 10 minutes.
Gomati Damle
Jul 5, 3:30 PMIn the code why it is written
int x = 2;
int y = 5;
x = 100; .Can anyone tell me why data type int is not written befor x=100;
Learneroo
Jul 5, 11:06 PMAs mentioned on the previous page, you need to declare a variable's type the first time, but afterwards you can use it without re-mentioning what type it is.
thales
Jul 7, 6:54 AMYes, but i want to learn programming. And isnt this a bit to easy to really learn programming?
Learneroo
Jul 7, 9:26 AMYou can start off easy. There's lot's of challenging material later if you want it!
thales
Jul 7, 11:02 AMok i want that challenging stuff :-)
Learneroo
Jul 7, 11:04 AMWhen you're ready, check out Algorithms
thales
Jul 7, 12:11 PMwe will, but we are not ready yet :-)
Heckingson Jumbo
Sep 18, 2:41 PMthis is just like js and c
Dion Matthew Levy
Sep 30, 12:43 AMThis was my hardest challenge yet...How does 100 + 5 = 105 if x=2???? Computers are DU|\/|@$$3$ !!!! If X(2)=100 And X(1)=2 and Y(5) then wouldn't x+x+y =107? the only place xxy ever made sense was in Klinefelter syndrome! :p
Gomati Damle
Jun 6, 7:57 PMThanks.
chima oscar
Jul 26, 8:11 AMGot confused here since x = 100; wasn't given any data type.
Ershad
Sep 24, 8:39 AMGreat Website! Thanks.
paul. kirsch
Jun 18, 12:25 AMThis website makes a lot of sense, because a lot of people are not employed or partially employed and can't afford the high monthly costs of other training programs. Also, the questions are simple
enough to build confidence rather than destroy confidence as many other training sites, (including free ones) do. Learneroo, you have my loyalty.
COD
Feb 16, 8:02 AMAm so great-full for the amazing challenges on this website