Lecture 5
Lecture 5
Unit-I
Data Types
• Data Types, Declaring and Initialization Variables, Java Operators. Class, Object &
Methods, Constructors, Overloading constructors, This Keyword, Using Objects as
Parameters, Argument passing, returning objects,
2
Content
(Unit I)
Data Types
Chapter 1.2:
Lecture 1.2.1-
1. Primitive Data Type
2. Variables
3
Objectives/Outcomes
4
Data Types
Data-types are divided into two categories:
1. Primitive Data-type: A primitive data-type specifies the size and type of variable
values, and it has no additional methods.
2. Non-Primitive Data-type: They are called reference types because they refer to
objects.
PRIMITIVE DATA TYPE NON PRIMITIVE DATA TYPE
1. Predefined in Java (already exist) 1. Created by the programmer and not defined in
java(except string)
2. Can not be used to call methods. 2. Can be used to call methods to perform certain
operations.
3. They always have a value. 3. They can be null.
4. Starts with lowercase letter 4. Starts with uppercase letter.
5. Size depends on data type. 5. Non-primitive types all have the same size
Fig 1: Difference between primitive and non primitive[1]
5
Primitive Data Types
There are eight primitive data types in java:
TYPE SIZE DESCRIPTION
6
Primitive Data Types contd.
8
Example of Variables
Here are some exampled of variable declarations of various types:
9
Example of Primitive Data Types
10
Example of Primitive Data Types
OUTPUT:
11
Difference between float and double
When to use float or double?
• The precision of a floating point value indicates how many digits the value can have
after the decimal point.
• The precision of float is only six or seven decimal digits, while double variables
have a precision of about 15 digits.
• Therefore it is safer to use double for most calculations.
12
Non-Primitive Data types
13
Non-Primitive Data types
14
Non-Primitive Data types contd.
• Classes: A class in Java is a blueprint which includes all your data. A class
contains fields(variables) and methods to describe the behavior of an object.
• Interface: Like a class, an interface can have methods and variables, but the
methods declared in interface are by default abstract (only method signature, no
body).
15
The Special Ninth Type
• The String type is so much used and integrated in Java, that some call it "the
special ninth type".
• A String in Java is actually a non-primitive data type, because it refers to an object.
• The String object has methods that are used to perform certain operations on strings.
16
Exercise
17
References
• Fig 1:https://www.w3schools.com/java/java_data_types.asp
• Fig 2: https://www.w3schools.com/java/java_data_types.asp
• Fig 3: https://www.edureka.co/blog/data-types-in-java
• Fig 4: https://www.edureka.co/blog/data-types-in-java/
18
References
• https://www.w3schools.com/java/java_data_types.asp
• https://www.edureka.co/blog/data-types-in-java/
19
THANK YOU
20