0% found this document useful (0 votes)
59 views19 pages

Topic 3 - Java Data Types and Variables

This document discusses data types and variables in Java. It explains that there are two categories of data types: primitive and non-primitive. The eight primitive types are byte, short, int, long, float, double, char, and boolean. Non-primitive types refer to objects and include strings, arrays, and classes. Variables are used to store and manipulate data in memory. They must be declared with a data type before use and can be assigned literals or other values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
59 views19 pages

Topic 3 - Java Data Types and Variables

This document discusses data types and variables in Java. It explains that there are two categories of data types: primitive and non-primitive. The eight primitive types are byte, short, int, long, float, double, char, and boolean. Non-primitive types refer to objects and include strings, arrays, and classes. Variables are used to store and manipulate data in memory. They must be declared with a data type before use and can be assigned literals or other values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

INTRODUCTION TO JAVA

LESSON 3: DATA TYPES AND VARIABLES


JAVA – DATA TYPES

Data type is an attribute of data which tells the compiler or


interpreter how the programmer intends to use the data.

This data type defines the operations that can be done on the data,
the meaning of the data, and the way values of that type can be
stored. A data type provides a set of values from which an expression
(i.e. variable, function, etc.) may take its values.
JAVA – DATA TYPES

Two categories of data type in Java:


1. Primitive Data Types - There are eight primitive datatypes
supported by Java. These data types are built-in or basic to
Java. Primitive datatypes are predefined by the language and
named by a keyword.
2. Non-Primitive / Reference Data Types - Reference data types
are created using defined constructors of the classes. They are
used to access objects. These variables are declared to be of a
specific type that cannot be changed. For example, Employee,
Puppy, etc.
PRIMITIVE DATA TYPES

A. Byte
⮚ The byte data type can store whole numbers from -128 to
127 (8-bit signed two's complement integer).
⮚ If it's certain that the value of a variable will be within -128
to 127, then it is used instead of int to save memory.
⮚ Default size of this data type: 1 byte.
⮚ Default value: 0
PRIMITIVE DATA TYPES

B. Short
⮚ The short data type can store whole numbers from -32768 to
32767 (16-bit signed two's complement integer).
⮚ Short datatype can also be used to save memory as byte data
type. A short is 2 times smaller than an integer
⮚ Default size of this data type: 2 bytes
⮚ Default value: 0
⮚ The byte data type couldn’t hold the value 150 but a short
data type can because it has a wider range.
PRIMITIVE DATA TYPES

C. Int
⮚ The int data type can store whole numbers from
-2147483648 to 2147483647 (32-bit signed two's complement
integer).
⮚ In general, the int data type is the preferred data type when
we create variables with a numeric value unless there is a
concern about memory..
⮚ Default size: 4 byte
⮚ Default value: 0
PRIMITIVE DATA TYPES

D. Long
⮚ The long data type can store whole numbers from
-9223372036854775808 to 9223372036854775807 (64-bit
signed two's complement integer).
⮚ This is used when int is not large enough to store the value.
⮚ Note that you should end the value with an "L".
⮚ Size: 8 bytes
⮚ Default value: 0
PRIMITIVE DATA TYPES

E. Float
⮚ The float data type is a single-precision 32-bit floating-point.
⮚ Sufficient for holding 6 to 7 decimal digits
⮚ Use a float (instead of double) if you need to save memory in
large arrays of floating-point numbers.
⮚ Size: 4 bytes.
⮚ Default Value: 0.0
⮚ Note that you should end the value with an "f".
⮚ It should never be used for precise values such as currency.
PRIMITIVE DATA TYPES

F. Double
⮚ The double data type is a double-precision 64-bit floating-
point.
⮚ The double data type can store fractional numbers from
1.7e−308 to 1.7e+308.
⮚ Sufficient for holding 16 decimal digits.
⮚ Size: 8 bytes
⮚ Default value: 0.0 (0.0d)
⮚ Note that you should end the value with a "d".
PRIMITIVE DATA TYPES

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.
PRIMITIVE DATA TYPES

G. Char
⮚ The char datatype is a single 16-bit Unicode character
⮚ The char data type is used to store a single character.
⮚ The character must be surrounded by single quotes, like 'A'
or 'c’.
⮚ Size: 2 bytes
PRIMITIVE DATA TYPES

H. Boolean
⮚ A boolean data type is declared with the boolean keyword
and can only take the values true or false.
⮚ Default value: false.
⮚ They are usually used for true/false conditions.
NON-PRIMITIVE DATA TYPES
Non-primitive data types are called reference types because they refer to
objects.
The main difference between primitive and non-primitive data types are:
⮚ Primitive types are predefined (already defined) in Java. Non-primitive
types are created by the programmer and is not defined by Java (except for
String).
⮚ Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
⮚ A primitive type has always a value, while non-primitive types can be null.
⮚ A primitive type starts with a lowercase letter, while non-primitive types
starts with an uppercase letter.
⮚ The size of a primitive type depends on the data type, while non-primitive
types have all the same size.
⮚ Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
NON-PRIMITIVE DATA TYPES

Strings
⮚ Strings are defined as an array of characters. The difference between a
character array and a string in Java is, the string is designed to hold a
sequence of characters in a single variable whereas, a character array
is a collection of separate char type entities.
⮚ The String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes.
⮚ 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.
VARIABLES

A variable is a name given to a memory location. It is the basic


unit of storage in a program.

⮚ The value stored in a variable can be changed during


program execution.
⮚ A variable is only a name given to a memory location, all the
operations done on the variable effects that memory location.
⮚ In Java, all the variables must be declared before use.
VARIABLES

How to declare a variable:

2 parts of variable declaration:


1. Initialization
2. Assignment of value
VARIABLES
Java Literals
⮚ Literals are data used for representing fixed values. They can be used
directly in the code.
⮚ They are represented directly in the code without any computation.
⮚ Literals can be assigned to any primitive type variable.

Different Types of Java Literals:


1. Boolean Literals
2. Integer Literals
3. Floating-point Literals
4. Character Literals
5. String Literals
END OF PRESENTATION

You might also like