0% found this document useful (0 votes)
7 views20 pages

Lecture 5

The document outlines the syllabus for a Java Programming course, focusing on data types, including primitive and non-primitive types. It details the characteristics of various data types, variable declaration, and the importance of choosing the correct data type for specific scenarios. Additionally, it includes exercises and references for further learning.

Uploaded by

Deepak Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

Lecture 5

The document outlines the syllabus for a Java Programming course, focusing on data types, including primitive and non-primitive types. It details the characteristics of various data types, variable declaration, and the importance of choosing the correct data type for specific scenarios. Additionally, it includes exercises and references for further learning.

Uploaded by

Deepak Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIVERSITY INSTITUTE COMPUTING

BSc (CS)/ BCA


Subject Name: Java Programming
Code: 22SCT-351/22CAT-351

JAVA PROGRAMMING DISCOVER . LEARN . EMPOWER


1
Syllabus

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

CO1: Identify the ways to implement the inheritance concept in


the Java programming language.

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

byte 1 byte Stores whole numbers from -128 to 127.


short 2 bytes Stores whole numbers from -32,768 to 32,767.
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647.
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal
digits.
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits.
boolean 1 bit Stores true or false values.
char 2 bytes Stores a single character/letter or ASCII values.
Fig 2: Primitive Data Types[2]

6
Primitive Data Types contd.

Fig 3: Primitive data types[3]


7
Variables
Variable

• The variable is the basic unit of storage in the java program.


• A variable is defined by the combination of an identifier, a type, and an optional
initializer.
• In addition, all variables have a scope, which defines their visibility, and a lifetime
• The basic form of a variable declaration is shown here:
Type identifier [ = value][, identifier [= value] ...] ;

8
Example of Variables
Here are some exampled of variable declarations of various types:

1. int a, b, c; // declares three ints, a, b, and c.


2. int d = 3, e, f = 5; // declares three more ints, initializing d and f.
3. byte z = 22; // initializes z.
4. double pi = 3.14159; // declares an approximation of pi.
5. char x = 'x'; // the variable x has the value 'x'.

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

Fig 4: Non primitive data types[4]

13
Non-Primitive Data types

• Strings: String is a sequence of characters. But in Java, a string is an object that


represents a sequence of characters. The java.lang.String class is used to create a
string object.

• Arrays: Arrays in Java are homogeneous data structures implemented in Java as


objects. Arrays store one or more values of a specific data type and provide indexed
access to store the same. A specific element in an array is accessed by its index.

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

Add the correct data type for the following variables:


1. ________num1 = 6;
2. ________num2 = 8.99f;
3. ________ letter = 'A';
4. ________ exp = false;
5. ________text= "Hello World";

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

You might also like