Variables, Data Types, and Operators
In this lesson, we’ll explore how Java handles variables, data types, and operators, comparing them to their Python counterparts. We’ll see how Java’s static typing differs from Python’s dynamic typing and learn about Java’s primitive types and type inference.
Variables and Data Types
Static Typing vs. Dynamic Typing
One of the most significant differences between Java and Python is their typing systems. Python uses dynamic typing, where variables can change types at runtime. Java, on the other hand, uses static typing, where variable types are declared explicitly and checked at compile-time.
Java’s Primitive Types
Java has eight primitive data types, which are not objects and are stored directly in memory. These are:
byte
: 8-bit signed two’s complement integershort
: 16-bit signed two’s complement integerint
: 32-bit signed two’s complement integerlong
: 64-bit signed two’s complement integerfloat
: 32-bit IEEE 754 floating-pointboolean
: true or falsechar
: 16-bit Unicode characterdouble
: 64-bit IEEE 754 floating-point
Here’s how you declare and initialize variables of these types:
// Java
byte b = 100;
short s = 10000;
int i = 100000;
long l = 1000000L; // Note the 'L' suffix for long literals
float f = 3.14f; // Note the 'f' suffix for float literals
double d = 3.14159;
boolean bool = true;
char c = 'A';
In Python, you don’t need to specify types, and there’s no distinction between primitive types and objects:
# Python
i = 100000
f = 3.14
b = True
c = 'A'
Reference Types
In addition to primitive types, Java has reference types, which are essentially objects. The most commonly used reference type is String
:
// Java
String greeting = "Hello, World!";
This is similar to Python, where strings are also objects:
# Python
greeting = "Hello, World!"
Type Inference with ‘var’ (Java 10+)
Starting from Java 10, you can use the var
keyword for local variable declarations with inferred types:
// Java
var x = 5; // x is inferred to be an int
var y = "Hello"; // y is inferred to be a String
This makes Java code look more similar to Python, but remember that the types are still static and determined at compile-time.
Declaring and Initializing Variables
In Java, you must declare a variable’s type before using it:
// Java
int age; // Declaration
age = 30; // Initialization
// Or combined:
int height = 180;
In Python, you don’t need to declare variables before using them:
# Python
age = 30
height = 180
Operators
Java and Python share many similar operators, but there are some differences to be aware of.
Arithmetic Operators
Both Java and Python have the standard arithmetic operators:
// Java
int a = 10, b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b; // Integer division: 3
int remainder = a % b;
In Python:
# Python
a, b = 10, 3
sum = a + b
difference = a - b
product = a * b
quotient = a / b # Float division: 3.3333...
integer_quotient = a // b # Integer division: 3
remainder = a % b
Note that in Java, division between two integers always results in an integer (truncating any decimal part). In Python, the /
operator always performs float division, while //
performs integer division.
Comparison Operators
Comparison operators are similar in both languages:
// Java
boolean isEqual = (a == b);
boolean isNotEqual = (a != b);
boolean isGreater = (a > b);
boolean isLess = (a < b);
boolean isGreaterOrEqual = (a >= b);
boolean isLessOrEqual = (a <= b);
In Python:
# Python
is_equal = a == b
is_not_equal = a != b
is_greater = a > b
is_less = a < b
is_greater_or_equal = a >= b
is_less_or_equal = a <= b
Logical Operators
Java uses &&
for logical AND, ||
for logical OR, and !
for logical NOT:
// Java
boolean x = true, y = false;
boolean andResult = x && y;
boolean orResult = x || y;
boolean notResult = !x;
Python uses and
, or
, and not
:
# Python
x, y = True, False
and_result = x and y
or_result = x or y
not_result = not x
Bitwise Operators
Both Java and Python support bitwise operators:
// Java
int a = 5, b = 3;
int bitwiseAnd = a & b;
int bitwiseOr = a | b;
int bitwiseXor = a ^ b;
int bitwiseComplement = ~a;
int leftShift = a << 1;
int rightShift = a >> 1;
int unsignedRightShift = a >>> 1; // Java-specific
In Python:
# Python
a, b = 5, 3
bitwise_and = a & b
bitwise_or = a | b
bitwise_xor = a ^ b
bitwise_complement = ~a
left_shift = a << 1
right_shift = a >> 1
Note that Python doesn’t have a separate unsigned right shift operator.
Type Conversion and Casting
In Java, you sometimes need to explicitly cast between types:
// Java
int i = 10;
long l = i; // Implicit conversion (widening)
int j = (int) l; // Explicit casting (narrowing)
double d = 3.14;
int k = (int) d; // Explicit casting, truncates to 3
In Python, type conversion is typically done using type constructor functions:
# Python
i = 10
f = float(i) # Convert to float
s = str(i) # Convert to string
d = 3.14
j = int(d) # Converts to 3
Conclusion
In this lesson, we’ve covered the basics of variables, data types, and operators in Java, comparing them to their Python equivalents. We’ve seen how Java’s static typing system differs from Python’s dynamic typing, explored Java’s primitive types, and looked at various operators and type conversion techniques.
In the next lesson, we’ll dive into control structures and functions in Java, exploring how they compare to Python’s constructs and introducing some Java-specific concepts like method overloading.