Data Types and Operators
Data Types and Operators
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:
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:
boolean Strings
char Classes
byte Interfaces
short Arrays
int
long
float and double
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)");
}
}
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
System.out.println(num);
11
Department of Computer Science and Engineering
Instance Variables
• Each object of the class has its own copy of instance variables.
Example
}
12
Department of Computer Science and Engineering
Class Variables (Static Variables)
Example
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.
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
Example:
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
name.
}
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.
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
• Once the method exits, the local variables are discarded, and their memory is released.
Example
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
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
Example
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
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.
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.
• 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.