0% found this document useful (0 votes)
22 views

Com01 PPT Variables

This document discusses variables in computer programming. It defines variables as values that can change depending on conditions or information passed to the program. It describes different types of variables such as constants, global variables, and local variables. It also covers different data types for variables including primitive types like int and boolean, as well as non-primitive types. Modifiers that can be added to variables are also outlined, including access modifiers like private and public, and non-access modifiers like static and final.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Com01 PPT Variables

This document discusses variables in computer programming. It defines variables as values that can change depending on conditions or information passed to the program. It describes different types of variables such as constants, global variables, and local variables. It also covers different data types for variables including primitive types like int and boolean, as well as non-primitive types. Modifiers that can be added to variables are also outlined, including access modifiers like private and public, and non-access modifiers like static and final.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming

Variables
COMPUTER PROGRAMMING <COM01>
Variable
a value that can change, depending
on conditions or on information
passed to the program

runtime storage of data.


Types of
Variables
• Constants
• Global variables
• Class variables
• Instance variables
• Local variables.
Data Types
● determines the values that the
variable can have and the
operations that can be
performed on it
Data Types
Groups
● Primitive Data Types - includes
byte, short, int, long, float,
double, boolean and char
● Non-primitive data types - such
as String, Arrays and Classes
(you will learn more about these
in a later chapter)
Primitive
Data Types
● A type with specified size and
type of variable values, and it has
no additional methods.
Primitive Data Types
Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values


Non-Primitive
Data Types
● Also called as reference
types because they refer to
objects.
Primitive vs.
Non-Primitive
• Primitive types are predefined (already
defined) in Java. Non-primitive types are
created by the programmer and is not
defined by Java (except for String).
• Non-primitive types can be used to call
methods to perform certain operations,
while primitive types cannot.
• A primitive type has always a value, while
non-primitive types can be null.
• A primitive type starts with a lowercase
letter, while non-primitive types starts
with an uppercase letter.
Modifiers
● keywords that you add to
those definitions to change
their meanings.
● It can be Access and Non-
Access Modifier
Access
Modifier
● Private - Visible to the class
only
● Public – Visible globally
● Protected - Visible to the
package and all subclasses.
Non-Access
Modifiers
● “static" keyword is used to
create variables that will
exist independently of any
instances created for the
class. Only one copy of the
static variable exists
regardless of the number of
instances of the class.
Non-Access
Modifiers
● “final” keyword is used to
create variable can be
explicitly initialized only
once
Non-Access
Modifiers
● “abstract” keyword is used
to create a placeholder
method or variable for an
abstract class

You might also like