Вы находитесь на странице: 1из 22

Understan

ding Data
Types

CC102 - FUNDAMENTALS OF PROGRAMMING


LESSON X – LOREM IPSUM

LEARNING OUTCOMES:
At the end of the session, the students should be able to:
1. Define appropriate data type for different
programming scenario
2. Can identify the different keyword categorically
3. Develop code that declares, initializes, and uses
primitives, arrays, enums, and objects as static,
instance, and local variables.
3. Write code that determines the equality of two
objects or two primitives.

CC102 - FUNDAMENTALS OF PROGRAMMING 2


LESSON III – Understanding Data Types

Data Related Concepts


• Understanding Variables, Data Types,
and Operators
• Naming the Variables: Legal Identifiers
• Reserved Names: The Keywords

CC102 - FUNDAMENTALS OF PROGRAMMING 3


LESSON III – Understanding Data Types
Understanding
Variables, Data Types,
and Operators
• 2 kinds of data types:
primitive and non-primitive
• 2 corresponding kinds of variables:
primitive variables,
and reference variables –
object references

CC102 - FUNDAMENTALS OF PROGRAMMING 4


LESSON III – Understanding Data Types

Working with primitive


data types
v

CC102 - FUNDAMENTALS OF PROGRAMMING 5


LESSON III – Understanding Data Types

Working with primitive


data types
Category Types Precision Example

byte From +127 to -128 byte b = 65;


char c = 'A';
char All Unicode characters
char c = 65;
short From +32,767 to -32,768 short s = 65;
Integer From +2,147,483,647 to
int int i = 65;
-2,147,483,648
From
long +9,223,372,036,854,775,807 to long l = 65L;
-9,223,372,036,854,775,808
CC102 - FUNDAMENTALS OF PROGRAMMING 6
LESSON III – Understanding Data Types

Working with primitive


dataTypes
Category types
Precision Example
From 3.402,823,5 E+38 to
float float f = 65f;
1.4 E-45
Floating-
point From
double d =
double 1.797,693,134,862,315,7
65.55;
E+308 to 4.9 E-324

boolean b =
boolean false, true
true;
Other
void -- --

CC102 - FUNDAMENTALS OF PROGRAMMING 7


LESSON III – Understanding Data Types

Syntax in declaring
Variable
• The general syntax for declaring and initializing
a variable:
<modifier> <dataType>
<variableName> (assignment Op)
<initial/literal Value>;

private int id = 10;


int id;

CC102 - FUNDAMENTALS OF PROGRAMMING 8


LESSON III – Understanding Data Types

Accessing Variables
• Once you declare a variable, you can access it
by referring to it by its name:
x = y;

Variables can be classified into three categories:


• Local variables
• Instance variables
• Static variables

CC102 - FUNDAMENTALS OF PROGRAMMING 9


LESSON III – Understanding Data Types

Literals
• A literal is a value assigned to a variable in the
source code
int id = 10;
The boolean Literals: true or false
The char Literals: ‘L’, ‘\u4567’, ‘\n’
The Integral Literals: 43, 43L, 053, 0x2b
The Floating-Point Literals: 12.33, 1.25E+8, 1.2534f

CC102 - FUNDAMENTALS OF PROGRAMMING 10


LESSON III – Understanding Data Types

Initial and default


values

CC102 - FUNDAMENTALS OF PROGRAMMING 11


LESSON III – Understanding Data Types

Sample
program

CC102 - FUNDAMENTALS OF PROGRAMMING 12


LESSON III – Understanding Data Types

Working with non


primitive data types
• All non-primitive data types in Java are objects
• You create an object by instantiating a class
• When you declare a variable of a non-primitive
data type, you actually declare a variable that
is a reference - reference variable / object
• reference - to the memory where an object
lives
Objects
Arrays – will be discussed on final term
The Data Type enum – for intermediate programming

CC102 - FUNDAMENTALS OF PROGRAMMING 13


LESSON III – Understanding Data Types

Working with non


primitive data types
• An object reference (a reference variable) is
declared:
Student studentOne;
• You create an object with the new operator
studentOne = new Student();

• The declaration of the object reference


variable, object creation, and initialization of
the reference variable:
Student studentOne = new Student();

CC102 - FUNDAMENTALS OF PROGRAMMING 14


LESSON III – Understanding Data Types

Using Assignment
Operator
• used to set
(or reset)
the value of
a variable:
x = 7;

• shortcut
assignment
operators:

CC102 - FUNDAMENTALS OF PROGRAMMING 15


LESSON III – Understanding Data Types

Advance Operators used


by Java

CC102 - FUNDAMENTALS OF PROGRAMMING 16


LESSON III – Understanding Data Types

Advance Operators used


by Java
• Shorcut of if-else operator ?:

CC102 - FUNDAMENTALS OF PROGRAMMING 17


LESSON III – Understanding Data Types

Advance Operators used


by Java
• CAST Operator
• The cast operator: (<type>) explicitly converts
a value to the specified type
byte z = (byte) (x/y);
• The new operator: instantiate a class and to
create an array
• The instanceof operator: determines if a
given object is of the type of a specific class
<op1> instanceof <op2>

CC102 - FUNDAMENTALS OF PROGRAMMING 18


LESSON III – Understanding Data Types

Equality of Two Objects


or two Primitives
• Three kinds of elements that can be compared to
test the equality:
1.Primitive variables:
• hold the same value
• can be tested with the == operator
2. Reference variables:
• can be compared by using the == operator
• hold the same value
3. Objects:
• tested with the equals() method of the Object class

CC102 - FUNDAMENTALS OF PROGRAMMING 19


Variables,
Identifiers
and
Keywords

CC101 - INTRODUCTION TO COMPUTING


LESSON III – Variables Identifiers and Keywords

Naming Variables
1. Each variable has a name, called an identifier.
2. Rules to name a variable:
• The first character must be a letter, a dollar sign ($),
or an underscore (_).
• A character other than the first character in an
identifier may be a letter, a dollar sign, an underscore,
or a digit.
• None of the Java language keywords (or reserved
words) can be used as identifiers.
3. Maximum character length for an identifier: Unlimited
Java SE 10 Edition

CC102 - FUNDAMENTALS OF PROGRAMMING 21


LESSON III – Variables Identifiers and Keywords

JAVA Keywords

CC102 - FUNDAMENTALS OF PROGRAMMING 22

Вам также может понравиться