Post on 14-Apr-2018
7/30/2019 2. JAVA VariablesOperators
1/24
Variables and Operators
Like C.
7/30/2019 2. JAVA VariablesOperators
2/24
Module Introduction
1. Introduction to Variables Explain variables and their purpose.
Rule and naming variablesconventions
2. Introduction to Data Types Identify the different data type and
explain their purpose
Primitive and reference data types
3. Formatted Output and Input
4. Operators
5. Type casting
FPT- APTECH 2/24FPT- APTECH 2/24
7/30/2019 2. JAVA VariablesOperators
3/24
#1 Variables
A location in the computers memory where a value is stored
and from which the value is retrieved later.
Variables must be declared before they can be used.
Syntax:
datatype variableName;
FPT- APTECH 3/24FPT- APTECH 3/24
7/30/2019 2. JAVA VariablesOperators
4/24
Naming variables Conventions
May consist of Unicode letters and digits, _, $
Always begin your variable names with a letter.
Must not be a keyword or reserved word in Java.
Variable names in Java are case-sensitive
If a variable name comprises a single word, the name should be inlowercase (for example, velocity or ratio).
If the variable name consists of more than one word, the first letterof each subsequent word should be capitalized
employeeNumber, accountBalance
FPT- APTECH 4/24
7/30/2019 2. JAVA VariablesOperators
5/24
#2 - Data Type
Determines the type of data that canbe stored in the variable
Determines the operations that can be
performed on this data.
Two categories of Java data type:
1. Primitive data types
2. Reference data types
FPT- APTECH 5/24
7/30/2019 2. JAVA VariablesOperators
6/24
Primitives data types
FPT- APTECH 6/24
7/30/2019 2. JAVA VariablesOperators
7/24
Reference data types
A variable of a reference data type holds the reference to theactual value or set of value represented by the variable.
FPT- APTECH 7/24
7/30/2019 2. JAVA VariablesOperators
8/24
#3 - Format Specifier
To format output displayed on the screen.
Use: printf(String format, Object... args)
format(String format, Object... args)
FPT- APTECH 8/24
7/30/2019 2. JAVA VariablesOperators
9/24
Example of Format Specifiers
FPT- APTECH 9/24
7/30/2019 2. JAVA VariablesOperators
10/24
Formatted input
Using the Scanner object in java.util package
Example
Scanner input = new Scanner(System.in);
int i = input.nextInt();
long l = input.nextLong();
float f = input.nextFloat();
double d = input.nextDouble();String s = input.nextLine();
FPT- APTECH 10/24
7/30/2019 2. JAVA VariablesOperators
11/24
Escape Sequences
Special sequence of character that is used to representcharacters which cannot be entered directly into a string.
FPT- APTECH 11/24
7/30/2019 2. JAVA VariablesOperators
12/24
#4 Operator
Mechanism for performing various operations on the datastored in variables.
Z = A + B
Z = A + B call an Expression Z, A, B called operands
=, + called operator
FPT- APTECH 12/24
7/30/2019 2. JAVA VariablesOperators
13/24
Assignment Operator (=)
Assign the value on its right to theoperand on its left.
Allow to create a chain of assignment
FPT- APTECH 13/24
7/30/2019 2. JAVA VariablesOperators
14/24
Arithmetic Operator
FPT- APTECH 14/24
7/30/2019 2. JAVA VariablesOperators
15/24
Unary Operator
++, -- : can be applied before(prefix) or after (postfix) the operand
FPT- APTECH 15/24
7/30/2019 2. JAVA VariablesOperators
16/24
Relational Operators
Test the relationship between two operands.
Always evaluates to a boolean value (true or false)
FPT- APTECH 16/24
7/30/2019 2. JAVA VariablesOperators
17/24
Conditional Operators
&&, || : work on two boolean operand.
? : : conditional operator accepts three operands
FPT- APTECH 17/24
7/30/2019 2. JAVA VariablesOperators
18/24
Bitwise and Bit shift Operators
Work on binary representation of data.
FPT- APTECH 18/24
7/30/2019 2. JAVA VariablesOperators
19/24
Operator Precedence
FPT- APTECH 19/24
7/30/2019 2. JAVA VariablesOperators
20/24
Operator associativity
FPT- APTECH 20/24
7/30/2019 2. JAVA VariablesOperators
21/24
#5 - Type Casting
One data type converts into anotherdata type
Two type of casting
1. Implicit casting2. Explicit casting
FPT- APTECH 21/24
7/30/2019 2. JAVA VariablesOperators
22/24
Implicit Type Casting
Automatic type conversion
Conditions
Two type should be compatible
The destination type should be larger than thesource
The primitive numeric data type can beimplicitly cast (type promotion rule)
FPT- APTECH 22/24
7/30/2019 2. JAVA VariablesOperators
23/24
Explicit Type Casting
To convert a higher precision data type tolower precision data type such as float to int.
FPT- APTECH 23/24
7/30/2019 2. JAVA VariablesOperators
24/24
Thats about all for today!
Variables
Operators
Type Casting Implicit casting
Explicit casting
Thank you al l for yo ur attent ion and pat ient !