1 of 29

Data Types

& Arithmetic Operations

Week 2

1

Helping Hand for Relief & Development

2 of 29

Goal of this lesson

  • In this lesson, we will learn:
    • About the primitive data types in Java
    • How to make our own variables in Java
    • How to assign and change data in our variables
    • How to write print statements with variables
    • How to perform simple arithmetic operations like addition, subtraction, multiplication, and division in Java

2

3 of 29

Key Terms

  • Data type
  • Primitive type
  • Variable
  • String
  • int
  • double
  • boolean

3

We will learn about these terms in the next slides

4 of 29

What is a data type?

  • A data type tells your computer what kind of information you want to store
  • Numbers - 1, 2.0, 5, 1034
  • Letters/Characters - A, b, Z, #, @
  • Words - hello, goodbye, ayesha
  • Sentences - nice to meet you!

4

Primitive

Non-primitive

Data types

Numbers

Letters

Characters

Words

Sentences

5 of 29

Quick Review

  • Before we get into data types in Java, let us quickly review:
  • Integer - whole numbers without decimals like 1, 2, 3, -1, -2, -3
    • Examples: 5, 3029, 490
  • Real Number - any non imaginary number (includes decimals)
    • Examples: 320.43, π, e
  • Binary - a base two number. In computer science it is either a 0 or 1

5

6 of 29

Data Types

  • In Java, there are 8 primitive data types that allow us to store:
    • Integers like 3, 5, 18 - byte, short, int, long
    • Decimals like 4.2, 15.3 - float, double
    • True or false - boolean
    • Characters like ‘A’, ‘k’, ‘#’, ‘?’ - char
  • There are also many non-primitive data types. Today we will only look at String that lets us store words and sentences

6

7 of 29

Why we need data types

awesome + cool

1 + 2

Example

  • Here, we define the variable sum as the integer 0

7

8 of 29

What is a variable?

  • In order to create data types that store either numbers, characters, or strings, we need to have a name for the data.
  • Variable: A piece of information that has a name, data type, and value

8

“Daanish”

name

0

sum

9 of 29

Most Important Data Types

  • char - a character from a to z
    • char letter = ‘m’;
    • This sets the variable letter to the value m
  • String - any arrangement of characters
    • String test = “hello”;
    • This sets the variable test to the value of hello
  • boolean - 1 or 0/True or False
    • boolean isOn = false;
    • The variable isOn is set to false

9

10 of 29

Most Important Data Types

  • byte - an integer between -27 and 27-1 (1 byte)
    • byte num = 5;
  • short- an integer between -215 and 215-1 (2 bytes)
    • short num = 4398;
  • int- an integer between -231 and 231-1 (4 bytes)
    • int num = 54892;
  • long - an integer between -263 and 263-1 (8 bytes)
    • long num = 298483;

10

11 of 29

Most Important Data Types

  • float - any number from -231 and 231-1 (4 bytes)
    • float num = 2090.453
  • double - any number from -263 and 263-1 (8 bytes)
    • double num = 320958434.65

11

12 of 29

Why It’s Important

  • Important things to note:
    • String and char use letters
    • boolean helps with simple binary variables
    • float and double use real numbers
    • byte, short, int, and long deal with integers

12

13 of 29

Variable Declaration

  • Variable declaration
    • [data type] [variable name] = [value];
    • The computer will put aside memory for the variable so it can reference the variable later in the program
  • Example:
    • int age = 17;
    • String name = “Daanish”;
  • Create two variables: one that stores your age and one for your name

13

14 of 29

Printing Variables

  • Assuming I declared my variable greeting to be “hello”, I can print out the variable
  • System.out.print(greeting); or System.out.println(greeting);
    • The ln will put the phrase on a new line in the console
  • To combine text and a variable, use the following syntax:
  • System.out.print(“The variable greeting: ” + greeting);
    • The console should read out “The variable greeting: hello”
  • Try printing some things on your own!

14

15 of 29

Constant Declaration

  • Constant declaration
    • final [data type] [variable name] = [value];
    • Including the word final lets the computer know the variable will never change in the program
  • Example:
    • final byte num = 2;
    • final char char1= ‘d’;
  • In what cases would you use a constant or a variable declaration?

15

16 of 29

Casting

  • Widening Casting
    • Converting a small type to a bigger type, such as a byte to a double
      • int intNum = 5;�double doubleNum = intNum; //doubleNum is 5.0
  • Narrowing Casting
    • Converting a large type to a smaller type
      • double doubleNum = 5.2;�int intNum = (int) doubleNum; //intNum is 5

16

17 of 29

CW Exercise 1

  • Explain the difference between 2, 2.0, “2”, and “2.0”
  • Cast an integer to a double, and a double to an integer. What happens?
  • Create a program that says “hello my name is [insert name] I am [insert age] years old and I want to study [insert field] when I get older”
    • Try doing this using variables, not just typing the phrase out into the System.out.print();

17

18 of 29

Arithmetic Operations

  • The 5 basic operations:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Modulo (%)

18

19 of 29

Full Order of Operations

19

20 of 29

Short Order of Operations

(1) () (parentheses)

(2) *, /, % (multiplication, division, modulo)

(3) +, - (addition, subtraction)

20

21 of 29

Order of Operations - Key Points

  • Key things to point out:
    • Anything in parentheses is calculated first
      • 2 * (3 + 4) = 14
    • Multiplication, division, and remainder are one one level above addition and subtraction
      • 2 * 3 + 4 = 10, and 2 + 3 * 4 = 14
  • You can always go back to the previous slide if necessary when coding in the future

21

22 of 29

CW Exercise 2

  • Using the following:
  • Output the following in the console

22

23 of 29

CW Exercise 3

  • What will the following code produce? Make predictions for all the values

23

Use a calculator if necessary

24 of 29

Key Terms

  • Data type
  • Primitive type
  • Variable
  • String
  • int
  • double
  • boolean

24

Define these terms in your own words, we will go over them in a minute

25 of 29

Key Terms

  • Data type - Tells the computer what kind of info you want to store
  • Primitive type - Simple data type (like integer, double, boolean)
  • Variable - A piece of memory that has a data type and a value
  • String - A data type that is a sequence of characters
  • int - An integer data type that stores integers (ie. -3, -2, -1, 0, 1, 2..)
  • double - A data type that stores real numbers (ie. -3.2, 4.5, 110)
  • boolean - A data type that is either true or false

25

26 of 29

What We Learned

  • In this lesson, we learned:
    • How to create our own variables in Java
    • How to print different kinds of output
    • About the primitive data types in Java
    • How to perform addition, subtraction, multiplication, and division in Java

26

27 of 29

Homework

  • Here is the link to the homework.
  • Please email your java file by Thursday, Feb 4th to hhrd.compsci@gmail.com

27

28 of 29

Extra Reading

28

29 of 29

Questions?

29

Feel free to ask!