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

5-Data Tpyes, Operators&Expression

The document discusses data types, operators, and expressions in Java. It outlines topics like data types, type casting, symbolic constants, and different types of operators such as arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators. It also covers operator precedence and associativity, and provides examples of arithmetic expressions and their evaluation in Java. The overall objective is to understand data types, operators, and expressions which are fundamental concepts in Java programming.

Uploaded by

Sanjay K
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)
41 views

5-Data Tpyes, Operators&Expression

The document discusses data types, operators, and expressions in Java. It outlines topics like data types, type casting, symbolic constants, and different types of operators such as arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators. It also covers operator precedence and associativity, and provides examples of arithmetic expressions and their evaluation in Java. The overall objective is to understand data types, operators, and expressions which are fundamental concepts in Java programming.

Uploaded by

Sanjay K
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/ 78

16CSOE04 – JAVA PROGRAMMING

UNIT – I INTRODUCTION TO JAVA

Topic: Data Types , Operators and Expressions

Presented
by

Dr.C.Rani
Assistant Professor
Department of Computer Science and Engineering
Government College of Engineering, Salem-11
1
Objectives and Outcomes

Session Objective

 To know about the Data types, operators and


Expression in Java Program

Session Outcomes

 Understand Data types, operators and Expression in

Java Program

2
OUTLINE
 Data Types
 Type Casting
 Types of Type Casting
 Introduction to Operators
 Types of Operators
 Arithmetic Opertors
 Integer Arithmetic
 Real Arithmetic
 Mixed-mode arithmetic
 Relational Operators
 Logical Operators
 Assignment Operators
 Increment and Decrement Operators
 Conditional Operator
 Bitwise Operators
 Special Operators
 instanceof Operator
 Dot Operator
 Arithmetic Expressions
 Evaluation of Expressions
3 Precedence of Arithmetic Operators
4
5
6
7
8
9
10
11
12
13
14
15
16
SYMBOLIC CONSTANTS
 Constants may appear repeatedly in number of places in
the program.
 Constant values are assigned to some names at the
beginning of the program, then the subsequent use of
these names in the program has the effect of caving
their defined values to be automatically substituted in
appropriate points.
 The constant is declared as follows:
 Syntax : final type symbolicname= value;
 Eg final float PI =3.14159;
 final int STRENGTH =100;

17
Rules :-

1.Symbolic names take the some form as variable names. But they one written in capitals
to distance from variable names. This is only convention not a rule.

2.After declaration of symbolic constants they shouldn’t be assigned any other value with
in the program by using an assignment statement.

• For eg:- STRENTH = 200 is illegal

3.Symbolic constants one declared for types there are not done in c & c++ where
symbolic constants one defined using the define statement.

4.They can’t be declared inside a method . they should be used only as class data members
in the beginning of the class.

18
TYPE CASTING
 Encounter situations where there is a need to store a value
of one type into a variable of another type.
 We must cast the value to be stored by proceeding it with
the type name in paraenthesis.
 Definition:
 The process of converting one data type to another is called casting.
 Syntax:
 type variable1= (type) variable2;
 Examples:
 int m=50;
 byte n- (byte )m;
 long count=(long)m;

19
TYPE CASTING

20
21
22
23
24
25
26
27
28
29
OPERATORS AND EXPRESSIONS

30
Outline
 Introduction
 Arithmetic Operators
 Integer Arithmetic
 Real Arithmetic
 Mixed-mode arithmetic
 Relational Operators
 Logical Operators
 Assignment Operators
 Increment and Decrement Operators
 Conditional Operator
 Bitwise Operators
 Special Operators
 instanceof Operator
 Dot Operator
 Arithmetic Expressions
 Evaluation of Expressions
 Precedence of Arithmetic Operators
31
32
33
34
Unary Operators
perat Description
or
+ Unary plus operator; indicates positive value (numbers are positive without this,
however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
~ 1's Complement operator

35
36
37
38
39
40
41
42
43
44
45
46
47
Precedence of Arithmetic operators

48
49
50
Arithmetic operators-Types
 Integer Arithmetic
 Real Arithmetic
 Mixed mode Arithmetic

51
Integer Arithmetic
 When an arithmetic operation is performed on two
whole numbers or integers than such an operation is
called as integer arithmetic. It always gives an integer as
the result.
 Let x = 27 and y = 5 be 2 integer numbers.
 Then the integer operation leads to the following results :

52
Real/ Floating point Arithmetic
• When an arithmetic operation is preformed on two real numbers
or fraction numbers such an operation is called floating point
arithmetic.
• The floating point results can be truncated according to the
properties requirement.
• The remainder operator is not applicable for floating point
arithmetic operands.
• Let x = 14.0 and y = 4.0 then

53
Mixed mode Arithmetic
• When one of the operand is real and other is an integer and if the
arithmetic operation is carried out on these 2 operands then it is
called as mixed mode arithmetic.
• If any one operand is of real type then the result will always be
real thus 15/10.0 = 1.5.

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Conditional / ternaryOperators
 Conditional operator--> (?:)
 the only ternary operator available in Java which operates on three operands.
 The conditional operator together with operands form a conditional expression.
 Syntax:
expr1? expr2:expr3

• Where expr1 is a boolean expression


• and expr2, and expr3 are the expressions of any type other than void.
• The expr2 and expr3 must be of the same type.
• The value of the expression involving the conditional operator is determined as follows,
• expr1 is a test condition which evaluates first.
• If it is true, then expr2 is evaluated, and this becomes the value of a conditional
expression.
• However, if it is false, then expr3 is evaluated, and this becomes the value of a
conditional expression.
• The value of the conditional expression can also assign to another variable.
70
Conditional Operator
 Example:
c = (a > b) ? a : b;
In this example, if (a>b) is true then a becomes value of the
conditional expression and if it is false then b becomes value of the
conditional expression which is finally assigned to c.

71
72
73
instanceof Operator
 The instanceof operator is an object reference operator which is used
to check the type of an object reference.
 The instanceof operator requires an object or array value as its left
operand and name of a reference type as its right operand.
 This operator evaluates to true if the object or array on its left-hand
side is an instance of the specified type; otherwise, it returns false.

person instanceof Manager

 evaluates true if the object person belongs to the class Manager


otherwise, it evaluates to false.

74
Dot Operator
 The dot operator (.) is used to access the fields and methods of an
object.
 For example: If s is an object of class Student then to access the
field rollNumber of the object s, we use the dot operator as
follows,
 s.rollNumber
 Similarly, if you want to access getData () method of s object then
we use the dot operator as follows,
 s.getData();

75
new Operator
 In Java, the new operator is used to create an object of a class or an
array.
 It is a unary prefix operator.
 To create an object, use the new operator with the name of the class
whose object you want to create, followed by parentheses.
 The parentheses can be empty or can contain arguments,
 Example
Student s = new Student();
 creates an object s of class Student.

76
Precedence and Associativity of Operators

77
78

You might also like