0% found this document useful (0 votes)
3 views4 pages

Java Basics Part1 Variables Operators

The document provides an overview of Java basics, focusing on variables, data types, and operators. It covers primitive and reference data types, variable declaration, type conversion, and various operators including arithmetic, comparison, and logical operators. Additionally, it includes best practices for variable naming, declaration, and type usage.

Uploaded by

Aniket Deshpande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java Basics Part1 Variables Operators

The document provides an overview of Java basics, focusing on variables, data types, and operators. It covers primitive and reference data types, variable declaration, type conversion, and various operators including arithmetic, comparison, and logical operators. Additionally, it includes best practices for variable naming, declaration, and type usage.

Uploaded by

Aniket Deshpande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Basics - Part 1: Variables, Data Types &

Operators
Variables & Data Types
Primitive Data Types
// Numeric Types
byte b = 127; // 8-bit, -128 to 127
short s = 32767; // 16-bit, -32,768 to 32,767
int i = 2147483647; // 32-bit, -2^31 to 2^31-1
long l = 9223372036854775807L; // 64-bit, -2^63 to 2^63-1

// Floating Point
float f = 3.14f; // 32-bit
double d = 3.14159265359; // 64-bit

// Character & Boolean


char c = 'A'; // 16-bit Unicode
boolean flag = true; // true or false

Reference Data Types


String str = "Hello World";
Integer num = 42;
Double pi = 3.14159;
Boolean isTrue = true;

Variable Declaration
// Single declaration
int age = 25;

// Multiple declaration
int x = 1, y = 2, z = 3;

// Constants (final)
final double PI = 3.14159;
final String COMPANY_NAME = "TechCorp";

Type Conversion
// Implicit conversion (widening)
int intValue = 100;
long longValue = intValue; // int to long
float floatValue = longValue; // long to float

1
// Explicit conversion (narrowing)
double doubleValue = 3.14159;
int intFromDouble = (int) doubleValue; // 3
long longValue2 = 1000L;
int intFromLong = (int) longValue2; // 1000

// String conversion
String numberStr = "42";
int number = Integer.parseInt(numberStr);
double decimal = Double.parseDouble("3.14");
String backToString = String.valueOf(number);

Operators
Arithmetic Operators
int a = 10, b = 3;

int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1

// Increment/Decrement
int x = 5;
x++; // Post-increment: 5, then 6
++x; // Pre-increment: 7
x--; // Post-decrement: 7, then 6
--x; // Pre-decrement: 5

Assignment Operators
int x = 10;
x += 5; // x = x + 5
x -= 3; // x = x - 3
x *= 2; // x = x * 2
x /= 4; // x = x / 4
x %= 3; // x = x % 3

Comparison Operators
int a = 5, b = 10;

boolean equal = (a == b); // false

2
boolean notEqual = (a != b); // true
boolean lessThan = (a < b); // true
boolean greaterThan = (a > b); // false
boolean lessEqual = (a <= b); // true
boolean greaterEqual = (a >= b); // false

Logical Operators
boolean x = true, y = false;

boolean and = x && y; // false (AND)


boolean or = x || y; // true (OR)
boolean not = !x; // false (NOT)

// Short-circuit evaluation
boolean result = x && expensiveOperation(); // expensiveOperation() not called if x is fals

Bitwise Operators
int a = 5, b = 3; // 5 = 101, 3 = 011

int and = a & b; // 1 (001)


int or = a | b; // 7 (111)
int xor = a ^ b; // 6 (110)
int not = ~a; // -6
int leftShift = a << 1; // 10 (1010)
int rightShift = a >> 1; // 2 (10)
int unsignedRightShift = a >>> 1; // 2 (10)

Operator Precedence
// Highest to lowest precedence:
// 1. Postfix: expr++, expr--
// 2. Unary: ++expr, --expr, +expr, -expr, ~, !
// 3. Multiplicative: *, /, %
// 4. Additive: +, -
// 5. Shift: <<, >>, >>>
// 6. Relational: <, <=, >, >=, instanceof
// 7. Equality: ==, !=
// 8. Bitwise AND: &
// 9. Bitwise XOR: ^
// 10. Bitwise OR: |
// 11. Logical AND: &&
// 12. Logical OR: ||
// 13. Conditional: ? :
// 14. Assignment: =, +=, -=, *=, /=, %=, etc.

3
// Example with precedence
int result = 2 + 3 * 4; // 14 (not 20)
int result2 = (2 + 3) * 4; // 20

Quick Reference
Data Type Ranges
• byte: -128 to 127
• short: -32,768 to 32,767
• int: -2,147,483,648 to 2,147,483,647
• long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
• float: ~3.4E-38 to ~3.4E+38
• double: ~1.7E-308 to ~1.7E+308
• char: 0 to 65,535 (Unicode)
• boolean: true or false

Common Operators Summary


• Arithmetic: +, -, *, /, %, ++, –
• Assignment: =, +=, -=, *=, /=, %=
• Comparison: ==, !=, <, <=, >, >=
• Logical: &&, ||, !
• Bitwise: &, |, ^, ~, «, », »>

Best Practices
1. Use meaningful variable names
2. Declare variables close to their usage
3. Use final for constants
4. Be careful with type conversions
5. Use parentheses to clarify operator precedence
6. Use appropriate data types for memory efficiency
7. Initialize variables when declaring them
8. Use camelCase naming convention
9. Avoid magic numbers (use constants)
10. Be aware of integer division vs floating-point division

You might also like