0% found this document useful (0 votes)
18 views28 pages

04 CS1 Unit 3 - Lesson 1

This document covers data types and variables in Java, detailing primitive types (boolean, char, byte, short, int, long, float, double) and non-primitive types (Strings, Classes, Arrays). It explains the purpose and characteristics of each data type, as well as the concept of variables as named memory locations that can hold changing values. Additionally, it outlines the rules for naming variables and includes an assessment task related to variable declarations.
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)
18 views28 pages

04 CS1 Unit 3 - Lesson 1

This document covers data types and variables in Java, detailing primitive types (boolean, char, byte, short, int, long, float, double) and non-primitive types (Strings, Classes, Arrays). It explains the purpose and characteristics of each data type, as well as the concept of variables as named memory locations that can hold changing values. Additionally, it outlines the rules for naming variables and includes an assessment task related to variable declarations.
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/ 28

CS 1 – Programming

Logic Formulation
Unit 3: Data Types, Structures, and Decisions
Lesson 1: Data types and Variables
Learning Outcome

Differentiated data structures that can


be used when writing algorithmic
solutions to problems.
Data Types
A data type is a set of values. Data types specify the
different sizes and values that can be stored in the
variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types
include boolean, char, byte, short, int, long, float and
double.

2. Non-primitive data types: The non-primitive data


types include Strings, Classes, and Arrays.
DATA TYPE
Program data can be classified according to their
types:
PRIMITIVE NON-PRIMITIVE
• Boolean • Long • String
• Byte • Float • Arrays
• Char • Double • Classes
• Short
• Int
Boolean
•Boolean data type represents one bit of
information
•There are only two possible values: true and
false
•This data type is used for simple flags that track
true/false conditions
Byte
• Byte data type is an 8-bit signed two's complement
integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays,
mainly in place of integers, since a byte is four times
smaller than an integer.
Char
•char data type is a single 16-bit Unicode
character
•Minimum value is '\u0000' (or 0)
•Maximum value is '\uffff' (or 65,535 inclusive)
•Char data type is used to store any character
String
• This data type can hold a series of characters or a
string of characters.

Example: “Hello”, “Jason”, “143.44”;


Short
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte
data type. A short is 2 times smaller than an integer

Example: short s = 10000, short r = -20000;


Int
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral
values unless there is a concern about memory.

Example: int a = 100000, int b = -200000;


Long
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -
1)
• This type is used when a wider range than int is needed
• Default value is 0L

Example: long a = 100,000,000,000;


Float
• Float data type is a single-precision 32-bit IEEE 754 floating
point
• Float is mainly used to save memory in large arrays of floating
point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency

Example: float f1 = 234.5f;


Double
• double data type is a double-precision 64-bit IEEE 754 floating
point
• This data type is generally used as the default data type for
decimal values, generally the default choice
• Double data type should never be used for precise values such
as currency

Example: double d1 = 123.4;


Variables
Variables
A variable is a container which holds the
value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location. A
variable is the name of a reserved area
allocated in memory. In other words, it is a
name of the memory location. It is a
combination of "vary + able" which means its
value can be changed.
Variables (Cont.)
Variables are named memory locations whose contents
can vary or differ over time.
For example, in the number-doubling program in,
myNumber and myAnswer are variables. At any moment in
time, a variable holds just one value. Sometimes,
myNumber holds 2 and myAnswer holds 4; at other times,
myNumber holds 6 and myAnswer holds 12. The ability of
variables to change in value is what makes computers and
programming worthwhile.
Variables (Cont.)
In most programming languages, before you can use any variable,
you must include a declaration for it. A declaration is a
statement that provides a data type and an identifier for a
variable. An identifier is a variable’s name. A data item’s data
type is a classification that describes the following:

• What values can be held by the item


• How the item is stored in computer memory
• What operations can be performed on the data item
Variable Declaration
When we declare a variable, we provide both a data type
and an identifier. Optionally, you can declare a starting
value for any variable. Declaring a starting value is known
as initializing the variable. For example, each of the
following statements is a valid declaration. Two of the
statements include initializations, and two do not:
num mySalary
num yourSalary = 14.55
string myName
string yourName = "Juanita"
int myNumber
int myAnswer

int myNumber
int myAnswer

Flowchart and pseudocode of number-doubling program with variable declarations


Naming Variables
Every computer programming language has its own set of
rules for creating identifiers or variable names. Most
languages allow letters and digits within variable names.
Some languages allow hyphens in variable names, such as
hourly-wage, and some allow underscores, as in
hourly_wage. Other languages allow neither. Some
languages allow dollar signs or other special characters in
variable names (for example, hourly$); others allow
foreign-alphabet characters, such as π or Ω.
Common Rules in Naming Variables
1. Variable names must be one word. The name
can contain letters, digits, hyphens, underscores,
or any other characters you choose, with the
exception of spaces. Therefore, r is a legal variable
name, as are rate and interestRate. The variable
name interest rate is not allowed because of the
space.
Common Rules in Naming Variables
(Cont.)
2. Variable names should have some
appropriate meaning. This is not a formal rule
of any programming language. When computing
an interest rate in a program, the computer does
not care if you call the variable g, u84, or fred. As
long as the correct numeric result is placed in the
variable, its actual name doesn’t really matter.
Common Rules in Naming Variables
(Cont.)
However, it is much easier to follow the logic
of a statement like:
set finalBalance = initialInvestment * interestRate
than a statement like:
set f = i * r, or set someBanana = j89 * myFriendLinda
Assessment Task

1. Create a flowchart and pseudocode of a


signing up process with variable declarations.
The following data items are required:
1) Full name
2) Contact Number
3) Email
4) Password
Example of
Sign
up/login
process
End
Up next...
Unit 3: Data Types, Structures, and Decisions
Lesson 2: Unstructured Spaghetti Code, Three Basic Structures

You might also like