Unit-1 Data Types and Operators.pptx to computers

csangani1 3 views 37 slides Mar 04, 2025
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

Data types are ways to group data values, and operators are symbols that perform actions on data


Slide Content

Data Types and Operators ‹#› Department of Computer Science and Engineering Mr Dasari Siva Krishna Assistant Professor, Dept. of Computer Science And Engineering GITAM School of Technology

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 Department of Computer Science and Engineering ‹#›

Department of Computer Science and Engineering ‹#› Data Types and Sizes 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 data types:  The primitive data types include boolean, char, byte, short, int, long, float and double. Non-primitive data types:  The non-primitive data types include  Strings , Classes ,  Interfaces , and  Arrays etc.

Department of Computer Science and Engineering ‹#› Primitive and Non-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. boolean data type byte data type char data type short data type int data type long data type float data type double data type

Department of Computer Science and Engineering ‹#› Data Types and Its Sizes

Department of Computer Science and Engineering ‹#› 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)"); } }

How to Find the Type of Variable in Java? 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 : Using Reflection (Field.getType() for Class Fields) Using getClass() with Arrays Using getTypeName() for Generic Variables 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: Local Variable Instance Variables Class Variables (Static Variables) Department of Computer Science and Engineering ‹#›

Local 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); } 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 } } 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++; } } 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: Local Scope Instance Scope Class Scope 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 } 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 ‹#›

Class Scope Class variables (static variables) are shared across all instances of the class and can be accessed using the class name. Example: public class Example { static int count = 0; // Class variable public static void printCount() { System.out.println(count); // Accessible anywhere in the class } } public void printName() { System.out.println(name); // Accessible here } } 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 Local Variable Instance Variables Class Variables (Static Variables) 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 } 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 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++; } } Department of Computer Science and Engineering ‹#›

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: Arithmetic Operators Unary Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Ternary (Conditional) Operator Shift Operators Instanceof Operator

Department of Computer Science and Engineering ‹#› 1. Arithmetic Operators

Department of Computer Science and Engineering ‹#› 2. Unary Operators

Department of Computer Science and Engineering ‹#› 3. Relational Operators

Department of Computer Science and Engineering ‹#› 4. Logical Operators

Department of Computer Science and Engineering ‹#› 5. Bitwise Operators

Department of Computer Science and Engineering ‹#› 6. Assignment Operators

Department of Computer Science and Engineering ‹#› 7. Ternary (Conditional) Operator variable = (condition) ? expression1 : expression2;

Department of Computer Science and Engineering ‹#› 8. Shift Operators

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

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 ‹#› 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 ‹#› 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 ‹#› 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 ‹#› 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 } }

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. Exercise Department of Computer Science and Engineering

Thank You Department of Computer Science and Engineering