Basic Java Cheat Sheet
Basic Java Cheat Sheet
Java Variables 2
Operators in Java 3
Type Conversion in Java 4
Comparing Stuff in Java 5
Loops in Java 6
Arrays in Java 7
Methods in Java 8
Classes in Java 9
Inheritance in Java 10
Usage notes:
1
Java Variables
Variables are the smallest practical working blocks of any application. They are essentially small
pieces of data stored in special purpose containers.
int a = 9;
int x = -99;
double b = 9.879;
Floats also store decimal numbers but they have a lower accuracy than Doubles:
boolean d = false
Variables are the basic building blocks of all software. They cannot be mixed so you cannot store
an int inside of a string or vice versa.
2
Operators in Java
Operators allow sums between numbers to take place. They also allow concatenation of strings
(adding words onto other words).
Adding a number:
int myNum = 9;
Subtracting a number:
Multiplying a number:
Dividing a number:
(The result would be equal to 0). Wait what? Well, ‘int’ can only hold a whole number and 9 / 10
= 0.9. Therefore ‘int result’ simply drops the ‘.9’ part! Be careful when performing arithmetic!
Adding Strings
3
Type Conversion in Java
You can’t mix types in Java but you can convert between them (with some limitations). If you can
avoid conversion then DO as conversions are where insidious errors can lurk - waiting to crash
your program.
Alternatively:
Why an error? You cant convert a word to a number so your app will kick and scream, bringing
the whole thing down. This is why you should avoid type conversion where possible.
4
Comparing Stuff in Java
if else statements:
String a = "grant";
String b = "grant2";
if (a.equals(b)) {
System.out.println("equal");
}
else if (!a.equals(b)) {
System.out.println("not equal");
}
else {
System.out.println("default");
}
int a = 9;
int b = 10;
if (a == b) {
System.out.println("equal");
}
else if (a != b) {
System.out.println("not equal");
}
else {
System.out.println("default");
}
int a = 4;
switch (a)
{
case 3:
System.out.println("3");
break;
case 4:
System.out.println("4");
break;
default:
System.out.println("default");
break;
}
5
Loops in Java
while loops:
int i = 0;
(run code in here each time we go around and i is less than 100)
i++; (adds one to i each time we go around)
}
for loops:
These are kind of like while loops but with a little more control
6
Arrays in Java
What if you’d like to hold a list of items, all of the same type?
(Why do you write ‘1’ and not ‘2’ to get second element? Because all indexing in programming is
based on numbers starting at ‘0’ so our program counts - 0, 1, 2, 3 etc. )
7
Methods in Java
What if you want to do stuff to your variables? It would be horrible to write ‘myNum + otherNum’
EVERY time you needed it. Methods allow you to store that code in one place that can be
accessed multiple times and from multiple places.
Calling a method:
8
Classes in Java
You have variables and methods now. Where should you put them? In classes! Classes have 2
objectives - the first of which is to organise your code. Java also generally expects variables and
methods to be enclosed in classes - it’s kind of obsessive like that!
Define a class (must be contained in a Java file of the same name as the class):
The second thing classes do is become blueprints for types of objects. In this case the class
holds a blueprint for a car.
Classes are mighty useful for defining new kinds of objects and VASTLY reducing code reuse.
Making objects from classes is one of the core principles of ‘Object Oriented Programming’. You
may see this abbreviated to OOP in some places.
9
Inheritance in Java
Classes don’t have to be redefined every time you make them. They can ‘inherit’ other class
properties and methods.
public Vehicle () {
}
}
10