Why to use Java? (1.1)

Java is an important programming language that is widely used throughout the tech industry and contains many important programmming concepts such as object oriented programming.

Basic Java (1.1)

  • All code that runs must be in the main method (shown below)
  • To print something, use System.out.print(); and inside the parenthesis put text in quotes (String)
    • To print with a new line, use System.out.println();
  • To comment your code, use // for single line comments and /**/ for multiple lines (example will be shown below)
    • It is important to describe how and why your code works, however dont describe obvious things
public class Example {

    public static void main(String[] args) {

        System.out.print("Hello World");  

    }
}

Example.main(null);
Hello World

List of Data Types (1.2)

  • Data types are different categories in which one can store various types of data.
  • The main Primitve data types are:
    • Integer (int): used for whole numbers
    • Double (double): used for numbers with decimals
    • Boolean (boolean): used for true or false conditionals
  • For Primitive types, variables store actual data instead of reference
  • If the variable is declared final, it cannot be edited
  • A non Primitive type which is commonly used is String
    • Stores text
public class Example {

    public static void main(String[] args) {

        int Herbo = 10;
        double gasPrices = 7.99;
        final boolean Hot = true;

        System.out.println(Herbo);
        System.out.println(gasPrices);
        System.out.println(Hot);

        // Hot = false; cannot assign a value to final variable Hot 

    }
}

Example.main(null);
10
7.99
true

Operators (1.3)

  • In order to perform mathmatical calculations on integers and doubles, you can use operators
  • Main ones are +, -, *, /
    • These are what you expect
    • When dividing integers, it always rounds down because output must be an integer
    • When dividing by 0, will get the ArithemticException Error
  • Modulus is %, used to get remainder when two numbers are divided
public class Math {

    public static void main(String[] args) {

        int number = 2;
        int number2 = 5;
        double number3 = 2.0;
        double number4 = 5.0;

        System.out.println(number+number2);
        System.out.println(number3+number4);
        System.out.println(number-number2);
        System.out.println(number3-number4);
        System.out.println(number * number2);
        System.out.println(number3 * number4);
        System.out.println(number/number2);
        System.out.println(number3/number4);
        System.out.println(number4 % number3);
        System.out.println(number2 % number);
    }
}

Math.main(null);
7
7.0
-3
-3.0
10
10.0
0
0.4
1.0
1

Assignment operators (1.4)

  • += adds value of a variabe to another variable and assigns total value to first variable
  • -= subtracts value of a variabe to another variable and assigns total value to first variable
  • *= multiplies value of a variabe to another variable and assigns total value to first variable
  • /= multiplies value of a variabe to another variable and assigns total value to first variable
  • %= takes the remainder of a variable with a second variable and assigns remainder to first variable
  • ++ increments a variable by 1, to incrememt by more change second plus to number which you want to incrememnt by
  • -- subracts a variable by 1, to incrememt by more change second plus to number which you want to subtract by

Casting and Ranges (1.5)

  • Doubles and Integers can be converted to each other using (int) or (double)
    • When converting from doubles to integers, will round down
  • Integers are 4 bytes of data, can store between Integer.MAX_VALUE and Integer.MIN_VALUE
public class Cast {

    public static void main(String[] args) {
        double num = 10.5;
        int num2 = 100;
        int numInt = (int)num;
        double num2Double = (double)num2;

        System.out.println(num);
        System.out.println(num2);
        System.out.println(numInt);
        System.out.println(num2Double);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);


    }
}

Cast.main(null);
10.5
100
10
100.0
2147483647
-2147483648

Code Example

public class Main {
    public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("What is your name?");
      String name = sc.next(); //string
      System.out.println(name);
      System.out.println("How many pizzas do you want to buy?");
      int pizzas = sc.nextInt(); //integer
      System.out.println(pizzas);
      System.out.println("Do you have the discount (true/false)?");
      boolean hasDiscount = sc.nextBoolean(); //boolean
      System.out.println(hasDiscount);
  
      double price; //double, defaults to 0
      if (hasDiscount) {
        price = 1.20;
      } else {
        price = 2.10;
      }
  
      char firstChar = name.charAt(0); //character
      double finalPrice = price * pizzas * 1.08; // adding taxes
  
      System.out.println("Hi " + firstChar + "! You have to pay " + (finalPrice) + " dollars.");
    }
  }
  
  Main.main(null);
What is your name?
Kinish
How many pizzas do you want to buy?
6
Do you have the discount (true/false)?
true
Hi K! You have to pay 7.776 dollars.