To Documents

A Python to Java Dictionary

A copy of this dictionary will be provided for the final exam.

Meaning Python Java
Spelling for class names Upper Camel Casing Upper Camel Casing
Spelling for variable names Underscore Notation or
Lower Camel Casing
(but be consistant)
Lower Camel Casing
Comment #
//
/* ... */
Addition, concatenation + +
Subtraction - -
Multiplication * *
Division / /
Mod (Remainder from
integer division)
% %
Is equal? == ==
Is greater to? > >
Is less than? < <
Is greater than or equal to? >= >=
Is less than or equal to? <= <=
Is not equal to? != !=
Logical and and &&
Logical or or ||
Assignment operators = += -= *= /= = += -= *= /=
Increment operator n += 1 n++
Decrement operator n -= 1 n--
Print to standard out
(screen)
print(n)
print(n, end=" ")
System.out.println(n);
System.out.print(n);
Scanner Import Statement import java.util.Scanner;
Read from standard in
(keyboard)
t = input("Enter a string:") Scanner s =
    new Scanner(System.in);
t = s.nextLine( );
Import Statements for
File IO
import java.util.Scanner;
import java.io.*;
Read from file fin = open("input.txt")
line = fin.readline( )
while line != "":
    ...
    line = fin.readline( )
Scanner s = new Scanner(
    new File("input.txt"));
while(s.hasNextLine( ))
{
    t = s.nextLine( );
    ...
}
Exit from program import sys
sys.exit( )
System.exit(0);
Import external module import sqlite3
from random import randrange
import java.util.Scanner;
import java.util.Random;
import java.io.*;
Convert to integer n = int(s) int n = Integer.parseInt(s);
Convert to floating point x = float(s) double n =
    Double.parseDouble(s);
If..else statements if condition1:
    action1
elif condition2:
    action2
else:
   action3

if (condition1)
{
     action1;
}
else if (condition2)
{
     action2;
}
else
{
     action3;
}
While statement n = 1
while n <= 10:
    print(n, end=" ")
    n += 1;
int n = 1;
while (n <= 10)
{
     System.out.print(
         n + " ");
     n++;
}
Traditional for statement for n in range(0, 10):
  print(n, end=" ")
for(int n = 0; n <= 10; n++)
{
     System.out.print(
         n + " ");
}
Short array definition a = [2, 5, 3, 4] int[ ] a = {2, 5, 3, 4};
Long array definition a = Array( )
a.append(2)
...
a.append(4)
int[ ] a = int[4];
a[0] = 2;
...
a[999] = 4;
Array access n = a[4]
a[4] = 352
n = a[4];
a[4] = 352;
Modern for each statement
(Access array by index)
for n in a:
  print n, " "
for(int n : a)
{
    System.out.print(
        n + " ");
}
Class definition class Card:
  ...

public class Card
{
     ...
}
Instance variable
declarations
Instance variables not
declared. They are
prefixed by self:
self.rank   self.suit
public int rank;
public String suit;
Constructor definition # Constructor is created by
# compiler, but calls
# __init__ method:

def __init__(self, r, s)
   rank = r
   suit = s
end
public Card(int r, String s)
{
    rank = r;
    suit = s;
}
Constructor invocation c = Card(r, s) Card c = new Card(r, s);
toString method definition def __init__(self):
    return "{self.name}" + \
             {self.age}"
public String toString( )
{
    return name + " " + age;
}
toString method invocation p = Person("Alice", 11)
print(str(p))
# or
print(p)
Person p = new
    Person("Alice", 11);
System.out.println(p.
    toString( ));
// or
System.out.println(p);
Instance Method Definition def have_birthday(self):
    self.age += 1;
public void haveBirthday( )
{
    age++;
}
Instance method invocation p = Person("Alice", 11)
p.have_birthday( )
Person p = new
    Person("Alice", 11);
p.haveBirthday( );