Java vs. Python (2): Data Types

If you know Java and want to quickly get a sense of how to use Python from the very beginning, the following summary can provide you a quick review of data types. You may also find the previous comparison of Java and Python useful.

By comparing data types between Java and Python, you can get the difference and start using Python quickly. Comparision also can also help developers understand the common concepts shared by different programming languages.

Apparently, Java has more data types/structures than Python, so I will list the most similar concept from Java for the corresponding Python data types.

1. Strings

Java Python
String city = "New York";
String state = "California";//has to be " not '
String lines = "multi-line " +
# Strings
city = "New York"
state = 'California'
# multi-line string
lines = """multi-line
moreLines = '''multi-line

In python, string can reside in a pair of single quotes as well as a pair of double quotes. It supports multiplication: "x"*3 is "xxx".

2. Numbers

Java Python
//integer numbers
int num = 100;
//floating point numbers
float f = 1.01f; 
//float f = 1.01;//wrong!
double d = 1.01;
# integer numbers
num = 100
num = int("100")
# floating point numbers
f = 1.01
f = float("1.01")
# null
spcial = None

In Java, when you type something like 1.01, its interpreted as a double.
Double is a 64-bit precision IEEE 754 floating point, while float is a 32-bit precision IEEE 754 floating point.
As a float is less precise than a double, the conversion cannot be performed implicitly.

3. Null

Java Python
Object special = null;
# null
spcial = None

4. Lists

Java Python
//arraylist is closest with list in python
ArrayList<Integer> aList = 
  new ArrayList<Integer>();
int n = aList.get(0);
//get sub list
List<Integer> subList = 
  aList.subList(0, 2);
//1, 3
aList = []
aList = [1, 'mike', 'john']
# extend
print aList
#[1, 'mike', 'john', 2, 'new', 'list']
aList = [0,1,2,3,4,5,6]
# size
print len(aList)
print aList[2]
print aList[0:3]
#[0, 1, 2]
print aList[2:]
#[2, 3, 4, 5, 6]
print aList[-2]
#lists are mutable
aList[0] = 10
print aList
#[10, 1, 2, 3, 4, 5, 6]

5. Tuples

Java Python
No tuples in Java.
aTuple = ()
aTuple = (5) # cause error
aTuple = (5,)
print aTuple
print aTuple[0]

In Python, tuples are similar with lists, and the difference between them is that tuple is immutable. That means methods that change lists' value can not be used on tuples.

To assign a single element tuple, it has to be: aTuple = (5,). If comma is removed, it will be wrong.

6. Sets

Java Python
HashSet<String> aSet = new HashSet<String>();
//iterate over set
Iterator<String> iterator = aSet.iterator();
while (iterator.hasNext()) {
	System.out.print( + " ");
HashSet<String> bSet = new HashSet<String>();
//check if bSet is a subset of aSet
boolean b = aSet.containsAll(bSet);
//union - transform aSet 
//into the union of aSet and bSet
//intersection - transforms aSet 
//into the intersection of aSet and bSet
//difference - transforms aSet 
//into the (asymmetric) set difference
// of aSet and bSet. 
aSet = set()
aSet = set("one") # a set containing three letters
#set(['e', 'o', 'n'])
aSet = set(['one', 'two', 'three'])
#set(['three', 'two', 'one'])
#a set containing three words
#iterate over set
for v in aSet:
    print v
bSet = set(['three','four', 'five'])
cSet = aSet | bSet
#set(['four', 'one', 'five', 'three', 'two'])
dSet = aSet & bSet
#find elements in aSet not bSet
eSet = aSet.difference(bSet)
#add element
#set(['four', 'six', 'five', 'three'])

7. Dictionaries

Dictionaries in Python is like Maps in Java.

Java Python
HashMap<String, String> phoneBook = 
                        new HashMap<String, String>();
phoneBook.put("Mike", "555-1111");
phoneBook.put("Lucy", "555-2222");
phoneBook.put("Jack", "555-3333");
//iterate over HashMap
Map<String, String> map = new HashMap<String, String>();
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key = " + entry.getKey() +
      ", Value = " + entry.getValue());
//get key value
//get all key
Set keys = phoneBook.keySet();
//get number of elements
//delete all elements
//delete an element
#create an empty dictionary
phoneBook = {}
phoneBook = {"Mike":"555-1111", 
#iterate over dictionary
for key in phoneBook:
    print(key, phoneBook[key])
#add an element
phoneBook["Mary"] = "555-6666"
#delete an element
del phoneBook["Mike"]
#get number of elements
count = len(phoneBook)
#can have different types
phoneBook["Susan"] = (1,2,3,4)
#return all keys
print phoneBook.keys()
#delete all the elements

More about Java Collections:

Category >> Basics >> Python >> Versus  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Many of your doubts regarding data types in Core Java can be cleared through Merit Campus, visit:

    Not only data types, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: to know more.

  • Alejandro

    From a most impartial point of view, mine, since I’m deciding on learning both Java and Python (for work reasons), I can say that Python seems to make way more sense in that the code is always shorter and way easier to read.