Variables and Data Types
Jython uses the same Python data types you know - int, float, string, list, dict, tuple, boolean - with the same dynamic typing: variables are names bound to values, and types are a property of values, not declarations. On top of that, Java types are first-class values you can create and manipulate.
Python Data Types
x = 42 # int
y = 3.14 # float
s = "Skillversitas" # str
t = (1, 2, 3) # tuple
lst = [1, 2, 3] # list
d = {"a": 1} # dict
flag = True # bool
print(type(x), type(s), type(lst))
Conversion Functions
print(int("42")) # 42
print(float("3.14")) # 3.14
print(str(42)) # "42"
print(int("0xFF", 16)) # 255
print(hex(255)) # 0xff
Java Types in Jython
Instantiate Java classes with their constructors and call methods normally:
import java.lang
i = java.lang.Integer(42)
print(i.intValue())
print(i + 1) # Jython auto-converts to Python int arithmetic
s = java.lang.String("hello")
print(s.length()) # Java method call
# strings interoperate: Python str and java.lang.String convert automatically
print(len(s))
Number Interoperability
| Python | Java | Notes |
|---|---|---|
| int | int, long, Integer, Long | auto-converted; overflow follows Java when called into Java |
| float | double, Double, float | auto-converted |
| bool | boolean, Boolean | True/False map directly |
Variable Rules (Same as Python)
- No declarations: x = 10 binds the name.
- Names are case-sensitive; start with a letter or underscore.
- Assignment creates or rebinds; there are no type annotations (Jython 2.7 has none).
- Use help(type) and dir() to inspect any object, Python or Java.
Summary
Jython variables follow normal Python rules - dynamic typing, no declarations, conversion via int/float/str - and additionally accept Java objects like java.lang.Integer and java.lang.String as first-class values. Numbers and strings interoperate automatically between the two type systems, which is what makes mixing Python and Java painless.