0% found this document useful (0 votes)
28 views32 pages

Data Types It Resources

It resources
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)
28 views32 pages

Data Types It Resources

It resources
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/ 32

DATA TYPES

VARIABLES
and OPERATORS
DATA TYPES
Data types specify the different sizes and values
that can be stored in the variable.

There are two types of data types in Java:

Primitive Non-Primitive
Primitive Data Types

In Java language, primitive data types are the


building blocks of data manipulation.

These are the most basic data types available in


Java language.
Primitive Data Types

In Java language, primitive data types are the


building blocks of data manipulation.

These are the most basic data types available in


Java language.
8 Types of Primitive
Data Types
❑ boolean
❑ byte
❑ char
❑ short
❑ int
❑ long
❑ float
❑ double
Boolean
The Boolean data type is used to store only two
possible values: true and false.

This data type is used for simple flags that track


true/false conditions.

The Boolean data type specifies one bit of


information, but its "size" can't be defined precisely.
Byte
It is an 8-bit signed two's complement integer.
Its value-range lies between -128 to 127 (inclusive).

The byte data type is used to save memory in


large arrays where the memory savings is most required.

It saves space because a byte is 4 times smaller


than an integer. It can also be used in place of
"int" data type.
Short
The short data type is a 16-bit signed two's
complement integer.

Its value-range lies between -32,768 to 32,767 (inclusive).

The short data type can also be used to save memory


just like byte data type.

A short data type is 2 times smaller than an integer.


Int
The int data type is a 32-bit signed two's
complement integer.

Its value-range lies between - 2,147,483,648 (-2^31)


to 2,147,483,647 (2^31 -1) (inclusive).

The int data type is generally used as a default data


type for integral values unless if there is no
problem about memory.
Long
The long data type is a 64-bit two's complement
integer.

Its value-range lies between


-9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive).

The long data type is used when you need a range of


values more than those provided by int.
Float
The float data type is a single-precision 32-bit
IEEE 754 floating point.

Its value range is unlimited.

The float data type should never be used for precise


values, such as currency. Its default value is 0.0F.
Double
The double data type is a double-precision
64-bit IEEE 754 floating point.

Its value range is unlimited.

The double data type is generally used for decimal


values just like float.

The double data type also should never be used for


precise values, such as currency. Its default value is 0.0d.
Char
The char data type is a single 16-bit Unicode character.

Its value-range lies between '\u0000' (or 0) to '\uffff'


(or 65,535 inclusive).

The char data type is used to store characters.


VARIABLES
A variable is a container which holds the value
while the Java program is executed.

A variable is assigned with a data type.

Variable is a name of memory location.


There are three types of variables in java:
local, instance and static.
______ var1 = 9;
______ var2 = 8.99f;
______ var3 = 'A';
______ var4 = false;
______ var5 = "Hello World";
Types of Variable
There are three types of variables in
Java:
• local variable
• instance variable
• static variable
Local Variable
A variable declared inside the body of the method
is called local variable.

You can use this variable only within that method


and the other methods in the class aren't even
aware that the variable exists.

A local variable cannot be defined with


"static" keyword.
Static Variable
A variable that is declared as static is called a
static variable.

It cannot be local.

You can create a single copy of the static variable


and share it among all the instances of the class.

Memory allocation for static variables happens


only once when the class is loaded in the memory.
Instance Variable
A variable declared inside the class but outside
the body of the method, is called an
instance variable.

It is not declared as static.

It is called an instance variable because its value


is instance-specific and is not shared among
instances.
OPERATORS
Operator in Java is a symbol that is used to
perform operations.

For example: +, -, *, / etc.

There are many types of operators in Java.


Types of Operators
✔ Unary Operator,
✔ Arithmetic Operator,
✔ Shift Operator,
✔ Relational Operator,
✔ Bitwise Operator,
✔ Logical Operator,
✔ Ternary Operator and
✔ Assignment Operator.
Unary Operators
The Java unary operators require only one
operand. Unary operators are used to perform
various operations i.e.:

• incrementing/decrementing a value by one


• negating an expression
• inverting the value of a Boolean
Arithmetic Operators
Java arithmetic operators are used to perform
addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
• +
• -
• *
• /
• %
Logical && and Bitwise
& Operators
The logical && operator doesn't check the second
condition if the first condition is false. It checks the
second condition only if the first one is true.

The bitwise & operator always checks both


conditions whether first condition is true or false.
Logical || and Bitwise
| Operators
The logical || operator doesn't check the second
condition if the first condition is true. It checks the
second condition only if the first one is false.

The bitwise | operator always checks both


conditions whether first condition is true or false.
Assignment
Operators
Java assignment operator is one of the most
common operators. It is used to assign the
value on its right to the operand on its left.
Ternary Operators
Java Ternary operator is used as one line
replacement for if-then-else statement and used a
lot in Java programming.

It is the only conditional operator which takes three


operands.
If-Else Example
if (a > b) {
min = a;
}else{
min = b;
}
System.out.println(min);

Ternary Operator Sample


min = (a<b)?a:b;
System.out.println(min);

You might also like