IntroCS Chapter 1.2 Builtin Data Types

San Skulrattanakulchai

September 21, 2016

Topics

Types

Builtin Types

Type Size in Bits Sample Operators Literals
boolean unspecified &&   ||   ! true   false
byte 8 +     *   /   %
char 16 +     *   /   % ‘a’   ‘0’   ‘\\’
short 16 +     *   /   %
int 32 +     *   /   % 321   0x3A
long 64 +     *   /   % 3L
float 32 +     *   / 3.21F   14.2e23f
double 64 +     *   / 3.21   14.2e23
string + “Hello”
array []

Literals

Operators

String Type

Variables & Identifiers

Expressions

Coercion and Casting

Methods and calls

Math class

Generating a Random Number

public class RandomInt { 
    public static void main(String[] args) { 
        int n = Integer.parseInt(args[0]);

        // a pseudo-random real between 0.0 and 1.0
        double r = Math.random(); 

        // a pseudo-random integer between 0 and n-1
        int k = (int) (r * n);

        System.out.println("Your random integer is: " + k);
    }
}