Data Types andOperators 1 Department of Computer Science and Engineering Mr Dasari Siva Krishna Assistant Professor, Dept. of Computer Science And Engineering GITAM School of Technology
2.
Literals in Java InJava, 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 2
3.
Department of ComputerScience and Engineering 3 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.
4.
Department of ComputerScience and Engineering 4 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 ComputerScience 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)"); } }
7.
How to Findthe 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
8.
How to Findthe 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
9.
Variable A variable inJava 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) Department of Computer Science and Engineering 9
10.
Local Variable • Declaredinside 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 10
11.
Instance Variables • Declaredinside 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 11
12.
Class Variables (StaticVariables) • 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 12
13.
Scope of Variablesin 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 Department of Computer Science and Engineering 13
14.
Local Scope Variables declaredinside 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 14
15.
Instance Scope Instance variablesare 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 15
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 class Example { static int count = 0; // Class variable public static void printCount() { System.out.println(count); // Accessible anywhere in the class } } Department of Computer Science and Engineering 16
17.
Lifetime of Variables Thelifetime 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) Department of Computer Science and Engineering 17
18.
Local Variable • Thelifetime 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 18
19.
Instance Variables Instance variablesexist 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 19
20.
Class Variables (StaticVariables) 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 20
21.
Department of ComputerScience and Engineering 21 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
Operator Precedence Operator precedencedetermines 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
31.
Department of ComputerScience and Engineering 31 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)
32.
Department of ComputerScience and Engineering 32 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
33.
Department of ComputerScience and Engineering 33 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 } }
34.
Department of ComputerScience and Engineering 34 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;
35.
Department of ComputerScience and Engineering 35 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 } }
36.
• Write aJava 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