Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables & Data Types Java.

Similar presentations


Presentation on theme: "Variables & Data Types Java."— Presentation transcript:

1 Variables & Data Types Java

2 Variables Variables – a cup … a container…. It holds something… some type of data When we declare a variable in Java, we must tell our program what type of data will be stored in that variable It’s no longer OK to just say the variable is a ‘number’ – we must specify WHAT kind of number Always be aware of the type of data your program is processing Variables can change values throughout a program

3 Primitive Data Types Data types – what KIND of data is stored in the variable There are 8 primitive data types (built-in types), but we mostly use: Int – integers (whole numbers… no decimal points) Double – numbers that contain decimal points. Called floating-point numbers Boolean – true or false String – sequence of characters

4 Integers Int x = 7; This line is considered a declaration
It declares the name of a variable (x) – also called an identifier It declares the data type of x – it’s an int The equal assign is an assignment operator Java is a strongly-type language – must declare data type

5 Integers Can hold values between -2147483648 and 2147483648
You can use standard arithmetic operations for addition (+), subtraction (-), multiplication (*), division (/), and the remainder of division (%)

6 Addition/Subtraction
Public class AddNumbers { public static void main (String[] args) int x = 7; int y = 6; int z = x +y; System.out.println(z); }

7 Division Public class DivideNumbers {
public static void main (String[] args) int weeklyPay = 800; int hoursWorked = 40; int hourlyRate = weeklyPay/hoursWorked; System.out.println(hourlyRate); }

8 Multiplication int weeklyRate = 20; int hoursWorked = 40; int WeeklyPay = hourlyRate*hoursWorked; System.out.println(WeeklyPay);

9 Modulus (remainder Int x = 7; Int y = 3; Int z = x % y; // z will be 1 System.out.println(z);

10 Floating Point Numbers
Numbers that contain decimal places Double and Float Precision – how accurate do you need your data to be? How many decimal places do you need? Double is the generally used data type for decimal places

11 Double Double hourlyRate = 15.75; Double hoursWorked = 38.25; Double totalPay = hourlyRate * hoursWorked; // will display //this won’t compile Int totalPay = hourlyRate * hoursWorked;

12 Double - continued // this won’t compile Double hourlyRate = 15.75;
Int hoursWorked = 38.25; Double totalPay = hourlyRate * hoursWorked; System.out.println (totalPay) // this will compile Int hoursWorked = 38;

13 Classroom Learning Create a Java program named Pay that calculates a worker’s net pay after taxes. Establish appropriate variables for: A person’s hourly rate The number of hours worked The person’s tax rate The gross pay The amount of taxes taken off The net pay

14 Payroll - continued Assign the following values to the variables:
The hourly rate should be 9.25 per hour The number of hours worked should be 32 The person’s tax rate is 18% The gross pay should calculate hourly rate * number of hours worked Taxes should be gross pay * tax rate Net pay should be gross pay – taxes At the end of the program display “Your net pay is _________ Thank you!!”

15 Getting input from UserScanner Class
import java.util.Scanner; [at top of program] [after public static void …..] Scanner response = new Scanner(System.in); Int testVariable; [putting input from scanner into a variable] System.out.print (“what is 1st number?”); testVariable = response.nextInt()

16 Boolean Boolean isMale = true; Boolean isFemale = false;
Has just two values – true or false Used with conditional/decision-making logic

17 Strings Sequence of characters Needs to be stored in quotes
String firstName = “Chet”; Data type upper case “S”

18 Changing Variable Valeus
int x = 7; int y = 6; X = 5; int z = x +y; //z is 11 //will get compiler errors Int x = 7; Int y = 6; Int x = 5; Int z = x + y;

19 Concatenation Used to combine two strings or a number and a string
String firstName = “Chet”; String lastName = “Brown”; System.out.println (firstName + lastName); //will display ChetBrown System.out.println (firstName + “ “ + lastName); // will display Chet Brown

20 Concatenation – cont’d
String firstName = “Chet”; Int age = 25; System.out.println(FirstName + “ is” + age + “years old”); //will display Chet is 25 years old


Download ppt "Variables & Data Types Java."

Similar presentations


Ads by Google