Appendix E. Java-to-Python Quick Reference

This is a quick reference guide to the syntactic differences between Java and Python. It is not intended to provide full information on semantic differences between the two languages.

Basic Data Types

Python has a more limited set of numerical types than Java, but otherwise the two languages are similar. Python does not perform automatic conversions between types, however each basic type has a built-in function of the same name that attempts to convert the argument to that type.

Java

Python

Comment

boolean

 

Python has no specific Boolean type. False is represented by 0, and by any empty string, sequence, or mapping.

char

string

A character is just a string of length 1.

byte, short, int

int

 

long

long

A Python long has unbounded length.

float, double

float

Python floats have the same width as Java doubles.

Advanced Data Types

Python’s more advanced built-in types have more functionality and flexibility built into them than their Java equivalents.

Java

Python

Comment

java.lang.String

string

 

java.util.List

list

 

java.util.HashMap

dictionary

 

java.io.File

file

Python files contain much of the functionality of Java I/O streams.

Logical Operators

Python tends to use English words for its logical operators rather than symbols. Python has no equivalent to the Java operators ^ (binary XOR) and ?: (ternary conditional operator).

Java

Python

Comment

&&

and

Python operator always short circuits.

||

or

Python operator always short circuits.

!

not

 

==

is

Test for identical object identity.

equals( ...

Get Jython Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.