Java Wrappers

Booleans

  • true and false
  • one bit

Integers

  • int values
  • 2-3 bits

Double

  • decimal values
  • 64 bits

Character

  • A single character
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);
Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12
0.09599477421748714

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);
60

Video Learning Notes

Primitive Data Types in Java

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";

Homework

Homework