Declaring and Initializing Variables:

image.png

Variables defined in a method are called “local varibales” since they are local to the method that was created.

public class MyFirstClass {
    public static void main(System[] args){
        System.out.println("Hello World");
        int x;
        x = 10;
        System.out.println(x);
        x = 5;
        System.out.println(x);
        x = x + 1;
        System.out.println(x);

        int MyFinalScore = 10;
        System.out.println(MyFinalScore);

    }
}

MyFirstClass.main(null);

Hello World
10
5
6
10

8 Primitive Data Types:

These 4 all hold integer values but having different ranges:

Byte: -128 to 127 (inclusive)

Int: -32768 to 327687 (inclusive)

Short: -2^31 to 2^31-1

Long: -2^63 to 2^63-1

The next two hold decimals, but double is more precise:

Float

Decimal

Rest are self-explantory:

Boolean: True or False

Char: Holds a character

For the AP Test, you need to know: int, double, and boolean

public class PrimitiveDataTypes {
    public static void main(String[] args){
        int a = 3;
        double b = 3.0;
        b = 3;
        //a = 3.0; doesn't convert because double can't convert to int.
        a = (int)3.999;
        System.out.println(a);

        boolean c = true;
        c = false;
        System.out.println(c);
    }
}

PrimitiveDataTypes.main(null);
3
false

Arithmetic and Uninary Operators:

public class Operators{
    public static void main(String[] args){
        int x = 3;
        System.out.println(x);
        System.out.println(3 + 3);

        x = 3 * 3;
        x = 3 * x;
        System.out.println(x);
        System.out.println(4 % 3); //output is remainder

        //x++; is x = x + 1;
        //x--; is x = x - 1;
        //x =* 3;  is x = x * 3;
        
        boolean z = true;
        System.out.println(!z);

    }
}

Operators.main(null);
3
6
27
1
false
public class IntDoubleDivision {
    public static void main(String[] args){
        System.out.println(5/3); //int division, will return 1
        
        System.out.println(5.0/3.0); //double division, will return 1.6667

        //System.out.println(5/0) will return error

        System.out.println(5/0.0); //Infinity

        System.out.println(-5/0.0); //Negative Infinity

        System.out.println(0.0/0.0); //Returns Nan (Not a Number)
    
    }
}

IntDoubleDivision.main(null);
1
1.6666666666666667
Infinity
-Infinity
NaN

Operation Precdence:

  • Some operators will be processed first, depending on their precedence (PEMDAS)
  • Precedence is same? Code goes left to right
  • Best used with parenthesis to clarify the order of execution

image.png

public class PEMDAS {
    public static void main(String[] args){
        int x = 9;
        int y = 6;
        System.out.println((double)x++/(3-1*2) > ((5-2) * 3));
        System.out.println(x);
        System.out.println((double)x);
        // ((double)x++/1 > 9)
        // ((double)9.0 > 9)
        // returns false
        // if we returned x after, it would return 10

    }
}

PEMDAS.main(null);
false
10
10.0

Casting a Primitive and Overflow:

  • Casting primitive data type allows us to convert a peice of data from one type to another type
  • Widening means converting a data type to a data type with more precise/more space
  • Narrowing means converting a data type to a data type with less precision/less space
  • For AP Exam, you only need to know about int, double, boolean
public class Casting{
    public static void Example1() {
        double a = 3.0;
        System.out.println(a);
 
        double b = 4; //in this case, the system automatically **widens** 4 to 4.0 
        System.out.println(b);
 
        double c = (double)5; //manually widens 5 to 5.0
        System.out.println(c);
 
        // int d = c; will cause error, c needs to narrow down from a double to int
 
        //narrowing must be done manually
        int d = (int)c;
        System.out.println(d);
 
        int e = (int)3.95;
        System.out.println(e); //truncate 3.95 --> 3
    }

    public static void Example2() {
        int x = 10;
        byte y; //value from -128 to 127

        // y = x; will cause error because of "lossy conversion", meaning system doesn't want to loose data
        y = (byte)x;

        System.out.println(y);

    }

    public static void main(String[] args){
        System.out.println("Example 1:");
        Example1();

        System.out.println(" ");
        System.out.println("Example 2:");
        Example2();
    }
}

Casting.main(null);
Example 1:
3.0
4.0
5.0
5
3
 
Example 2:
10

Swapping Variable Values:

  • Switch values of two variables
public class SwitchVariables {
    public static void WrongWay() {
        int x = 3;
        int y = 6;

        //wrong way, you are essentially declaring all variables equal to 6
        x = y;
        System.out.println(x); //x = 6 (y is 6);
        y = x;
        System.out.println(y); //y = 6;
    }

    public static void RightWay() {
        int x = 3;
        int y = 6;

        //correct way, declare a temporary variable that holds a value
        int temp = y;
        y = x;
        x = temp;
        System.out.println("x-value (or temp): " + temp);
        System.out.println("y-value: " + y);
    }

    public static void main(String[] args){
        System.out.println("Wrong Way:");
        WrongWay();

        System.out.println(" ");
        System.out.println("Right Way:");
        RightWay();
    }
}

SwitchVariables.main(null);
Wrong Way:
6
6
 
Right Way:
x-value (or temp): 6
y-value: 3