Variables_DataTypes_Operators
Variables_DataTypes_Operators
1. Local Variables
Explanation:
Can only be accessed within the block where they are declared.
obj.calculateSum();
}
}
Output:
Sum: 15
2. Instance Variables
Explanation:
Default values are assigned if not initialized (0 for int, null for objects, etc.).
Example:
obj.displayAge();
}
Output:
Age: 25
3. Static Variables
Explanation:
StaticVariableExample.displayCompany();
}
Output:
Example:
obj.methodWithLocalVariable();
}
Output:
Local Variable: 30
Instance Variable: 10
Static Variable: 20
}
Output:
Integer: 10
Float: 5.5
Character: A
Boolean: true
Reference Variables Example:
System.out.print("Numbers: ");
}
Output:
Explanation:
}
Output:
Speed Limit: 60
int num;
}
}
Default Values of Instance and Static Variables:
int intVar;
float floatVar;
boolean boolVar;
String stringVar;
public static void main(String[] args) {
}
Output:
Default int: 0
1. Local variables must be initialized before use, and they have a short lifetime.
4. Constants are declared with the final keyword and cannot be modified.
5. Default values are assigned only to instance and static variables, not local
variables.
Data Types
Data types specify the type of data a variable can hold. In Java, data types are
categorized into two main types:
1. Primitive Data Types
o Basic building blocks of data manipulation.
2. Reference Data Types
Overview
Primitive types are predefined by Java and named by reserved keywords. They store
simple values directly in memory.
List of Primitive Data Types
1. Integer Types
}
Output:
Byte: 100
Short: 30000
Int: 100000
Long: 1000000000
2. Floating-Point Types
}
}
Output:
Float: 3.14
Double: 2.71828
3. Character Type
}
Output:
Character: A
Unicode Character: A
4. Boolean Type
}
Output:
Overview
Reference data types are used to store references (memory addresses) to objects or
arrays.
Common Reference Data Types
1. Strings
System.out.println(greeting);
}
}
Output:
Hello, World!
2. Arrays
System.out.println("Array Elements:");
for (int num : numbers) {
}
Output:
Array Elements:
10 20 30 40
3. Objects
String color;
this.color = color;
}
Output:
The color is: Red
char \u0000
boolean false
Example:
int defaultInt;
float defaultFloat;
boolean defaultBoolean;
String defaultString;
Type Casting
1. Implicit Casting (Widening Conversion): Automatic conversion from smaller to
larger type.
2. Explicit Casting (Narrowing Conversion): Manual conversion from larger to
smaller type.
Example:
// Implicit Casting
// Explicit Casting
double decimal = 9.78;
}
Output:
Explicit Casting: 9
Assignments to Practice
1. Declare variables of each primitive type and print their default values.
1. Arithmetic Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Unary Operators
7. Ternary Operator
8. Shift Operators
1. Arithmetic Operators
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus (Remainder) a % b
Example:
int a = 10, b = 3;
System.out.println("Addition: " + (a + b));
== Equal to a == b
!= Not equal to a != b
Example:
3. Logical Operators
` `
Example:
4. Bitwise Operators
` ` Bitwise OR
~ Bitwise Complement ~a
Example:
5. Assignment Operators
= Assign a=5
int a = 10;
a += 5; // a = a + 5
a *= 2; // a = a * 2
6. Unary Operators
+ Positive +a
- Negative -a
! Logical NOT !a
Example:
int a = 10;
System.out.println("a: " + a);
7. Ternary Operator
Example:
System.out.println(result);
}
}
8. Shift Operators
Example:
Practice Assignments
Java provides powerful and flexible libraries for input and output operations. These
operations are categorized into different streams to read from and write to various data
sources like files, consoles, and network connections.
3. Standard Streams
Java has three standard streams for console I/O:
1. Console Input/Output
import java.util.Scanner;
}
Explanation:
Using System.out:
System.out.print("Java Programming.");
Arithmetic operations are fundamental in Java and allow you to perform basic
mathematical operations such as addition, subtraction, multiplication, division, and
finding the remainder (modulus).
import java.util.Scanner;
System.out.println("\nResults:");
if (num2 != 0) {
scanner.close();
1. Importing Scanner:
o The Scanner class is imported from java.util to allow user input from the
console.
2. Input from the User:
o Division and modulus operations are only performed if the second number
is non-zero. If zero is entered, an appropriate message is displayed.
5. Output:
Input:
Results:
Addition: 18.0
Subtraction: 12.0
Multiplication: 45.0
Division: 5.0
Modulus: 0.0
Input:
Results:
Addition: 10.0
Subtraction: 10.0
Multiplication: 0.0
Practice Assignments
Swapping variables means exchanging their values. In Java, there are several ways to
achieve this. Below are two common methods with detailed explanations and code
examples:
int a = 5;
int b = 10;
System.out.println("Before Swap:");
// Swapping logic
int temp = a; // Store the value of 'a' in a temporary variable
System.out.println("\nAfter Swap:");
}
Explanation
1. Initial Values:
o a = 5 and b = 10.
2. Temporary Variable:
int a = 5;
int b = 10;
System.out.println("Before Swap:");
// Swapping logic
a = a + b; // Step 1: Add both numbers
b = a - b; // Step 2: Subtract 'b' from the result to get the original 'a'
a = a - b; // Step 3: Subtract the new 'b' from the result to get the original 'b'
System.out.println("\nAfter Swap:");
}
Explanation
1. Initial Values:
o a = 5 and b = 10.
2. Step-by-Step Swap:
o b = a - b: Now a = 15 and b = 5.
o a = a - b: Now a = 10 and b = 5.
3. No Temporary Variable:
This method works for integer data types and uses bitwise XOR.
Code Example
int b = 10;
System.out.println("Before Swap:");
System.out.println("a = " + a + ", b = " + b);
// Swapping logic
a = a ^ b; // Step 3: XOR the result with new 'b' to get original 'b'
System.out.println("\nAfter Swap:");
}
Explanation
1. Initial Values:
o a = 5 and b = 10.
2. Step-by-Step XOR Operations:
o b = a ^ b: Now a = 15 and b = 5.
o a = a ^ b: Now a = 10 and b = 5.
3. Use Case:
o Efficient for integer swapping, but not suitable for floating-point numbers.
Practice Assignments