Casting

Below, there are two examples of casting. Both examples convert a double data type to integer via truncation. 1.511 is outputted as 1 in the first line. 28.2 is printed as 28 in the second line. This can simplify the output and make it easier to operate on in future code as well.

double d1  = 6.53;
double d2 = 4.32;

// Casting for division
int dd = (int) (d1/d2);

// Casting for multiplication
int md = (int) (d1*d2);

System.out.println(dd); // output of division, is integer
System.out.println(md); // output of multiplication, is integer
1
28

Wrapper Classes

Wrapper class allow the use of primitive data types like integers as classes, and therefore use methods and code that otherwise would not be allowed. Below, the toString method can not be used with the primitive, but works with the wrapper.

int i = 1;
Integer ii = 1;

// String unwrapped = i.toString(); 
String wrapped = ii.toString();

System.out.println(i);
System.out.println(ii);
|   String unwrapped = i.toString();
int cannot be dereferenced

Concatenation

Concatenation is combinining two or more strings.

String s1 = "hello";
String s2 = "world";
String conc = s1.concat(s2);
System.out.println(s1+ " " + s2);
System.out.println(conc);
System.out.println(s1 + " " + s2 + " " + 12);
hello world
helloworld
hello world 12

Math Class

Allows math operations. Absolute value and random.

int ex = -1;
System.out.println(Math.abs(-1));
// random between 1 and 10
System.out.println((int)(Math.random() * (100-10) + 10));
System.out.println((Math.random() * (10-1) + 1));
1
27
2.393138714397412

Compound Boolean Expression

Compound boolean expressions are when multiple boolean expressions are nested within each other.

boolean a = true;
boolean b = false;
System.out.println(!a && !b);
false

Truth Tables

Help visualize compound boolean expressions and make it easier to evaluate.

DeMorgans Law

Make boolean expressions easier.

boolean a = true;
boolean b = false;

System.out.println(!(a&&b));
System.out.println(!a || !b);
System.out.println(!(a||b));
System.out.println(!a && !b);
true
true
false
false