Computer
Programming II
CSC202
STATUS: COMPULSORY CREDITS: 3
CSC202: Computer Programming II College of Basic & Applied Sciences
Computer Programming
Fundamentals
CSC202 Lectures
Java Data Types
CSC202: Computer Programming II College of Basic & Applied Sciences
Overview
Java Data Types
Operators in Java
CSC202: Computer Programming II 3
Preliminaries & Conventions
Java Data Types
Data types specify the type of data that can be stored inside variables
in Java.
There are 8 data types predefined in Java, known as primitive data
types.
Note: In addition to primitive data types, there are also referenced types
(object type).
Primitive Data Types
1. boolean type
This data type has two possible values, either true or false
Default value: false
CSC202: Computer Programming II Preliminaries & Conventions
Boolean Data Types Cont’d
They are usually used for true/false conditions.
Example 1: Java boolean data type
class Main {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag); // prints true
}
}
CSC202: Computer Programming II Preliminaries & Conventions
Java Data Types (2)
There are many other 'built-in' data types in Java.
byte -128 to 127 (1 byte)
short -32,768 to +32,767 (2 bytes)
int -2,147,483,648 to +2,147,483,647 (4 bytes)
long -263 to 263-1 (8 bytes)
float single precision floating point (4 bytes)
double precision floating point (8 bytes)
char 16-bit Unicode character (2 bytes)
CSC202: Computer Programming II Preliminaries & Conventions
Java Data Types (3)
String type
Java also provides support for character strings
Strings in Java are not primitive types. Instead, they are objects.
For example,
String myString = "Java Programming";
CSC202: Computer Programming II Preliminaries & Conventions
Operators in Java
An operator is a symbol. Compiler identifies Operator and performs
specific mathematical or logical operation. C provides following operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Cast Operators
Bitwise Operators
Other Operators (increment/decrement, ternary, instanceof)
CSC202: Computer Programming II C Programming Basics
Operators in Java (1)
Arithmetic Operators
* multiplication
/ division
% remainder after division (modulo arithmetic)
+ addition
- subtraction and unary minus
CSC202: Computer Programming II C Programming Basics
Operators in Java (2)
Assignment Operators
= assign
+= assign with add
-= assign with subtract
*= assign with multiply
/= assign with divide
%= assign with remainder
>>= assign with right shift
<<= assign with left shift
&= assign with bitwise AND
^= assign with bitwise XOR
CSC202:
|= Computer Programming
assign with II OR
bitwise C Programming Basics
Operators in Java (2) Cont’d
Assignment Operators Cont’d
For example,
a = a + 64; is same as
a += 64;
CSC202: Computer Programming II C Programming Basics
Operators in Java (3)
Relational Operators
== equal.
!= Not equal.
> < Greater than/less than
> = greater than or equal to
< = less than or equal to
Logical Operators
&& Called Logical AND operator. If both the operands are non-zero, then
condition becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero,
then condition becomes true.
CSC202: Computer Programming II C Programming Basics
Operators in Java (4)
Logical Operators Cont’d
! Called Logical NOT Operator.
It is used to reverses the logical state of its operand.
If a condition is true, then Logical NOT operator will make it false.
Cast Operators
Cast operators are used to convert a value from one to another type.
(float) sum; converts type to float
(int) fred; converts type to int
CSC202: Computer Programming II C Programming Basics
Operators in Java (5)
Bitwise Operators
Bitwise operators performs operation on actual bits present in each byte of a
variable.
Each byte contain 8 bits, each bit can store the value 0 or 1
~ one's complement
& bitwise AND
^ bitwise XOR
| bitwise OR
<< left shift (binary multiply by 2)
>> right shift (binary divide by 2)
CSC202: Computer Programming II C Programming Basics
Other Operators in Java
Increment and Decrement Operators
Increment and decrement operators are used to add or subtract 1 from
the current value of operand.
++ increment
-- decrement
?: ternary (conditional)
CSC202: Computer Programming II C Programming Basics
Questions?
CSC202: Computer Programming II Preliminaries & Conventions