Chapter3_DataTypesVariablesArrays (1)
description
Transcript of Chapter3_DataTypesVariablesArrays (1)
UNIT- ICHAPTER IN BOOK- 3
DATA TYPES, VARIABLES AND ARRAYS
-K. Indhu
K. INDHU 2
SYLLABUS COVERED HERE• Data Types, Variables and Arrays
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
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.
K. INDHU 5
DATA TYPES IN JAVA
K. INDHU 6
PRIMITIVE DATA TYPES IN JAVA
K. INDHU 7
SAMPLE PROGRAM USING ‘LONG’
K. INDHU 8
SAMPLE PROGRAM USING ‘DOUBLE’
K. INDHU 9
SAMPLE PROGRAM USING ‘CHAR’
K. INDHU 10
SAMPLE PROGRAM USING ‘BOOLEAN’
K. INDHU 11
DYNAMIC INITIALIZATION OF VARIABS
K. INDHU 12
SCOPE / LIFETIME OF VARIABLES
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.
K. INDHU 14
JAVA’S TYPE CONVERSIONS CHART
K. INDHU 15
SAMPLE PROGRAM TYPE CONVERSION
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.
K. INDHU 17
SINGLE DIMENSIONAL ARRAYS
K. INDHU 18
SAMPLE PROGRAM
K. INDHU 19
MULTI-DIMENSIONAL ARRAYS
K. INDHU 20
SAMPLE PROGRAM
K. INDHU 21
SAMPLE PROGRAM
DECLARATION- 3 DIMENSIONAL ARRAY
K. INDHU 22
ALTERNATE ARRAY DECLARATION WAY
K. INDHU 23
SO FAR WE STUDIED…• Data Types, Variables and Arrays
HAPPY LEARNING!!!