Tankertanker Tankertanker
Design Design
3.0 Java Language
3.2 Data Type, Operator & Expression
Tankertanker
Design
3.2 Data Type
Tankertanker
Design
Learning Outcomes:
b) Identify various primitive data types –
(int, double, boolean and char)
Tankertanker
Design
3.2 Primitive Data Type
Tankertanker
•Design There are many different types of data.
• Variables are classified according to their data type,
which determines the kind of data that may be
stored in them.
• Data types in Java includes:
• - int
• - double
• - boolean
• - char
Tankertanker
Design
3.2 Primitive Data Type
Tankertanker
Design
• Variables can hold different types of data.
• Common data types for variables:
Data Explanation example
type Integers in the range −231 to 231 int x =5;
int
double Floating point numbers with 15 digits of double x=1.7
bool accuracy Answer=true
char code=‘A’
Variable with a value of either true or
false.
Variable in single character
Tankertanker
Design
3.2 Primitive Data Type
Tankertanker integer, int
Design
• Used to declare numeric program variable of integer type.
• Range of data can be assign to int data type:
( -2147483648 to +2147483647)
• Positive integers do not require a + sign in front of them.
• No commas are used within an integer. In Java, commas
are used to separate items in a list. Thus, 36,782 is
interpreted as two integers: 36 and 782
Tankertanker
Design
3.2 Primitive Data Type
Tankertanker integer, int
Design
• Examples of variable declaration:
i. int max_number;
ii. int counter;
• Assigning value to int variable into a memory location.
• Examples:
i. int max_number=2000;
ii. int counter=1;
Tankertanker
Design
3.2 Primitive Data Type
Tankertanker double floating-point, double
Design
• Used to declare a number with a decimal point or
with fractional values.
• It can store a floating-pointing number with :
15 digits of accuracy.
• Examples of variable declaration:
i. double taxRate;
ii. double pi;
Tankertanker
Design
3.2 Data Type
Tankertanker double floating-point, double
Design
• Assigning value to double variable into a memory
location.
• Examples:
i. double taxRate=1.2345678901;
ii. double pi=3.142;
Tankertanker
Design
3.2 Data Type
Tankertanker character, char
Design
• Used to declare character variables.
• May hold only a single characters – that is letters,
digits and special symbols.
• Examples of variable declaration:
i. char grade;
ii. char code;
Tankertanker
Design
3.2 Data Type
Tankertanker character, char
Design
• Assigning value to char variable into a memory
location, need to place single quote marks ( ‘ ’ )
around the value as in:
i. grade = ‘A’;
ii. code = ‘+’;
Tankertanker
Design
3.2 Data Type
Tankertanker boolean, bool
Design
• Used to declare a Boolean literals: true and
false.
• Examples of variable declaration:
i. bool gender;
ii. bool choice;
Tankertanker
Design
3.2 Data Type
Tankertanker boolean, bool
Design
• Bool data type can store two value only 0 or 1.
0 for False & 1 For True.
• if define Bool variable and that variable contain
value more than 1 it store only 1.
Example of variable declaration:
i. bool num= 1212313; // it store num = 1 .
(It shows zero when its False or value is Zero only.)
ii. bool choice;
(the variable choice now can be assigned with value
true or false)
Tankertanker
Design
3.2 Data Type
Tankertanker :The bool type takes one byte and stores a
boolDesign
value of true (1) or false(0).
public static void main (String [] args)
{
boolean legalAge;
int age;
legalAge = (age >= 21);
This statement:
legalAge = (age >= 21);
Assigns the value true to legalAge if the value of age is greater than or equal to 21.
This statement assigns the value false to legalAge if the value of age is less than 21
Tankertanker
Design
3.2 Data Type
Tankertanker
•Design Declare the possible data type for each variables in
the following statement:
Problem 1 :
“Find the average of three numbers input by user .”
Variables: ____________
Data type: ____________
Problem 2:
“Calculate the area of a rectangle”
Tankertanker
Design
Practice!
Tankertanker
Design
Tankertanker
Design
Practice!
Tankertanker
Design
What is the output of the following program lines.
int num;
num=(1/3) * 3;
System.out.print (“(1/3) * 3 is equal to ” +num) ;
Tankertanker
Design
Practice!
TankertanIkderentify data type.
Design
1. 5
2. ‘9’
3. True
4. 0.00532
5. 66.0
6. -7
7. ‘L’
Tankertanker Tankertanker
Design Design
3.0 Java Language
To be continue