Unit 1 Java Primitives
boolean a = true;
int b = 5;
double c = 7.5;
char d = 'a';
The Math class allows you to use different math related methods such as min, max, avg, sin, abs, etc. Namely, there is the java.lang.Math.random() method which will give a random number between 0.0 and 1.0.
double x = 28;
double y = 4;
double z = Math.random();
// return the maximum of two numbers
System.out.println("Maximum number of x and y is: " +Math.max(x, y));
// return the square root of y
System.out.println("Square root of y is: " + Math.sqrt(y));
//returns 28 power of 4 i.e. 28*28*28*28
System.out.println("Power of x and y is: " + Math.pow(x, y));
// return the logarithm of given value
System.out.println("Logarithm of x is: " + Math.log(x));
System.out.println("Logarithm of y is: " + Math.log(y));
// return the logarithm of given value when base is 10
System.out.println("log10 of x is: " + Math.log10(x));
System.out.println("log10 of y is: " + Math.log10(y));
// return the log of x + 1
System.out.println("log1p of x is: " +Math.log1p(x));
// return a power of 2
System.out.println("exp of a is: " +Math.exp(x));
// return (a power of 2)-1
System.out.println("expm1 of a is: " +Math.expm1(x));
// returns a random number
System.out.println(z);
Operators
Addition (+)
Subtraction (-)
Division (/)
A casting for doubles will temporarily create a double value so that instead of having an int, it will have a double temporarily.
Ex: 7/(double)3 will make a temporary 3.0
A casting for integers or rounding will temporarily round a double value so that it is an integer.
Ex: 7/(int)3.333 will make a temporary 3
Multiplication (*)
Increment (++, --)
int a = 5 + 10;
int b = 20 - 5;
int c = 45 / 3;
int d = 5 * 3;
System.out.println(a + b + c + d);
Video Learning Notes
A byte is just 8 bits. Int is used for integer values. Floats are used for values with few decimal digits, while doubles are used for values with many decimal digits. A boolean is used for false or true values. A char will have a single character. A string will have multiple characters.
byte a = 12;
int b = 5;
float c = 6.3f;
double d = 7.344345542;
boolean e = true;
char f = 'W';
String g = "Hello";