data types operators expressions_3

27
CT038-3-2 Object Oriented Development with Java Data Types, Operators and Expressions

Upload: zak-lee

Post on 12-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Java notes

TRANSCRIPT

Page 1: Data Types Operators Expressions_3

CT038-3-2 Object Oriented Development with

Java

Data Types, Operators and Expressions

Page 2: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Topic & Structure of the lesson

• Introduction

• Constants

• Variables

• Data Types

• Operators

• Expressions

Page 3: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Learning outcomes

• At the end of this lecture you should be able to:

• Identify data types, operators and expressions.

Page 4: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Key terms you must be able to use

• If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams:

• Constants

• Variables

• Data types

• Operator

Page 5: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Introduction

• A programming language is designed to process certain kind of data consisting of numbers, characters and strings and to provide useful output as information.

• Every program instruction must conform precisely to the syntax rules of the language.

Page 6: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Constants

• Constants in Java refer to fixed values that do not change during the execution of a program.

• In Java constant are identified with the keyword final.

Page 7: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Syntax - Constant

•In a method:final typeName variableName = expression;

•In a class:accessSpecifier static final typeName variableName = expression;

Example:

final double NICKEL_VALUE = 0.05;

public static final double LITRES_PER_GALLON = 3.785;

Page 8: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Variables

• A variable may take different values at different times during the execution of the program

Page 9: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Syntax - Variable

typeName variable Name = value;

Or

typeName variable Name;

Example

String greeting = “ Hello Java”;

Page 10: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Data Types

• Every variable in Java has a data type.

• Data types specify the size and type of values that can be stored.

Page 11: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Data Types in Java

Primitive Non Primitive

Numeric Non-numeric

Integer Floating point Character Boolean

Classes Arrays

Interface

Page 12: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Integer Type

• Integer types can hold whole numbers.

• Java supports signed values, meaning they can be both positive and negative.

Page 13: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Size and Range of Integer Types

Type Size Min value Max value

byte One byte -128 127

short Two bytes

-32 786 32 767

int Four bytes

-2 147 483 648 2 147 483 647

long Eight bytes

- 9 223 372 036 854 775 808

9 223 372 036 854 775 807

Page 14: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

• Wider data types require more time for manipulation.

• So it is advisable to use smaller data types, wherever possible.

Page 15: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Floating Point Type

• Integer can only hold whole numbers.

• So we use floating point type to hold numbers containing fractional parts.

• Example : 25.79 and -1.37

Floating Point

Float Double

Page 16: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Size and Range of Floating Point Types

Type Size Min Value Max Value

float 4 bytes 3.4e-0.38 3.4e+038

double 8 bytes 1.7e-0.38 1.7e+038

Page 17: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Character Type

• The character type, representing code units in the Unicode encoding scheme.

• The char type assumes a size of 2 bytes.

• It can hold only a single character.

• Example

char c = ‘X’ ;

Page 18: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Boolean Type

• Boolean type is used when we want to test a particular condition during the execution of the program.

• There are only two values that a boolean type can take: true or false

• Both these words are Java keywords.• boolean type uses one bit of storage.

Page 19: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Operators

• An operator is a symbol that tells the program to perform certain mathematical or logical manipulations.

Page 20: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Java operators are classified into:

• Arithmetic operators

• Relational operators

• Logical operators

• Bitwise operators

• Conditional operators

• Increment and decrement operators.

Page 21: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Expressions

• Expressions are evaluated using an assignment statement in the form

variable = expression;• When the statement is encountered, the

expression is evaluated first and the result then replaces the previous value of the variable on the left side.

Page 22: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

• Example :

x = a*b-c

The variables a,b and c must be declared before they are used in the program.

Page 23: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Type Conversions in Expressions

• Java permits mixing of constants and variables of different types in an expression.

• The final result of an expression is converted to the type of the variable on the left of the assignment.

Page 24: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Casting a Value

• Example

• Consider the calculation of ratio of girls to boys in this class.ratio = girls_number / boys_number

• girls_number and boys_number are declared as integer in the program.

• But there is possibility of the ratio to be a floating number.

Page 25: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

• This problem can be solved by converting locally the variables to the floating point as shown below.

ratio = (float)girls_number / boys_number

• The operator (float) converts girls_number to floating point.

Page 26: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Use of Casts

Examples Actionx = (int)7.5 7.5 is converted to integer

a = (int) 21.3 / (int)4.5 Evaluated as 21 / 4

b = (double) sum/n Division done in floating point mode

y = (int)(a+b) The result of (a+b) is converted to integer.

z = (int)a + b a is converted to integer then added to b

Page 27: Data Types Operators Expressions_3

CT038-3-2 OODJ Data types, Operators and Expressions

Summary

• Java has eight primitive types, including four integer types and two floating point types.

• A final variable is a constant. Once the value is set, it cannot be changed.

• You can use cast type to convert a value to different type.