- 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]+" ");
}
Booleans
Booleans
One of the 4 basic types was the boolean
data type, which can store either true or false values. As with ints and Strings, you need to declare the type (boolean
) and name, and can assign it a value(true
or false
). For example:
boolean alive = true;
boolean hungry = false;
boolean haveMoneyLeft = true;
Notice the booleans are not Strings (which would be in quotes), but their own basic values: true
and false
.
Assigning booleans based on expressions
Often, you don't directly assign true
or false
to a boolean, but instead provide it with the result of a comparison. For example:
boolean haveMoneyLeft = yourMoney > yourDebts;
yourMoney > yourDebts
is a comparison which will return either true or false. That result will then be stored in the boolean variable haveMoneyLeft
. This boolean can then be used later.
Using boolean variables in conditional expressions
In previous nodes, we showed how you can use if
, while
and for
statements to execute code only when a certain comparison is true, such as:
if(yourMoney > yourDebts){
buyStuff();
}
Sometimes you may want to store the results of a comparison to use later. You can use the boolean
data type to do so. For example:
boolean haveMoneyLeft = yourMoney > yourDebts;
if(haveMoneyLeft){ //this boolean will be true or false
buyStuff(); // this will only get executed if haveMoneyLeft is true
}
The boolean haveMoneyLeft
can then be used multiple times (so one can check if there's still money without rewriting the whole comparison). However, if 'yourMoney' or 'yourDebts' changes in value, 'haveMoneyLeft' will not automatically check if it should change in value too, so you may need to run the original comparison again then.
Not
If you only wanted to run code if a boolean was false, you could write an equality to check that. For example, to see if haveMoneyLeft was false, you could check
if(haveMoneyLeft == false)
.
You can also use the NOT operator to get the opposite of a boolean. In Java, an exclamation point is used for NOT: !
.
For example:
if( ! haveMoneyLeft){
declareBankruptcy();
}
Challenge
You will be given two integers a
and b
. Return true
if a
is a multiple of b
, and false
otherwise.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Rod
Nov 5, 3:53 AMThis error was returned. What is the problem with this syntax?
Main.java:6: error: unexpected type
return ((a%b) = 0);
^
required: variable
found: value
1 error
Learneroo
Nov 5, 3:14 PM=
is an assignment, you probability want an equality check==
Adhurim Esati
Jul 13, 7:49 AMI didn't finish reading the whole lesson but what if
{
boolean haveMoneyLeft = yourMoney > yourDebts;
}
What if I have a debt of 500gold and yourMoney is 500gold too
this makes:
{
boolean haveMoneyLeft = false;
}
right ?
Bernard Mitchell
May 24, 10:13 PMNice this was an easy one. thanks again for the great site.
Asadujjaman Shimul
Dec 3, 2:39 AMimport java.util.Scanner;
public class BooleanExm
{
public static void main(String[] args)
{
double a;
double b;
Scanner input = new Scanner(System.in);
}
rajni dound
Nov 18, 3:17 PMimport java.util.println;
public class Main(){
public static void main(String[] args){
int a = 4;
int b = 2;
if(a%b==0){
System.out.println("true");
}else{
System.out.println("false");
}
rajni dound
Nov 18, 3:19 PMimport java.util.Scanner;
public class Main(){
public static void main(String[] args){
int a = 4;
int b = 2;
if((a%b)==0){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
Mike Grote
Mar 15, 11:41 AMKeep it simple, the answer is right there.....
boolean x;
if(a%b ==0){
x = true;
}
else{
x=false;
}
return x;
}
Kerry Pollock
Aug 12, 2:17 PMIn my solution, I used only =, not == , and I got a correct response. Can you tell me why that is? Thanks.