Java: Basic control flow
Introduction
Why Java
- Cross platform between desktop environments
- Strongly typed
- Garbage collected
- Compatibility with other languages
Products made with Java
- Minecraft
- Blu Ray players
- Phonegap
Key structures
Overview of keywords
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
Variables
// Simple variables
boolean test = true;
Color color = Color.RED;
String hello = "hello ";
String world = "world!";
Random random = new Random();
String helloWorld = hello + world;
int number = 1;
double decimal = 0.5d;
// Generic ("type based types") variables
List<People> people = new ArrayList<>();
Cage<Lion> lionCage = new Cage<>();
Variables quiz
? test = 0.5d;
? age = 22;
? list = new ArrayList<>();
double test = 0.5d;
int age = 22;
long age = 22;
List<String> list = new ArrayList<>();
ArrayList<String> list = new ArrayList<>();
List<?> list = new ArrayList<>();
ArrayList<?> list = new ArrayList<>();
Loops
// For loop
for (int i = 0; i < 30; i++) {
}
// while loop
boolean looping = true;
while (looping) {
looping = false;
}
// Do while loop
boolean looping = true;
do {
looping = false;
} while(looping);
// Foreach
String[] list = {"Hello", "World"};
for (String item : list) {
}
Condition statements
String red = "red";
String blue = "blue";
if (red.equals(blue)) {
System.out.println("blue is red");
} else {
System.out.println("blue is not red");
}
switch (red) {
case "red":
System.out.println("red is the color of roses");
break;
case "blue":
System.out.println("blue is the color of the sky");
break;
case "green":
System.out.println("green is the color of grass");
break;
case "yellow":
System.out.println("yellow is the color of Mcdonald's");
break;
}
User input
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line.reverse());
}
Data storage constructions
Structures
Contents | Unique | Size changeable | |
---|---|---|---|
Array | values | false | false |
List | values | false | true |
Set | keys | true | true |
Map | keys,values | true | true |
Array
String[] test = {"hi", "there"};
int[] numbers = {10, 20, 30};
int[] empty = new int[20];
empty[0] = 100;
System.out.println("Element at index 0: " + empty[0]);
List
List<String> list = new ArrayList<>();
List<String> list = new LinkedList<>();
list.add("hello world");
Set
Set<String> set = new HashSet<>();
set.add("hello world");
set.add("hello world");
set.add("hello");
set.add("world");
System.out.println(set);
Map
Map<String, Integer> map = new HashMap<>();
map.put("hello world", 1);
map.put("hello world", 2);
map.put("hello", 3);
map.put("world", 4);
System.out.println(map);
More?
Java: Basic conrol flow
By Fernando van Loenhout
Java: Basic conrol flow
- 62