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

Data Types and Operators

The document provides an overview of data types and operators in Java, detailing literals, primitive and non-primitive data types, and their sizes. It explains variable types, scopes, lifetimes, and various operators including arithmetic, relational, and logical operators. Additionally, it covers type conversion methods and includes exercises for practical application.

Uploaded by

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

Data Types and Operators

The document provides an overview of data types and operators in Java, detailing literals, primitive and non-primitive data types, and their sizes. It explains variable types, scopes, lifetimes, and various operators including arithmetic, relational, and logical operators. Additionally, it covers type conversion methods and includes exercises for practical application.

Uploaded by

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

Data Types and Operators

Department of Computer Science and Engineering 1


Literals in Java

In Java, literals are constant values that are directly assigned to variables or used in expressions. They represent
fixed values in the code.

Types of Literals:

1. Integer Literals: Example: 10, 1000, 0xFF (hexadecimal), 012 (octal)


2. Floating-Point Literals: Example: 3.14, 2.0f, 0.0
3. Character Literals: Example: 'A', '1', '%‘
4. String Literals: Example: "Hello, World!", "Java"
5. Boolean Literals: Example: true, false
6. Null Literal: Example: null

2
Department of Computer Science and Engineering
Data Types in Java

3
Department of Computer Science and Engineering
Data Types and Sizes

Data types represents different values with different sizes that can be stored in the variable. primitive
data types are the building blocks of data manipulation. There are two types of data types in Java:

Primitive data types: Non-primitive data types:


The primitive data types include The non-primitive data types include

boolean Strings
char Classes
byte Interfaces
short Arrays
int
long
float and double

Department of Computer Science and Engineering 4


Primitive and Non-Primitive Data Types

Department of Computer Science and Engineering 5


Data Types and Its Sizes
Type Type Byte Range
byte 1 minimum value of -128 to a maximum value of 127
short 2 minimum value of -32768 to a maximum value of
Integer Value
32767
int 4 minimum value of -231 to a maximum value of 231-1
long 8 minimum value of -263 to a maximum value of 263-1
float 4 -3.4e+38 to 3.4e+038
Floating Point
double 8 -1.7e+308 to 1.7e+308
Character char 2 0 to 65, 536
Boolean boolean 1 true or false

Department of Computer Science and Engineering 6


Data Types and Sizes

In Java, unlike languages like C or C++, you don't have direct control over the memory sizes of primitive
data types or objects. However, you can still get the size of data types using various techniques.
• Java provides constants in wrapper classes to get the size in bytes of primitive data types.
Example
public class Main {
public static void main(String[] args) {
System.out.println("Size of byte: " + Byte.BYTES + " byte(s)");
System.out.println("Size of short: " + Short.BYTES + " byte(s)");
System.out.println("Size of int: " + Integer.BYTES + " byte(s)");
System.out.println("Size of long: " + Long.BYTES + " byte(s)");
System.out.println("Size of float: " + Float.BYTES + " byte(s)");
System.out.println("Size of double: " + Double.BYTES + " byte(s)");
System.out.println("Size of char: " + Character.BYTES + " byte(s)");
}
}

Department of Computer Science and Engineering 7


How to Find the Type of Variable in Java?

1. Using instanceof Operator : If you want to check if an object is of a specific


type, you can use the instanceof operator.
Example:

public class Main {


public static void main(String[] args)
{
Object obj = "Hello, World!";
if (obj instanceof String)
{
System.out.println("The variable is of type String");
}
else if (obj instanceof Integer)
{
System.out.println("The variable is of type Integer");
}
}
}
Department of Computer Science and Engineering
How to Find the Type of Variable in Java? (Cont…)

2. Using getClass() Method


If you have an object and want to know its exact type at runtime, you can use the getClass() method.
Example:
public class Main {
public static void main(String[] args) {
Object obj = 123; // An Integer
System.out.println("The type of obj is: " + obj.getClass().getSimpleName());
}
}
Other Ways :
3. Using Reflection (Field.getType() for Class Fields)
4. Using getClass() with Arrays
5. Using getTypeName() for Generic Variables
6. Using Class.isPrimitive()

Department of Computer Science and Engineering


Variable

A variable in Java is a container used to store data that can change during the execution of a program.

Types of Variables:

1. Local Variable
2. Instance Variables
3. Class Variables (Static Variables)

10
Department of Computer Science and Engineering
Local Variable

• Variable declared inside a method, constructor, or block can only be used within the block they are

defined in.

Example

public void method() {

int num = 10; // Local variable

System.out.println(num);

11
Department of Computer Science and Engineering
Instance Variables

• Declared inside a class but outside any methods, constructors, or blocks.

• Each object of the class has its own copy of instance variables.

Example

public class Car {

int speed; // Instance variable

public void setSpeed(int speed) {

this.speed = speed; // Access instance variable

}
12
Department of Computer Science and Engineering
Class Variables (Static Variables)

• Declared with the static keyword inside a class.

• Shared by all instances of the class.

Example

public class Counter {

static int count = 0; // Class variable

public static void increment() {

count++;

}
13
Department of Computer Science and Engineering
Scope of Variables in Java

The scope of a variable refers to the part of the program where it can be accessed or modified.

Types of Variable Scope:

1. Local Scope

2. Instance Scope

3. Class Scope

14
Department of Computer Science and Engineering
Local Scope

Variables declared inside a method, constructor, or block are local and can only be accessed

within that method, constructor, or block.

Example:

public void exampleMethod() {

int localVar = 10; // local scope

System.out.println(localVar); // Accessible here

15
Department of Computer Science and Engineering
Instance Scope

Instance variables are accessible throughout the class but are specific to each object (instance) of
the class.
Example:
public class Person {
String name; // Instance variable
public void setName(String name) {
this.name = name; // Accessible throughout the class
}
public void printName() {
System.out.println(name); // Accessible here
}
}
Department of Computer Science and Engineering 16
Class Scope

Class variables (static variables) are shared across all

instances of the class and can be accessed using the class

name.

Example: public void printName() {

public class Example { System.out.println(name); // Accessible here

static int count = 0; // Class variable }

public static void printCount() { }

System.out.println(count); // Accessible anywhere in the class

}
17
Department of Computer Science and Engineering
Lifetime of Variables

The lifetime of a variable refers to how long it exists in memory and when it can be accessed.

Lifetime Based on Scope

1. Local Variable
2. Instance Variables
3. Class Variables (Static Variables)

18
Department of Computer Science and Engineering
Local Variable

• The lifetime of local variables is limited to the execution of the method, constructor, or block in

which they are defined.

• Once the method exits, the local variables are discarded, and their memory is released.

Example

public void myMethod() {

int x = 10; // Local variable

// x exists only within this method

19
Department of Computer Science and Engineering
Instance Variables

Instance variables exist as long as the object they belong to exists. When the object is created,

memory is allocated for its instance variables, and they are destroyed when the object is garbage

collected.

Example

public class Car {

int speed; // Instance variable

Car car = new Car(); // Instance variable exists as long as the object exists

20
Department of Computer Science and Engineering
Class Variables (Static Variables)

Class variables exist as long as the class is loaded in the JVM. They are created when the class is

loaded and destroyed when the program ends.

Example

public class Counter {

static int count = 0; // Class variable

public static void increment() {

count++;

}
21
Department of Computer Science and Engineering
Operators

In Java, operators are special symbols used to perform operations on variables and values. Java supports a
rich set of operators, which can be categorized into several types.
Here's an overview of the different types of operators in Java:
1. Arithmetic Operators
2. Unary Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Assignment Operators
7. Ternary (Conditional) Operator
8. Shift Operators
9. Instanceof Operator
Department of Computer Science and Engineering 22
1. Arithmetic Operators

Department of Computer Science and Engineering 23


2. Unary Operators

Department of Computer Science and Engineering 24


3. Relational Operators

Department of Computer Science and Engineering 25


4. Logical Operators

Department of Computer Science and Engineering 26


5. Bitwise Operators

Department of Computer Science and Engineering 27


6. Assignment Operators

Department of Computer Science and Engineering 28


7. Ternary (Conditional) Operator

variable = (condition) ? expression1 : expression2;

Department of Computer Science and Engineering 29


8. Shift Operators

Department of Computer Science and Engineering 30


Operator Precedence

Operator precedence determines


the order in which operators are
evaluated in an expression. Java
operators have a specific
precedence level and associativity
that dictates how expressions are
processed when there are multiple
operators.

Department of Computer Science and Engineering


Operator Precedence

For reference on Operator precedence you may refer to this link


https://javagoal.com/operators-in-java/

Department of Computer Science and Engineering


Type Conversion in Java

In Java, type conversion refers to changing a variable's data type to another type.
This is often necessary when performing operations between different data types.

There are two types of type conversions in Java:

• Implicit Type Conversion (Widening or Automatic Type Conversion)

• Explicit Type Conversion (Narrowing or Casting)

Department of Computer Science and Engineering 33


1. Implicit Type Conversion

Implicit type conversion happens automatically when assigning a smaller data


type to a larger data type. This is also known as widening conversion because it
converts a data type with a smaller range to one with a larger range without any
data loss.

byte → short → int → long → float → double

Department of Computer Science and Engineering 34


Example

public class ImplicitConversion {


public static void main(String[] args) {
int num = 100;
double result = num; // Implicit conversion from int to double
System.out.println("Result (double): " + result); // Output: 100.0
}
}

Department of Computer Science and Engineering 35


2. Explicit Type Conversion (Narrowing)

Explicit type conversion requires using a cast operator to convert a larger data
type to a smaller data type. This is also known as narrowing conversion because it
converts a data type with a larger range to one with a smaller range, which might
lead to data loss.

Syntax for Casting:


targetType variable = (targetType) value;

Department of Computer Science and Engineering 36


Example

public class ExplicitConversion {


public static void main(String[] args) {
double num = 100.99; // double type
int result = (int) num; // Explicit cast from double to int
System.out.println("Result (int): " + result); // Output: 100
}
}

Department of Computer Science and Engineering 37


Exercise
• Write a Java program to determine whether a person is eligible to vote. The program should take the person's
age as input and check if the age is between 18 and 120 using logical operators.

• Write a Java program to check if a given number is a power of 2 using bitwise operators. The program should
take an integer input and print whether it is a power of 2.

• Write a Java program to evaluate the following complex expression and print the result:

result = (a + b * c) / (d - e) + f % g

• Write a Java program that uses a nested ternary operator to determine the largest of three numbers and print
the result.

Department of Computer Science and Engineering

You might also like