0% found this document useful (0 votes)
69 views

Lecture 2.1 - Data Type & Type Casting

The document provides an overview of data types and type casting in Java, detailing both primitive and non-primitive data types, their memory sizes, value ranges, and default values. It explains the concept of wrapper classes and the process of type casting, including implicit and explicit casting with examples. Additionally, it lists references for further reading on Java programming.

Uploaded by

subsadult
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Lecture 2.1 - Data Type & Type Casting

The document provides an overview of data types and type casting in Java, detailing both primitive and non-primitive data types, their memory sizes, value ranges, and default values. It explains the concept of wrapper classes and the process of type casting, including implicit and explicit casting with examples. Additionally, it lists references for further reading on Java programming.

Uploaded by

subsadult
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

Data Type and Type Casting


Course Code: CSC 1205 Course Title: Object Oriented Programing 1 (JAVA)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 2.1 Week No: 2 Semester: Fall 20-21


Lecturer: Rifath Mahmud ([email protected])
2

Data Type and Type Casting

1. Different Data Types in Java


2. Memory size, Value range and default value of data types
3. Wrapper Classes
4. Different type of type casting
5. Type casting of primitive data types
3

Data Types in Java


Data Type Classification Tree

Data Types
Primitive Data Types Non Primitive Data Types

Boolean Type AlphaNumeric Type


Built in Library Classes
boolean Integral Type Floating Point Type
User Defined Classes
float double
Character Type Integer Type

char
byte short int long
4

Data Types in Java


What are primitive and non-primitive data types?

Primitive data types: Primitive data types are predefined types of data, which
are supported by the programming language. For example, integer, character,
and float are all primitive data types. Programmers can use these data types
when creating variables in their programs.

Non-primitive data types: Non-primitive data types are not defined by the
programming language, but are instead created by the programmer. They are
sometimes called "reference variables," or "object references," since they
reference a memory location, which stores the data. In the Java programming
language, non-primitive data types are created, rather than predefined. While
an object may contain any type of data, the information referenced by the
object may still be stored as a primitive data type.
5

Data Types in Java


Data type summary

Primitive Data Types Non-Primitive Data Types

1. boolean Any built in library classes in java and any


2. byte classes that we will be creating are non
3. short primitive Data types. Some Examples:
4. int
5. long 1. String
6. char 2. System
7. float 3. Scanner
8. double
6

Data Types in Java


Java Literals

Java Literals are syntactic representations of boolean, character,


numeric, or string data. For example, in the following statement, an
integer variable named count is declared and assigned an integer
value. The literal 0 represents, the value zero.

int count = 0;
7

Data Types in Java


Memory Size for data types

Data Type Memory Size It means that if we declare a variable of int type, it
boolean 1 Byte will occupy 4 Bytes of memory.

byte 1 Byte Similarly, When we declare a variable char c, it will


short 2 Bytes occupy 2 Bytes of memory
int 4 Bytes
1 Byte = 8 bits,
long 8 Bytes So, 4 Bytes = 32 bits
char 2 Bytes and, 2 Bytes = 16 bits
float 4 Bytes
double 8 Bytes
8

Data Types in Java


Value Range and Default Value

Data Type Minimum Value Maximum Value Default Value boolean has only
boolean - - false 2 values:
true and false
byte -27 27 -1 0
short -215 215 -1 0 Question is,
How to calculate
int -231 231 -1 0
Value ranges for
long -263 263 -1 0L the other data
char ‘\u0000’ ‘\uFFFF’ ‘\u0000’ types?
-3.4 x 1038 -1.4 x 10-45
float 0.0F Formula:
1.4 x 10 -45
3.4 x 10 38

If number of bits = n
-1.79 x 10308 -4.9 x 10-324
double 0.0 Min Value = -2n-1
4.9 x 10 -324
1.79 x 10 308
Max Value = 2n – 1
9

Data Types in Java


Calculating Value Ranges

Data Type Minimum Value Maximum Value Size of byte is 1Byte [8 bits]
byte -128
-27 2127
7
-1
Known as Sign Bit. Represents
whether the number is Positive or
Lets, declare two byte type variable and draw negative.
the memory representation for them If the value of sign bit is 1, it is a
negative number
byte b1, b2; If the value of sign bit is 0, it is a
positive number
b1 1 0 0 0 0 0 0 0 Equivalent decimal
Rest of the is -128
bits are 0

b2 0 1 1 1 1 1 1 1 Equivalent decimal
Rest of the is 1
bits are 127

The same approach can be followed for short, int and long
10

Data Types in Java


Calculating Value Ranges

Data Type Minimum Value Maximum Value Size of char is 2 Bytes [16 bits]
char ‘\u0000’ ‘\uFFFF’
Values of char data type are
unsigned
Lets, declare two char type variable and draw
the memory representation for them

char c1, c2;

c1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 All the
Equivalent bits are 0 0000
Hexadecimal
c2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
All the
Equivalent bits are 1 FFFF
Hexadecimal
11

Data Types in Java


Wrapper Class

A wrapper class is a java library class whose object wraps or contains a primitive
data type.
Data Type Wrapper Class Wrapper classes contains methods that are required
boolean Boolean for performing some operations regarding their
respective primitive data type.
byte Byte
short Short For Example: Let’s say we want to convert a String
value to an integer value or double value. The Integer
int Integer class has a method to convert the String into an
long Long integer and the Double class has a method that
char Character
converts a String value to a double value.

float Float int parseInt(String)


double parseDouble(String)
double Double
12

Type Casting
Type Casting

Type Casting:
Type Casting is the process of converting the value of a primitive data type to
another primitive data type. Example: Converting an integer value to a double value
and vice versa, Converting a char value to an integer and vice versa etc.
There are two types of type casting:
1. Implicit Type Casting
2. Explicit Type Casting

Explicit Type Casting: Implicit Type Casting:


• Converting the value of a larger • Converting the value of a smaller
primitive data type to a smaller primitive data type to a larger
primitive data type. primitive data type.
• Possibility of value loss during the • No possibility of value loss during
conversion exists. the conversion.
13

Type Casting
Implicit Type Casting

Implicit Implicit Implicit

Data Type byte short int long


Memory Size 1B < 2B < 4B < 8B

Implicit Implicit Implicit

byte int byte long short long


1B < 4B 1B < 8B 2B < 8B
14

Type Casting
Explicit Type Casting

Explicit Explicit Explicit

Data Type byte short int long


Memory Size 1B < 2B < 4B < 8B

Explicit Explicit Explicit

byte int byte long short long


1B < 4B 1B < 8B 2B < 8B
15

Type Casting
Example: Implicit Type Casting

Lets, declare one byte type variable, one short type variable and draw the memory
representation for them

byte b1;
short s1;

Assigning the
Equivalent
Equivalent value
binary ofof
120gets
decimal
Assigning b1
ins1 isin120
stored
b1 s1 b1 0 1 1 1 1 0 0 0

s1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0

Now, if we write the following statements: Sign bit


Rest of b1
of the bitsget
in copied into
s1 is filled
b1 = 120; the sign
with 0. bit of s1. Rest of the
bits of b1 gets copied into their
s1 = b1; //implicit type casting respective positions in s1.
16

Type Casting
Example: Explicit Type Casting

Lets, declare one short type variable, one byte type variable and draw the memory
representation for them

short s2;
byte b2;

s2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0
Equivalent decimal
binary
Assigning the
130 value is
of-126.
in s2gets stored
s2 in b2in s2 b2 1 0 0 0 0 0 1 0

Now, if we write the following statements: The bits of b2 gets filled up by


s2 = 130; the respective bits of s2.

b2 = (byte) s2; //explicit type casting


17

Type Casting
Some more Type Castings

Implicit Explicit Explicit ??

float double float double float long


4B < 8B 4B < 8B 4B < 8B
5.5 5

Implicit Explicit Implicit ??

int double int double float long


4B < 8B 4B < 8B 4B < 8B
5 5.0 5 5.5 5.0 5
18

Type Casting
Some more Type Castings

Explicit Explicit

char short char short


2B 2B 2B 2B

Implicit Explicit Explicit Implicit

char int short char int short


2B < 4B > 2B 2B < 4B > 2B
Books

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• Java How to Program Java, 9th Edition, By Deitel and Deitel.
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• Introduction to Programming Using Java, 6th Edition, By David j. Eck
• Head First Java, By Kathy Sierra and Bert Bates
References

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• docs.oracle.com

You might also like