Download presentation
Presentation is loading. Please wait.
Published byPauline Bell Modified over 8 years ago
1
Copyright © Texas Education Agency, 20131 Computer Programming Variables and Data Types
2
Using variables in everyday life Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]2
3
Think of something whose contents may be unknown, or may change, such as… Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]3 Your wallet Your locker Your backpack The term ‘variable’ means that it can change. Typically we think of the value or the contents changing.
4
We can refer to things that we don’t know the exact value of Sally is two years older than Mike ( S = M +2) Return to campus two minutes before lunch is over ( C = L – 2) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]4
5
Using variables in Mathematics We can represent some unknown quantity with a variable. We can work equations without knowing the value of the variable. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]5
6
We use variables to represent an unknown quantity R stands for ‘radius’ -- the circumference of a circle is 2 x PI x R The slope-intercept formula for a line is Y = mX + b, where m is the slope of a line, b is the y axis intercept. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]6
7
Variables in Programming Variables are named containers that hold values. The value of a variable can be changed. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]7
8
Variables allow us… To refer to something we do not know the value of To be more flexible in programming To write code that is more understandable Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]8
9
A variable’s name… Refers to a location in memory that contains the actual value Cannot start with a number Cannot change Cannot be re-declared (used again for a different variable) in the same portion of the program Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]9
10
Consider the Java statement below int keys = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]10
11
In Java, the name of the variable must be declared before it can be used. int keys = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]11
12
The data type of the variable is also part of the declaration. int keys = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]12
13
The statement that gives the data type and name of the variable is known as the declaration. int keys = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]13
14
The value of the variable can be assigned in the declaration. int keys = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]14
15
The value of the variable can also assigned after the declaration. int keys; keys = 88; When the variable is given a value for the first time, this is known as initialization. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]15
16
Notice that the data type of the variable is only declared one time. int keys; keys = 88; keys = 942; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]16
17
The value of the most variables can be changed after the declaration. int keys = 88; keys = 279; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]17
18
The value of a variable that is declared as ‘final’ can not be changed after the declaration. This is known as a constant. final int KEYS = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]18
19
It is a programming convention (accepted practice) to capitalize the name of constants. final int KEYS = 88; This lets programmers know that this variable is a constant, without having to look through the code to find it’s declaration. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]19
20
The value of a constant must be given in the declaration statement. final int KEYS = 88; final int KEYS; KEYS = 88; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]20
21
Several variables of the same data type can be declared in the same statement. int qty = 7, size = 4, age; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]21
22
What will be printed in the code below? int x = 7;// The value of x is 7 x = 5;// x changes to 5 System.out.println(x);// print the value of x Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]22
23
How many different kinds of data types are there? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]23
24
There are 2 main types Intrinsic (primitive) data types Reference (class) data types Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]24
25
Intrinsic (also called ‘primitive’) data types have the following characteristics They are a part of the basic Java language. There are only 8 different ones. They use a specific amount of memory space. They start with a lower case letter (Boolen, int, float, long). Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]25
26
Primitive (intrinsic) data types Boolean: can only be true or false char: can only be one character byte: can hold an integer from -128 to 127 short: can hold an integer from -32768 to 32767 int: can hold an integer from -2 million to 2 million long: can hold an integer from -9 quintillion to 9 quintillion float: can hold a decimal number +/- 1.4e-45 to +/- 3.4e38 double can hold a decimal number +/- 4.9e-324 to 1.7 e308 Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]26
27
Which primitive should you use? One that matches the data type (don’t use an ‘int’ if you want to store a decimal number) One that conserves memory (don’t use a ‘long’ if you want to store an integer that is less than 100) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]27
28
Examples: Boolean isHungry = true; double interest = 0.67; char symbol = ‘#’; //Notice the quotes. byte num = 3; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]28
29
Reference data types ( also called ‘class’ data types) Are not part of the basic Java language Have been created by Java programmers By convention, the name of the data type starts with a capital letter Can be made up of complex data Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]29
30
Example String name = “Jeremiah Johnson”; Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]30
31
Data Type Analogy Intrinsic data types are part of the Java language, in the same way that words are part of the English language Reference data types are not part of the basic Java language, in the same way that specifics documents or books are not part of the basic English language, but are constructed from it Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]31
32
The End We will learn more about Reference Data Types when we study classes. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [Variables and Data Types]32
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.