0% found this document useful (0 votes)
14 views42 pages

Java OOP: Constants, Variables, Data Types

The document covers core concepts in Object-Oriented Programming, specifically focusing on constants, variables, data types, tokens, keywords, identifiers, operators, and assignment in Java. It outlines learning objectives, the structure and rules for declaring variables, the distinction between primitive and reference data types, and various operators used in Java. Additionally, it explains the importance of constants and provides examples of variable declaration and initialization.

Uploaded by

davegach31
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)
14 views42 pages

Java OOP: Constants, Variables, Data Types

The document covers core concepts in Object-Oriented Programming, specifically focusing on constants, variables, data types, tokens, keywords, identifiers, operators, and assignment in Java. It outlines learning objectives, the structure and rules for declaring variables, the distinction between primitive and reference data types, and various operators used in Java. Additionally, it explains the importance of constants and provides examples of variable declaration and initialization.

Uploaded by

davegach31
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

OBJECT ORIENTED PROGRAMMING

RUAHA CATHOLIC UNIVERSITY


DEPT.: Computer Science
Course Code : RCS122
Lecturer: Mr. Karisa & Mr. Vincent
Module 4: Exploring Core Concepts in
Object-Oriented Programming
Constants, Variables, and Data
Types; Tokens, Keywords,
Identifiers; Operators and
Assignment in Java

Building Blocks of Java Code: Mastering Constants,


Variables, Data Types, Tokens, Keywords, Identifiers,
Operators, and Assignment
Learning Objectives
❖ Elucidate the concept of constants and demonstrate how
to declare them in Java.
❖ Comprehend the purpose of variables and illustrate their
declaration in Java.
❖ Characterize the primitive data types in Java (e.g., int,
float, boolean, char) and outline their key attributes.
❖ Assess the distinctions between primitive data types and
reference data types in Java.
Learning Objectives
❖ Define tokens, keywords, and identifiers within the
framework of Java syntax.
❖ Interpret and apply the rules governing the naming of
identifiers in Java.
❖ Describe and implement the camelCase convention for
naming variables and methods in Java.
❖ Describe and implement the PascalCase convention for
naming classes in Java.
Learning Objectives
❖ Identify and explain the various types of operators in
Java (e.g., arithmetic, relational, logical, bitwise).
❖ Evaluate the precedence and associativity rules of
operators in Java.
❖ Explain the role of the assignment operator and
demonstrate its application in Java.
Variable

A variable is a container that holds data values.


Variables are used to store information that can be
manipulated and accessed during the execution of a
program. Each variable has a name, a data type, and
a value.
Key components of a variable

1. Name: A variable's name is used to identify it in the program.


Variable names must follow certain rules:
i. Must start with a letter, underscore (_), or dollar sign ($).
ii. After the first character, can include letters, digits, underscores, or
dollar signs.
iii. Cannot be a reserved word (keywords such as int, class, etc.).
iv. Case-sensitive (e.g., felimina is different from Felimina).
Key components of a variable

[Link] Type: The data type of a variable determines the type of


values it can hold. Java has two main categories of data types:
i. Primitive Data Types: These are basic data types built into the
Java language, such as integers, floating-point numbers,
characters, and booleans.
ii. Reference Data Types: These are non-primitive data types that
include classes, interfaces, arrays, and enumerations. They store
references to objects rather than the actual objects themselves.
Key components of a variable

[Link]: The value of a variable is the data stored in


it. The value must be compatible with the variable's
data type.
Variables can be assigned values during declaration
or later in the program.
Declaration and Initialization of
variable
❖ To declare a variable in Java, you need to specify its data type
followed by the variable name.
❖ basic syntax for declaring a variable: data_type variable_name;
❖ You can also initialize the variable with an initial value at the time
of declaration.
basic syntax: data_type variable_name = initial_value;
example of declaring and initializing variables of different data types.
Declaration and Initialization of
variable: Example
Keywords
In Java, keywords are reserved words that have
predefined meanings and cannot be used for any
other purpose such as naming variables, classes, or
methods.
Keywords : examples
abstract assert byte catch const
boolean break case char do
double else enum extends final
finally floatf for goto if
implements import new package private
instanceof int long public protected
native return short static super
switch synchronized throw throws Transient
try void volatile while
Constants, Variables, and Data
types
❖ Variables may be tagged as constants (final keyword).
❖ Variables may be initialized at creation time – final
variables must be initialized at creation time
❖ Objects are variables in Java and must be dynamically
allocated with the new keyword.– E.g., a = new ClassA();
❖ Objects are freed by assigning them to null, or when they
go out of scope (automatic garbage collection).
– E.g., a = null;
Constants, Variables, and Data
types
int n = 1;
char ch = ‘A’;
String s = “Hello”;
Long L = new Long(100000);
boolean done = false;
final double pi = 3.14159265358979323846;
Employee joe = new Employee();
char [] a = new char[3];
Vector v = new Vector();
Constants, Variables, and Data
types
❖ Member Variables declared within a class are known as
member variables while those declare within functions are
known as local variables.
❖ Member variables can be either static or non-static.
❖ A static member variable belongs to the class itself, and
it exists as long as the class exists.
❖ Memory is allocated for the variable when the class is first
loaded by the Java interpreter.
Constants, Variables, and Data
types
Member variable declaration can be marked with
modifiers such as static, public, and private. For
example
static String usersName;
public static int numberOfPlayers;
private static double velocity, time;
Constants, Variables, and Data
types
❖ A static member variable that is not declared to
be private can be accessed from outside the class
where it is defined, as well as inside.
❖ When it is used in some other class, it must be
referred to with a compound identifier (with the dot
operator) of the form [Link]-name.
Constants, Variables, and Data
types
❖ In Java, constants are variables whose values
cannot be changed once they are assigned.
❖ They are declared using the final keyword, which
signifies that the value of the variable remains
constant throughout the program.
Constants, Variables, and Data
types
❖ Constants are typically used to represent fixed values
such as mathematical constants, configuration parameters,
or other values that should not change during the
execution of the program.
final double PI = 3.14159;
final int MAX_SIZE = 100;
PI and MAX_SIZE are declared as constants using the final
keyword
Constants, Variables, and Data
types
❖ Constants are typically used to represent fixed
values such as mathematical constants,
configuration parameters, or other values that
should not change during the execution of the
program.
final double PI = 3.14159;
final int MAX_SIZE = 100;
PI and MAX_SIZE are declared as constants using
the final keyword
Data types
❖ A data type is a classification of the type of data
that a variable can hold.
❖ It specifies the size and type of values that can be
stored in a variable.
❖ Java has two categories of data types: primitive
data types and reference data types.
Data types : Primitive data types

❖ Primitive data types: Primitive data types are basic


data types built into the Java language. They are
used to represent simple values such as numbers
and characters. Java has eight primitive data types
Data types : Reference data type

❖ Reference data types are non-primitive data types


that include classes, interfaces, arrays, and
enumerations.
❖ Unlike primitive data types, reference data types do
not store the actual values but store references
(addresses) to objects in memory.
Data types : Reference data type

❖ They are used to create complex data structures


and objects.
Example: String message = "Hello, World!";
Scanner scanner = new Scanner([Link]);
MyClass obj = new MyClass();
Data types
Data types

❖ Data types in Java are used to declare variables,


parameters, and return types of methods.
❖ A variable of type char occupies two bytes in
memory.
❖ The value of a char variable is a single character
such as A, *, x, or a space character.
Data types
❖ The value can also be a special character such a
tab or a carriage return or one of the many
Unicode characters that come from different
languages.
❖ When a character is typed into a program, it must
be surrounded by single quotes like 'A', '*', or 'x'.
Without the quotes, A would be an identifier and *
would be a multiplication operator.
Data types

❖ The quotes are not part of the value and are not
stored in the variable; they are just a convention
for naming a particular character constant in a
program.
Data types

❖ Certain special characters have special literals that


use a backslash (\). In particular a
✔ tab is represented as '\t',
✔ carriage return as '\r'
✔ linefeed as '\n'
✔ single quote character as '\''
✔ backslash itself as '\\'
Data types

❖ Note that even though you type two characters


between the quotes in '\t', the value represented by
this literal is a single tab character.
Operators

[Link] Operators:
❖ +: Addition
❖ -: Subtraction
❖ *: Multiplication
❖ /: Division
❖ %: Modulus (Remainder after division)
Operators
[Link] Operators:
❖ += Adds the value on the right to the current value of the variable and
assigns the result to the variable
❖ -= Subtracts the value on the right from the current value of the variable
and assigns the result to the variable
❖ *= Multiplies the current value of the variable by the value on the right and
assigns the result to the variable
❖ /= Divides the current value of the variable by the value on the right and
assigns the result to the variable
❖ %= Performs modulus division on the current value of the variable with the
value on the right and assigns the remainder to the variable
Operators

[Link] Operators:
❖ == Equality
❖ != Inequality
❖ > Greater than
❖ < Less than
❖ >= Greater than or equal to
❖ <= Less than or equal to
Operators

[Link] Operators:
❖ && Logical AND
❖ || Logical OR
❖ ! Logical NOT
Operators
[Link] and Decrement Operators:
❖ ++ Increment operator (increases the value of a variable
by 1)
❖ -- Decrement operator (decreases the value of a variable
by 1)
These operators can be used in postfix form (variable++,
variable--) or prefix form (++variable, --variable). The
difference between postfix and prefix form lies in the order of
evaluation.
Operators
[Link] Operators:
❖ & Bitwise AND
❖ | Bitwise OR
❖ ^ Bitwise XOR (Exclusive OR)
❖ ~ Bitwise NOT (Unary complement)
❖ << Left shift
❖ >> Right shift
❖ >>> Unsigned right shift
Operators

[Link] Operator (Ternary Operator):


❖ condition ? expression1 : expression2
❖ If the condition is true, the operator returns the
value of expression1; otherwise, it returns the
value of expression2.
ASSIGNMENT

In Java just as it is the case in all programming


languages one of the ways to get data into a variable
is within an assignment statement. An assignment
statement takes the form.
variable = expression;
ASSIGNMENT

❖ The expression is a mixture of variables,


constants, functions and arithmetic operators
indicating that a calculation (evaluation) is to be
done.
❖ Evaluation of the expression follows the same
rules as applied in algebra that is the computer
follows the order of operations similar to the one
that you follow when you do arithmetic.
ASSIGNMENT

After evaluating the expression, it value is stored in


the variable appearing on the left-hand side of the
assignment statement.
Examples of assignment statements
int lower =10;
rate = 0.07;
interest = rate * principal;
Questions

❖ Any Questions?, Any Clarifications?


✔ Feel free to Ask?
❖ Next: Module 5
✔ Methods and Constructors
❖ Thank You..!

You might also like