Java Vocab
Type Casting
Type casting in Java is when you assign one primitive data type to another data type. The two types of casting are as follows:
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> doubleNarrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
public class wideningCasting {
public static void main(String args[]) {
int myInt = 9;
double myDouble = myInt;
System.out.println(myInt);
System.out.println(myDouble);
}
}
// widening is done automatically when you assign a smaller type to a larger one
wideningCasting.main(null);
public class narrowingCasting {
public static void main(String args[]) {
double myDouble = 9.5;
int myInt = (int) myDouble;
System.out.println(myDouble);
System.out.println(myInt);
}
}
/*
in order to convert a larger data type to a smaller one, you must use (dataType) when defining the variable, or
you will recieve an error about lossy conversion
*/
narrowingCasting.main(null);
Wrapper Classes
A wrapper class provdies a way to use primitive data types as objects. Examples of primitive types and wrapper classes below.
Primitive Type | byte | short | int | long | float | double | boolean | char |
---|---|---|---|---|---|---|---|---|
Wrapper Class | Byte | Short | Integer | Long | Float | Double | Boolean | Character |
Java example -
ArrayList<int> myNums = new ArrayList<int>(); // invalid
ArrayList<Integer> myNums = new ArrayList<Integer>(); // valid
// just add the strings and java does the work for you
public class additionConcat {
public static void main(String args[]) {
String string1 = "Hello ";
String string2 = "World!";
String string3 = string1 + string2;
System.out.println(string3);
}
}
additionConcat.main(null);
// the manual concat() method
public class manualConcat {
public static void main(String args[]) {
String string1 = "Hello ";
String string2 = "World!";
System.out.println(string1.concat(string1));
System.out.println(string1.concat(string2));
System.out.println(string2.concat(string2));
System.out.println(string2.concat(string1));
}
}
// using the concat() method is a more dynamic and customizable way to concatenate strings
manualConcat.main(null);
import java.lang.Math;
public class mathRandom {
public static void main(String args[]) {
/*
* This generates a random number between 0-1, multiplies this large decimal by 100, and then rounds it,
* so it's basically generating a random number between 0-100
*/
for(int i = 0; i < 10; i++) {
System.out.println(Math.round(Math.random()*100));
}
}
}
// the random class is much better in my opinion than Math.random, but this method will generate a random double between 0-1
mathRandom.main(null);
Compound Boolean Expressions / De Morgan's Law / Truth Tables
AND | OR | NOT |
---|---|---|
&& | || | ! |
De Morgan's Law:
When the NOT
expression is applied to an AND
, it will switch it to an OR
. The same happens from OR
to AND
.
Truth Tables:
A truth table is just a way of organizing true/false conditions.
A = true
B = false
C = true
D = true
AND | OR | XOR | |
---|---|---|---|
A, B | false | true | false | C, D | true | true | true |
public class compBools {
public static void main(String args[]) {
if(true && false) {
System.out.println("True and false.");
}
if(true || false) {
System.out.println("True or false.");
}
// "NOT(true AND false)" is equivalent to "false OR true"
if(!(true && false)) {
System.out.println("False or true.");
}
}
}
compBools.main(null);