0% found this document useful (0 votes)
10 views27 pages

Topic 02 - OOP - Data Types and Operators in Java

Uploaded by

kusaltharindu739
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)
10 views27 pages

Topic 02 - OOP - Data Types and Operators in Java

Uploaded by

kusaltharindu739
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/ 27

ITC 1233 - OBJECT ORIENTED

PROGRAMMING
DATA TYPES AND OPERATORS IN JAVA

Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
RECALLING…
1. Take a piece of paper and write the very first java program
you practiced during the first practical session.

2. Write your index number in the paper.


WHAT ARE DATA TYPES IN JAVA?
DATA TYPES
Following are the most commonly used data types in Java:
• int – for integers (whole numbers)
• float/double – for floating point numbers (numbers with decimal
• points)
• char – stores single characters
• boolean – stores values with two states: true or false
• String – stores text

int, float, double, char, boolean etc. are primitive data


types. In Java, String is a reference (Object) data type. This
will be discussed later.
DECLARING AND INITIALIZING VARIABLES

Syntax

Examples
PRIMITIVE DATA TYPES
Data Type
Data Type Size Description
Size
int 4 bytes Stores whole numbers
Descr
float 4 bytes ● Stores fractional numbers
● Upto 6-7 decimal digits

double 8 bytes ● Stores fractional numbers


● Upto 15 decimal digits

boolean 1 bit Stores true or false value

char 2 bytes Stores a single character/letter


or ASCII value
TYPE CASTING
In Java, mainly there are two types of type casting.
1. Widening casting (Implicit type casting)
• Converting a smaller type to a larger type
byte -> short -> char -> int -> long -> float -> double

2. Narrowing casting (Explicit type casting)


• Converting a larger type to a smaller type
• Narrowing casting should be ‘explicitly’ done by placing the
data type in
parenthesis in float
double -> front ->
of long
the value.
-> int -> char -> short -> byte
EXAMPLE: WIDENING CASTING
IS YOUR ANSWER CORRECT?

Output: 5
5.0
EXAMPLE: NARROWING CASTING

The data type should be


‘explicitly’ mentioned within
parenthesis.

Output:
5.5
5
DATA TYPE CONVERSION: PRIMITIVE DATA
TYPES TO STRINGS
DATA TYPE CONVERSION: STRINGS TO PRIMITIVE
DATA TYPES
WHAT ARE THE OPERATORS IN JAVA?
WHY WE ARE USING OPERATORS?
OPERATORS
Java operators,
• manipulate a logical or arithmetic value or operand in a
certain way to deliver a specific outcome.
• Playing a crucial role in the programming world by
dealing with simple arithmetic functions and executing
complex algorithms.
• And also responsible for key functions like security
encryption.
FORMAT OF APPLICATIONS IN OPERATORS
OPERATORS
In Java, the operators can be divided into the
following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
The behaviour of these operators are quite similar to
their behaviour in the C language.
ARITHMETIC OPERATORS
Operator Name Description Example

+ Addition Adds two values together a+b

- Subtraction Subtract one value from another a-b

* Multiplication Multiplies two values a*b

/ Division Divide one value by another a/b

% Modulus Returns a division remainder a%b

++ Increment Increment the value of the variable by 1 ++a

- - Decrement Decrement the value of the variable by 1 - -a


ASSIGNMENT OPERATORS
Operator Example Same As

= a=5 a=5

+=, -=, *=, /=, %= a+=2, a-=2, a*=2, a/=2, a%=2 a=a+2, a=a-2, a=a*2, a=a/2,
a=a%2

&=, |= ,^= a&=2, a|=2, a^=2 a=a&2, a=a|2, a=a^2

<<=, >>= a<<2, a>>2 a=a<<2, a=a>>2


COMPARISON OPERATORS
Operator Name Example
== Equals to a==b

!= Not equal a!=b

> Greater than a>b

< Less than a<b

>= Greater than or equal to a>=b

<= Less than or equal to a<=b


LOGICAL OPERATORS

Operator Name Example


&& Logical and a<<8 && b>>3

|| Logical or a>=5 || b<2

! Logical not !(a>>2 && b<<7)


BITWISE OPERATORS
Operator Name Example
& Bitwise AND a&b

| Bitwise OR a|b

^ Bitwise XOR a^b

~ Bitwise Complement ~a

<< Bitwise left shift a<<b

>> Bitwise right shift a>>b

>>> Unsigned right shift a>>>b


QUESTION: STATE WHETHER THE
FOLLOWING STATEMENTS ARE TRUE OR
FALSE
1. Comments cause the computer to display the text after the // on the
screen when the program executes.
2. All variables must be given a type when they’re declared.
3. Java considers the variables number and NuMbEr to be identical.
4. The remainder operator (%) can be used only with integer operands.
5. The arithmetic operators *, /, %, + and - all have the same level of
precedence.
6. The identifier _ (underscore) is valid in Java 9.
ACTIVITY 01
public class Arithmetic{
public static void main ( String[] args ) {
int a = 17;
int b = 6;
System.out.println ( a + b );
System.out.println ( a – b );
System.out.println ( a * b );
System.out.println ( a / b );
System.out.println ( a % b );
}
}
ANY QUESTIONS?

You might also like