52
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and reference types To know the data range and storage requirements for each type To know the conditions for data conversion To know the permissible operations that can be performed on data To be able to evaluate expressions

Data Types and Operations On Data Objective To understand what data types are The need to study data…

Embed Size (px)

DESCRIPTION

Data Types - Introduction The concept of data type is like what the bolts and nuts are to a piece of machinery. It is impossible to write meaningful program, without understanding: Data types, data values The amount of memory required for each type of data, and The permissible operations that can be performed on data

Citation preview

Page 1: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Data Types and Operations On Data

Objective

• To understand what data types are

• The need to study data types

• To differentiate between primitive types and reference types

• To know the data range and storage requirements for each type

• To know the conditions for data conversion

• To know the permissible operations that can be performed on data

• To be able to evaluate expressions

Page 2: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Data Types and Operations On Data

• Introduction• Data Types • Primitive Type

Integral Type Floating Point Type Character Type Boolean Type

• Type Compatibility• Reference Type• Arithmetic Operator and Operations• Arithmetic Expressions• Relational Operator and Operations• Relational Expressions• Logical Operator and Operations• Input and Output Operations

Page 3: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Data Types - Introduction

• The concept of data type is like what the bolts and nuts are to a piece of

machinery.

• It is impossible to write meaningful program, without understanding:

Data types, data values

The amount of memory required for each type of data, and

The permissible operations that can be performed on data

Page 4: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Data Types - Introduction

• Like anything else, conservation of the use of memory is important.

• Some former languages did not consider conserving memory.

• Most of them had only two ways to store numeric values:

int, for integers, and

float, for floating point values.

• Hence wasting memory, especially when storing small values ;

• The amount of memory for each value, large or small, is the same.

Page 5: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Introduction – Data Types

• In the first section we:

a) Study the fundamental data types in Java, and

b) how they relate to programming.

c) Focus on the operations that can be performed on each type.

• The second section introduces the reference type.

• This includes some of the fundamental Java classes, such as:

a) The String class

b) The Math class

c) The wrapper classes

Page 6: Data Types and Operations On Data Objective To understand what data types are The need to study data…

The Fundamental Types

• We had established that data is vital to a program.

• It must be stored in primary memory for the processor to handle it.

• The Java specifies two broad categories of data types:

a) Primitive, and

b) Reference type

• Primitive types are atomic

• They cannot be decomposed into simpler types

• Reference types are constructed from:

a) Primitive types,

b) As well as from other reference types

Page 7: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Data Types

Data types

Primitive types Reference types

Integral types Floating Point

boolean

Integers

byte shortint long float double

User defined classes

Java classes

char

Boolean types

Character

Page 8: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Primitive Types

• Primitive types are atomic

• There are three types – integral, floating point, and boolean

• Integral – they can be represented by an integer value

• There are two groups – integer and character

• Integer – byte, short, int, long

Page 9: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Integer Type

Data types Storage Required Range of Values

byte 8 bits (byte) -128 to +127

Short 16 bits (2 bytes) -32,768 to +32,767

int 32 bits (4 bytes0 2,147,483,648 to +2,147,483,647

long 64 bits (8bytes) -9223,372,036,854,775,808 to

+9223,372,036,854,775,807

Page 10: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Floating Point Type

Data Type Storage required Range of Values

float 32 bits (4 bytes) -3.4 x 1038 to +3.4 x 1038

double 64 bits (8 bytes) -1.7 x 10308 to +1.7 x 10308

Page 11: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Storage Space

long

int

short

byte

Page 12: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Default Values

• Integer types – 0

• Floating point types – 0.0

Page 13: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Assignment Incompatibility

• Variables can be initialized wrongly

• This situation gives rise to syntax errors

• Consider the following statement:

int x = 2.0;

Configuration: j2sdk1.4.1_02 <Default>----C:\chapter3\unicodeChar.java:5: possible loss of precision found : double required: intint x = 2.0; ^1 errorProcess completed

Page 14: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Assignment Incompatibility

• Consider the following statement:

short x = 150000;

• This gives rise to syntax error also.

Configuration: j2sdk1.4.1_02 <Default>----C: \chapter3\unicodeChar.java:5: possible loss of precision found : int required: shortshort x = 150000; ^1 errorProcess completed

Page 15: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Assignment Incompatibility

Consider the following segment of code:

int x = 2;byte y = x;

-----Configuration: j2sdk1.4.1_02 <Default>----C:\istings\data_types\default_types.java:6: possible loss of precisionfound : intrequired: byte byte y = x; ^1 error

Process completed.

Page 16: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Character type - char

• The character data type named char is :

Any printable symbol found on the keyboard, or

Certain sequence of characters called escape sequence. • In either case, it requires 16 bits (2 bytes) of memory to store the char value

• A char value can be represented decimal value in the range 0 to 65,536, or as Unicode character in the range ‘\u0000” to ‘\uFFFF’

Data type Storage Required Range in Decimal Range in Unicode

Char 16 bits (2 bytes) 0 to 65,536 \u0000 to \uFFFF

Page 17: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Boolean Type

• Java’s logical data type is called boolean.

• The set of values that represent the boolean data type is true and false.

• This data type is implemented when comparing primitive types. • Boolean variables are declared the same way as other variables

• The default of a boolean variable is false • Consider the following statement

boolean x = 0;

Configuration: j2sdk1.4.1_02 <Default>----C: \chapter3\unicodeChar.java:5: incompatible typesfound : int required: boolean boolean x = 0; ^1 errorProcess completed.

Page 18: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Operator and Operations on Data

• Computers are known as number crunching machines.

• To crunch numbers, they need to perform operations on the numbers

• Java has five types of operations to perform on primitive data values:

Arithmetic operations

Relational operations

Logical operations

Bit-wise operations, and

Bit-shift operations

• We will study the first three – Arithmetic, Relational and Logical operations

Page 19: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Arithmetic Operator and Operations

• Java defines five binary arithmetic operators: + - * / %• They are used to form arithmetic expressions. • The format of an arithmetic expression is:

operand operator operand;• Operands are any valid identifier or numeric literal, and • Operator is any of five arithmetic operators. See summarized below

Operator Name Algebraic form Example Result

+ Addition x + y 25 + 15 40

- Subtraction x – y 25 -1 15 10

* Multiplication x * y 18 * 2 36

/ Division x / y 37 / 5 7

% Modulus operation x % y 37 % 5 2

Page 20: Data Types and Operations On Data Objective To understand what data types are The need to study data…

The operator / vs %

• The operators (+ , - , * ) have the usual arithmetic meaning• The operator ( / ), gives the quotient when applied to division

That is why: 37/5 = 7 , and not 7.4

• The operator ( % ), gives the remainder when applied to division Hence, 37 % 5 = 2.

2___________

35

7375

2___________

35

7375

Page 21: Data Types and Operations On Data Objective To understand what data types are The need to study data…

The operator / vs %

• If a task takes a worker 127 minutes to complete, how many hours and how many minutes did it take the person to complete.

• If we were to program this, we would have to tell the computer precisely how to carry out the calculation. That is:

The number of hours would be (127 / 60) 2 hours, and

The number of minutes would be (127 % 60) 7 minutes.

Page 22: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Example

• A small company wants you to write a program to figure out the number of

boxes needed to ship book orders without wasting space. They have four types

of boxes: extra large, large, medium and small, which can hold 25, 15, 5 and 1

book(s) respectively.

• Write a Java program that accepts the number of books to be shipped and

displays the number of boxes needed with their type. For example if the

company wants to ship 81 books your output should be 3 big boxes, 1 medium

box and 1 small box.

Page 23: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Solution

• As before let us identify the elements of the problem

• Let us call the entity – PackingBooks

• No list the characteristics (attributes) of PackingBooks Constants Variables

• Constructor

• Operations Methods

Page 24: Data Types and Operations On Data Objective To understand what data types are The need to study data…

PackingBooks - Attributes

• Constants Extra large box Large box Medium box Small box

• Variables books Extra large Large Medium Small

Page 25: Data Types and Operations On Data Objective To understand what data types are The need to study data…

PackingBooks - Constructor

• The problem suggests only one argument required – the number of books

• PackingBooks( books)

Mutator Method

determineBoxes()

Accessor Methods

getBigBox()

getLargeBox()

getMediumBox()

getSmallBox()

Page 26: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Class PackingBooks

1. class Packing2. {3. static final int XTRA_LARGE_BOX = 25,4. LARGE_BOX = 15,5. MEDIUM_BOX = 5,6. SMALL_BOX = 1;7.8. int books;9. int big, large, medium, small;10.11. Packing(int books)12. {13. this.books = books;14. }

Page 27: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Class PackingBooks – Mutator Method

15.16. void determineBoxes()17. {18. big = books/XTRA_LARGE_BOX;19. books = books % XTRA_LARGE_BOX;20.21. large = books/LARGE_BOX;22. books = books % LARGE_BOX;23.24. medium = books/MEDIUM_BOX;25. small = books % MEDIUM_BOX;26. }

Page 28: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Class PackingBooks – Mutator Method

27. int getXtraLargeBox()28. {29. return big;30. }31.32. int getLargeBox()33. {34. return large;35. }36.37. int getMediumBox()38. {39. return medium;40. }41.42. int getSmallBox()43. {44. return small;45. }46. }

Page 29: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Class PackingBooks – Test class

1. class TestPacking2. {3. public static void main(String [] arg)4. {5. Packing pack = new Packing(127);6. pack.calculate();7.8. System.out.println(pack.getXLarge() + " extra large boxex\n" + pack.getLarge()9. + " large boxex\n" + pack.getMedium() 10. + " medium boxes\n" + pack.getSmall()11. + " small box\n" + pack.getTotal()12. + " total boxes\n") ;13. }14. }

Page 30: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Integer Operations

Integer oprations Result Example Actual Result Correct Result

int + int number too large 2147483647 + 53 -2147483596 2147483700

int - int number too large -2147483647 - 53 2147483596 -2147483700

int * int number too large 1000000000 * 500 1783793664 500000000000

int / int Arithmetic Exception 125/0 ArithmeticException No correct result

int % int Arithmetic Exception 125 % 0 ArithmeticException No correct result

Integer operations can produce erroneous results

Page 31: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Floating Point OperationsFloating -Point operations can produce erroneous results

Floating-Point Operations Results

float + float Infinity

float - float -Infinity

float * float Infinity

float / float Infinity

-float / float -Infinity

-float / 0.0 -Infinity

0.0/0.0 NaN

-float * float -Infinity

Modulus operation ( % ) Same as division

Page 32: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Arithmetic expressions

2 + 3 – 4 + 5

2 + 3 – 4 + 5

5

1

6

In general, arithmetic expressions are evaluated from left to right

Page 33: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Arithmetic expressions

• The order of evaluating an expression may be altered.

• Criteria

Unary operators are done first - they have the highest priority.

Parenthesize (sub-expressions) have the next level of priority.

Multiplication, division and modulus have next level of priority

Addition and subtraction have the lowest level of priority.

Page 34: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Arithmetic expressions

2 + 3 – 4 * 5

2 + 3 – 4 * 5

20 5

-15

The operations * / % must be carried out before addition and subtraction

Page 35: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Arithmetic expressions

2 + ( 3 – 4) * 5

2 + ( 3 – 4 ) * 5

-1

-5

-3

The parentheses must be carried out before + - * / %

Page 36: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Arithmetic expressions

11 % 4 * 2 – 14/3 + (8 –2 * -3)

11 % 4 * 2 – 14 / 3 + ( 8 – 2 * -3 )

-6

14 3

6

4

2

16

Page 37: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Converting Algebraic Expression

s = s0 +v0t + ½ gt2

• s = s0 + v0 * t + g * t * t / 2.0; or

• s = s0 + v0 * t +1.0/2 *g * t * t ; or

• s = s0 + v0 * t +0.5 *g * t * t ; or

• s = s0 + v0 * t + (float)1/2 *g * t * t ;

Page 38: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Centigrade =

1. class DataTypes2. {3. public static void main(String[] arg)4. {5. double fahrenheit = 42.0;6. double centigrade = 5/9*(fahrenheit - 32.0);7. System.out.println(centigrade);8. }9. }

9)32(5 fahrenheit

Page 39: Data Types and Operations On Data Objective To understand what data types are The need to study data…

The Fundamental Types

What will happen if you attempt to compile each of the following lines of code?

1. int x = 25.0;

2. float x = 1.0;

3. byte x = 130;

4. char x = 128;

5. boolean x = 1;

Page 40: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Relational Operator and Operations

• There are six binary relational operators in Java.

• They are used to form conditional expressions.

• They are used with primitive numeric and the character types only.

Page 41: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Relational Operators & Operations

Operator Operation Usage pattern Example

< Strictly less than x < y 100 < 200

<= Less than or equal tox <= y 100 <= 200

> Strictly greater than x > y 200 > 100

>= Greater than or equal to x >= y 200 >= 100

== Is equal to x == y 200 == 100

!= Not equal to x != y 200 != 100

Page 42: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Relational expression• Relational expression is a combination of two primitive types separated by a

relational operator.

• The result from evaluating the expression is one of the boolean values, true or false.

• The general form of simple conditional statements is:

• Left_Operand Relational_Operator Right_Operand

Example: • 10 > 15• ‘a’ != ‘A’

Page 43: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluating Relational Expressions

• When evaluation relational expressions, you must:• First resolve all arithmetic expressions to get the primitive values.• Then compare the primitive values as specified by the relational operator

Example: Use the following declaration to evaluate the relational expressions

int x = 10, y = 20;double z = 5.0;

Evaluate the following relational expressions.

1. x / y < z

2. x%y - 10 >= x - y/x - z

Page 44: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Relational Expression: x / y < z

int x = 10, y = 20;double z = 5.0;

x / y < z

0

true

Page 45: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Relational Expression: x%y - 10 >= x - y/x - z

int x = 10, y = 20;double z = 5.0;

x % y - 10 >= x - y / x - z

10 2 0 8

3.0 false

Page 46: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Relational Expression: x%y – (10 >= x ) - y/x - z

int x = 10, y = 20;double z = 5.0;

X % y – ( 10 >= x ) - y/x - z

Page 47: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Relational Expression: x%y – (10 >= x ) - y/x - z

int x = 10, y = 20;double z = 5.0;

x % y - ( 10 >= x ) - y / x - z

10 true

????

Page 48: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluating Relational Expressions

• The value from a relational expression can be assigned to a Boolean variable. The format is:

boolean id = relational_expression. For example :

int x = 120;

double y = 20;

boolean flag = 2 * x > y + 2;

• Note: The relational expression must be evaluated first. The resulting value is assigned to the Boolean variable flag.

In this case the value true is assigned to the variable flag.

Page 49: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Logical Operator and Operations

• Java has three logical operators:

logical-AND ( && ) – Determines the validity of two relational expressions

logical-OR ( || ) - Determines the validity of one or both two relational expressions

logical-NOT ( ! ) – Negates the result of a boolean value

• This means that the relational expressions must first be evaluated, before logical operators can be applied.

Page 50: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Logical Expression: x + y > z && x – y < z

int x = 10, y = 20;double z = 5.0;

x + y > z && x – y < z

30 -10

true true true

Page 51: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Logical Expression

int x = 10, y = 20;double z = 5.0;

x - y > z || x – y < z

-10 -10

false true true

Page 52: Data Types and Operations On Data Objective To understand what data types are The need to study data…

Evaluate Logical Expression: !(x + y > z && x – y < z)

int x = 10, y = 20;double z = 5.0;

!(x + y > z && x – y < z)

30 -10

true true true

false