UNIT- I
CHAPTER IN BOOK- 3
DATA TYPES, VARIABLES
AND ARRAYS
-K. Indhu
SYLLABUS COVERED HERE
Data Types, Variables and Arrays
K. INDHU
GOALS
1. Why Java is strongly typed ?
2. Data Types in Java
3. Primitive Data Types in Java
4. Sample Program using Long
5. Sample Program using Double
6. Sample Program using Char
7. Sample Program using Boolean
8. Dynamic Initialization of Variables
9. Scope / Lifetime of Variables
[Link] Type Conversions
[Link] Program Type Conversion
[Link] Type Promotion Rules
[Link] Dimensional Arrays
[Link] Dimensional Arrays
K. INDHU
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
DATA TYPES IN JAVA
K. INDHU
PRIMITIVE DATA TYPES IN JAVA
K. INDHU
SAMPLE PROGRAM USING
LONG
K. INDHU
SAMPLE PROGRAM USING
DOUBLE
K. INDHU
SAMPLE PROGRAM USING
CHAR
K. INDHU
SAMPLE PROGRAM USING
BOOLEAN
K. INDHU
10
DYNAMIC INITIALIZATION OF
VARIABS
K. INDHU
11
SCOPE / LIFETIME OF
VARIABLES
K. INDHU
12
JAVAS 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
13
JAVAS TYPE CONVERSIONS
CHART
K. INDHU
14
SAMPLE PROGRAM TYPE
CONVERSION
K. INDHU
15
JAVAS TYPE PROMOTION
RULES
Java's widening conversions
are->
From
From
From
From
From
From
a byte to a short, an int, a long, a float, or a double,
a short to an int, a long, a float, or a double,
a char to an int, a long, a float, or a double,
an int to a long, a float, or a double,
a long to a float or a double,
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
16
SINGLE DIMENSIONAL ARRAYS
K. INDHU
17
SAMPLE PROGRAM
K. INDHU
18
MULTI-DIMENSIONAL ARRAYS
K. INDHU
19
SAMPLE PROGRAM
K. INDHU
20
SAMPLE PROGRAM
DECLARATION- 3
DIMENSIONAL ARRAY
K. INDHU
21
ALTERNATE ARRAY
DECLARATION WAY
K. INDHU
22
SO FAR WE STUDIED
Data Types, Variables and Arrays
K. INDHU
23
HAPPY
LEARNING!!!