Chapter3_DataTypesVariablesArrays (1)

24
UNIT- I CHAPTER IN BOOK- 3 DATA TYPES, VARIABLES AND ARRAYS -K. Indhu

description

datatype java

Transcript of Chapter3_DataTypesVariablesArrays (1)

Page 1: Chapter3_DataTypesVariablesArrays (1)

UNIT- ICHAPTER IN BOOK- 3

DATA TYPES, VARIABLES AND ARRAYS

-K. Indhu

Page 2: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 2

SYLLABUS COVERED HERE• Data Types, Variables and Arrays

Page 3: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 3

GOALS1. Why Java is strongly typed ?2. Data Types in Java3. Primitive Data Types in Java4. Sample Program using ‘Long’5. Sample Program using ‘Double’6. Sample Program using ‘Char’7. Sample Program using ‘Boolean’8. Dynamic Initialization of Variables9. Scope / Lifetime of Variables10. Java’s Type Conversions11. Sample Program Type Conversion12. Java’s Type Promotion Rules13. Single Dimensional Arrays14. Multi Dimensional Arrays

Page 4: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 4

WHY JAVA IS STRONGLY TYPED ?• In Java->• (1) Every variable has a type, Every expression has a type

and Every type is strictly defined.

• (2) Also, all assignments are checked for type compatibility.

• (3) Any type mismatches during assignments are errors that must be corrected before the compiler will finish compiling the class.

• Thus Java is called Strongly Typed language.

Page 5: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 5

DATA TYPES IN JAVA

Page 6: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 6

PRIMITIVE DATA TYPES IN JAVA

Page 7: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 7

SAMPLE PROGRAM USING ‘LONG’

Page 8: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 8

SAMPLE PROGRAM USING ‘DOUBLE’

Page 9: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 9

SAMPLE PROGRAM USING ‘CHAR’

Page 10: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 10

SAMPLE PROGRAM USING ‘BOOLEAN’

Page 11: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 11

DYNAMIC INITIALIZATION OF VARIABS

Page 12: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 12

SCOPE / LIFETIME OF VARIABLES

Page 13: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 13

JAVA’S TYPE CONVERSIONS• When one type of data is assigned to another type of variable, an

automatic type conversion will take place if below two conditions are met->

• ■ The two types are compatible.• ■ Destination type is larger than Source type.

• When the above two conditions are met, WIDENING CONVERSION takes place.

Page 14: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 14

JAVA’S TYPE CONVERSIONS CHART

Page 15: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 15

SAMPLE PROGRAM TYPE CONVERSION

Page 16: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 16

JAVA’S TYPE PROMOTION RULES• Java's widening conversions are->

– From a byte to a short, an int, a long, a float, or a double,– From a short to an int, a long, a float, or a double,– From a char to an int, a long, a float, or a double,– From an int to a long, a float, or a double,– From a long to a float or a double,– From a float to a double.

• The Type Promotion Rules are:-1. All byte and short values are promoted to int.2. If one operand is a long, the whole expression is promoted to long.3. If one operand is a float, the entire expression is promoted to float.4. If any of the operands is double, the result is double.

Page 17: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 17

SINGLE DIMENSIONAL ARRAYS

Page 18: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 18

SAMPLE PROGRAM

Page 19: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 19

MULTI-DIMENSIONAL ARRAYS

Page 20: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 20

SAMPLE PROGRAM

Page 21: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 21

SAMPLE PROGRAM

DECLARATION- 3 DIMENSIONAL ARRAY

Page 22: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 22

ALTERNATE ARRAY DECLARATION WAY

Page 23: Chapter3_DataTypesVariablesArrays (1)

K. INDHU 23

SO FAR WE STUDIED…• Data Types, Variables and Arrays

Page 24: Chapter3_DataTypesVariablesArrays (1)

HAPPY LEARNING!!!