这篇是从国外一篇blog翻译过来,虽然文章里面的个人看法有些偏激,但是确实有很多Python和Java的语法比较的内容,很适合像我这样搞java转Python的,权当是Javaer到Pythoner的快速入门吧:)
?
-----------------------------------------------------------------------------------------------------------------------
?
?
Java VS Python 生产效率 -概述 
下面是3个主要的特性能够让Python程序员效率高于java程序员
Java
Python
静态类型?
?
?
?
?
?
?
?
?
?
“hello world!”
Java
Python
public class HelloWorld
{
    public static void main (String[] args)
    {
        System.out.println("Hello, world!");
    }
}
print "Hello, world!"
print("Hello, world!") # Python version 3
?
?
?
?
?
?
?
例子
下面的例子里,我们初始化integer为0,再把他转为string,最后检查他是否为空。注意,数据的声明在java里是必须的,但Python里不是。还会注意到即时只是在简单的比较两个字符串的时候,java代码还是会显得冗长。
Java
Python
int    myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...
myCounter = 0 myString = str(myCounter) if myString == "0": ...
// print the integers from 1 to 9
for (int i = 1; i < 10; i++)
{
   System.out.println(i);
}
#print the integers from 1 to 9
for i in range(1,10):
    print i
?
?
?
?
例子
你的应用有15个class。(准确的说是15个顶层的public class)。
Java
Python
每个顶层的public class都必须定义一个单独的文件。如果你的应用有15个class,那就有15个文件。 多个class可以被定义到单个文件中。如果你应用有15个class,只要你愿意甚至可以将整个应用都存放到一个文件中,但是更明智的情况是将这些class分散到4-6个文件中。?
?
?
?
?
例子
在你的应用中,A方法调B方法,B调C,C调D,D调E,E调F。当你发现F必须抛出SpecialException异常,它必须被A捕获时。
Java
Python
你必须在F中throw SpecialException,然后在A中进行catch。这个原因还是因为java事实上是封闭的面向对象程序语言,使用了checked exception --- 在异常出现的每个方法中,他们必须被抛出或捕获,否则连编译都无法通过。
?
?
?
?
?
?
例子
你的应用里面有个Employee class,当一个Employee的实例被创建,构造函数可能传入1个、2个或者3个参数。
如果是在java中,这意味着你必须写3个不同参数的构造函数。如果你用Python,你只用写一个带可选参数和默认值的构造函数。
Java
Python
public class Employee
{
    private String myEmployeeName;
    private int    myTaxDeductions = 1;
    private String myMaritalStatus = "single";
    //--------- constructor #1 -------------
    public Employee(String EmployeName)
    {
        this(employeeName, 1);
    }
    //--------- constructor #2 -------------
    public Employee(String EmployeName, int taxDeductions)
    {
       this(employeeName, taxDeductions, "single");
    }
    //--------- constructor #3 -------------
    public Employee(String EmployeName,
           int taxDeductions,
           String maritalStatus)
    {
       this.employeeName    = employeeName;
       this.taxDeductions   = taxDeductions;
       this.maritalStatus   = maritalStatus;
    }
...
class Employee():
    def __init__(self,
        employeeName
        , taxDeductions=1
        , maritalStatus="single"
        ):
        self.employeeName    = employeeName
        self.taxDeductions   = taxDeductions
        self.maritalStatus   = maritalStatus
...
?
?
?
?
?
?
?
?
?
例子
关于紧凑(注:略去”名人名言“若干)
Java
Python
import java.io.*;
...
BufferedReader myFile =
    new BufferedReader(
        new FileReader(argFilename));
# open an input file myFile = open(argFilename)
?
?
?
?
?
?
?
?
?
例子
string处理能力的比较
功能
Java
Python
去掉头尾空格 s.trim() s.strip() 去掉头部空格 未提供 s.lstrip() 去掉尾部空格 未提供 s.rstrip()?
?
?
?
?
?
?
?
例子
将int加到Vector
Java
Python
public Vector<Integer> aList = new Vector<Integer>; public int aNumber = 5; public int anotherNumber; aList.addElement(aNumber); anotherNumber = aList.getElement(0);
aList = [] aNumber = 5 aList.append(aNumber) anotherNumber = aList[0]
?
?
?
?
?
?
?
?
?
例子
Java
Python
if ( a > b )
{
    a = b;
    b = c;
}
if  a > b :
    a = b
    b = c
?
?
原文地址:http://pythonconquerstheuniverse.wordpress.com/2009/10/03/python-java-a-side-by-side-comparison/
 相关文章
                            相关文章