KEMBAR78
Chapter 2 - Data Types and Variables | PDF | Data Type | Variable (Computer Science)
0% found this document useful (0 votes)
47 views12 pages

Chapter 2 - Data Types and Variables

The document discusses Java data types and variables. It defines primitive types like byte, short, int, long, float, double, char, boolean and reference types. It also describes identifiers, variables, declaring variables, scope and lifetime of variables, type conversion and casting, and Java's automatic conversions between data types.

Uploaded by

J A Y T R O N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views12 pages

Chapter 2 - Data Types and Variables

The document discusses Java data types and variables. It defines primitive types like byte, short, int, long, float, double, char, boolean and reference types. It also describes identifiers, variables, declaring variables, scope and lifetime of variables, type conversion and casting, and Java's automatic conversions between data types.

Uploaded by

J A Y T R O N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

z

Data Types and


Variables
2

z
Data Types

 Data Type simply means the type of data. It tells the


compiler or interpreter how the programmer intends
to use the data.
 Every data has a type and every type is strictly
defined.
 Java’s types are divided into primitive types and
reference types.
Yeboah Adjei-Twum
3

z
Primitive Types

Type Bytes Range


byte 1 -128, 127
short 2 -32768, 32767
int 4 -2B, 2B
Long 8
float 4
double 8
char 2 0 – 65,536
boolean 1
Yeboah Adjei-Twum
4

z
Identifiers

 Identifiers are the names that identify the elements such


as classes, methods, and variables in a program.
 An identifier is a sequence of characters that consists of
letters, digits, underscores (_), and dollar signs ($).
 An identifier must start with a letter, an underscore (_),
or a dollar sign ($). It cannot start with a digit.
 An identifier cannot be a reserved word.
Yeboah Adjei-Twum
5

z
Variables
 A variable is the basic unit of storage in a Java
program.
 is a location in the computer’s memory where a value
can be stored for use later in a program.
 All variables must be declared with a name and a
type before they can be used.
 A variable’s name enables the program to access the
variable’s value in memory.
Yeboah Adjei-Twum
6

z
Declaring a Variable

 Type identifier [ = value ];


Yeboah Adjei-Twum
7

z
The Scope and Lifetime of Variables

 Local Variables
 Variables declared within a block or a method.

 Variables declared inside a scope are not visible


to code outside that scope.

 Global Variables
Yeboah Adjei-Twum
8

z
Type Conversion and Casting

 Type conversion is assigning a value of one type to a


variable of another type.
 Example; it is possible to assign an int value to a long
variable.
 However, not all types are compatible, and thus, not
all type conversions are implicitly allowed.
 Casting allows you to perform explicit conversion
Yeboah Adjei-Twum

between incompatible types.


9

z
Java’s Automatic Conversions

 When one type of data is assigned to another type of


variable, an automatic type conversion will take place
if the following two conditions are met:
 The two types are compatible
 The destination type is larger than the source type.

 Example, the int type is always large enough to hold


all valid byte and short values, so no explicit cast
statement is required.
Yeboah Adjei-Twum
10

z
Java’s Automatic Conversions

 For widening conversions, the numeric types,


including integer and floating point types, are
compatible with each other.
 However, there are no automatic conversions from
the numeric types to char or boolean.
Yeboah Adjei-Twum
11

z
Scenario 1

 A program that finds that adds two numbers.


 Define all the ‘identifiers’ (Choosing appropriate identifiers)
Yeboah Adjei-Twum
12

z Scenario 2
 A customer of a bank made a deposit of GHC 500 on 14/09/2019.
The details of the customer is as follows:
 Name: Abigail Ayebeng
 Balance before the deposit: GHC 1200.
 Write a program that displays:
 The name of the customer
 The date of the last deposit
 The current balance of the customer

(Note first assign the values to variables and then during printing, call the
variables instead of the values themselves. Choose appropriate identifiers)
Yeboah Adjei-Twum

You might also like