[Tutor] Differnce between java and python (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Dec 22 19:14:52 CET 2005


[Keeping tutor at python.org in CC.  Ok, so now we know that Shivayogi
already has Java experience.

Shivayogi might also be a little annoyed that he has to learn a new
language.  We have to tread more carefully in making claims like "Python
is easier than Java", because for anyone starting on another language, the
language we already know is obviously easier than the one we are trying to
learn.  *grin*]


Hi Shivayogi,

There are a few nice things about Python that make common tasks easier to
do.  We can go through a few examples, and if you have questions on them
or would like to see more examples, please ask and folks on the list will
be happy to talk with you.  (Please make sure to keep tutor at python.org in
CC by using your email client's "Reply to All"  feature.)


For example, Java has an "iterator" protocol, but until Java 1.5, it
didn't have much syntactic support.  As an example, writing loops to go
across iterators involved manually next()ing the iterable and calling
hasNext():


/*** Java ***/
import java.util.*;

// ... within some method body

List words = Arrays.asList(new String[] {"hello", "world",
                                         "this", "is",
                                         "a", "test"});
for (Iterator iter = words.iterator(); iter.hasNext(); ) {
    String word = (String) iter.next();
    System.out.println(word.toUpperCase());
}
/******/


Python's for loop, on the other hand, natively works across iterables: the
'for' loop itself is responsible for doing things like 'next()':

### Python ###
words = ["hello", "world", "this", "is", "a", "test"]
for word in words:
    print word.upper()
######

I understand that Java's for loop finally has iterator support, but from
the examples I've seen, it looks a bit syntactically heavyweight.


Another casual example where Python and Java differ is the treatment of
common types.  Python doesn't have a notion of "primitive" types, whereas
in Java, we sometimes have to think about 'int' vs. Integer.

For example, we know that the Java method:

/******/
int double(int x) {
    return x * 2;
}
/******/

breaks on large input because the declared types are primitive ints.  To
make this really work, we have to use BigIntegers:

/******/
BigInteger double(BigInteger x) {
    return x.multiply(new BigInteger("2"));
}
/******/


On the other hand, Python integers also automatically promote themselves
to bignums rather than overflow, so the function:

######
def double(x):
    return x * 2
######

just works.


Does this make sense so far?  Please feel free to ask more questions.
There's also a book by Bruce Tate called "Beyond Java" that talks about
these issues.

Best of wishes!



---------- Forwarded message ----------
Date: Thu, 22 Dec 2005 10:40:13 +0530
From: shivayogi kumbar <shivayogiks at gmail.com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Differnce between java and python

Sir,
         I hve done my MSc(c.s).I did my project on Java.Know I have joined
the company where they work on Python.So It has forced to me learn
PYthon.SoI have approaced you
people.What are the advntages of Python over java.
                               Thank you

On 12/22/05, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
>
> On Wed, 21 Dec 2005, shivayogi kumbar wrote:
>
> > sir plz tell me the main differnces between java and python?  And What
> > are the advantages of python?
>
> Hi Shivayogi,
>
> When you post on a mailing list that's dedicated to help teach programming
> with Python, you do anticipate what kind of answers you're going to get
> here, right?  *grin*
>
> Are you a beginner programmer?  Do you have any previous experience with
> programming?  I'd really like us to turn the discussion toward what you
> really want to learn.  Advocacy and cheerleading is fun, of course, but
> let's see if we can help you too.  What are you trying to do?
>
>



More information about the Tutor mailing list