Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
377 views

A Comparison of The Syntax of Python and Java PDF

Python and Java have some key differences in their syntax and data types: - Python is dynamically typed and supports both object-oriented and non-object-oriented programming, while Java only supports object-oriented programming and is statically typed. - Python uses indentation to delimit blocks of code and comments start with #, while Java uses semicolons (;) and supports both // line comments and /* block comments */. - Python supports a range of built-in data types including integers, floats, Booleans, strings and collections, while Java only supports primitive types like int and double as well as reference types like String and collections. - Both languages support common operators like &&, ||, and ! but

Uploaded by

shu2u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
377 views

A Comparison of The Syntax of Python and Java PDF

Python and Java have some key differences in their syntax and data types: - Python is dynamically typed and supports both object-oriented and non-object-oriented programming, while Java only supports object-oriented programming and is statically typed. - Python uses indentation to delimit blocks of code and comments start with #, while Java uses semicolons (;) and supports both // line comments and /* block comments */. - Python supports a range of built-in data types including integers, floats, Booleans, strings and collections, while Java only supports primitive types like int and double as well as reference types like String and collections. - Both languages support common operators like &&, ||, and ! but

Uploaded by

shu2u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A Comparison of the Basic Syntax of Python and Java

Python Java

Python supports many (but not all) aspects of Java supports only object-oriented
object-oriented programming; but it is programming.
possible to write a Python program without
making any use of OO concepts.
Programs written in Java must be explicitly
Python is designed to be used interpretively. compiled into bytecodes (.class files),
A Python statement may be entered at the though an IDE may do this automatically in a
interpreter prompt (>>>), and will be executed way that is transparent to the user. Java does
immediately. (Implementations make some not support direct execution of statements -
use of automatic compilation into bytecodes though there are tools like Dr. Java that
(.pyc files). support this.

Python is dynamically typed: Java is statically typed:


A variable is introduced by assigning a A variable must be explicitly declared to be
value to it. Example: of some type before assigning a value to it,
though declaration and assignment may be
someVariable = 42
done at the same time. Examples:
A variable that has been assigned a value of int someVariable;
a given type may later be assigned a value int someVariable = 42;
of a different type. Example:
A variable that has been declared to be of a
someVariable = 42 particular type may not be assigned a value
someVariable = 'Hello, world' of a different type.

Python supports the following built-in data Java has two kinds of data types: primitive
types: types and reference types. Java supports the
following primitive data types:
Plain integers (normally 32-bit integers in
the range -2147483648 through byte - 8-bit integers
2147483647). short - 16-bit integers
Long integers (size limited only by memory int - 32-bit integers
size of the machine running on) long - 64-bit integers (Java also supports a
Booleans (False and True). class java.math.BigInteger to represent
Real numbers. integers whose size is limited only by
Complex numbers. memory)
float - 32-bit real numbers.
In addition, Python supports a number of
double - 32-bit real numbers.
types that represent a collection of values -
including strings, lists, and dictionaries. boolean - (false and true).
char - a single character.

In addition, Java supports arrays of any type


as the reference types, and the API includes
the class String and a large number of classes
used for collections of values.

1
A Comparison of the Basic Syntax of Python and Java

Python is line-oriented: statements end at the Statements in Java always end with a
end of a line unless the line break is explicitly semicolon (;). It is possible for a statement to
escaped with \. There is no way to put more run over more than one line, or to have
than one statement on a single line. multiple statements on a single line.
Examples: Examples:
this is a statement this is a statement;
this is another statement this is another statement;
this is a long statement that extends over more \ this is a long statement that extends over more
than one line than one line;
a statement; another; another;

Python comments begin with # and extend to Java has two kinds of comments. A comment
the end of the line. Example: beginning with // extend to the end of the
line (like Python comments). Comments can
# This is a comment
also begin with /* and end with */. These
A new statement starts here
can extend over multiple lines or be
embedded within a single line. Examples:
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
Statement starts /* comment */ then continues

Python strings can be enclosed in either single Java strings must be enclosed in double
or double quotes (' or ""). A character is quotes (""). A character is a different type of
represented by a string of length 1. Examples: object and is enclosed in single quotes (').
'This is a string' Examples:
"This is also a string" # Equivalent
"This is a String"
'c' # A string
'c' // A character, but not a String
"c" # An equivalent string

Python uses the following operators for Java uses the following operators for
constructing compound boolean expressions: constructing compound boolean expressions:
and, or and not. Example: &&, ||, ! and ^ (meaning exclusive or)
Example:
not(x > 0 and y > 0) or z > 0
! (x > 0 && y > 0) || z > 0 ^ w > 0

In Python, the comparison operators (>, <, >=, In Java, most of the comparison operators
<=, == and !=) can be applied to numbers, ( >, <, >=, and <=) can be applied only to
strings, and other types of objects), and primitive types. Two (== and !=) can be
compare values in some appropriate way (e.g. applied to any object, but when applied to
numeric order, lexical order) where possible. reference types they test for same (different)
object rather than same (different) value.

2
A Comparison of the Basic Syntax of Python and Java

There is no universally-accepted Python By convention, most names in Java use mixed


convention for naming classes, variables, case. Class names begin with an uppercase
functions etc. letter; variable and function names begin with
a lowercase letter. Class constants are named
using all uppercase letters with underscores.
Examples:
AClassName
aVariableName
aFunctionName()
A_CLASS_CONSTANT

Python definite looping statements have the Java has two kinds of definite looping
form for variable in expression: Example: statements. One has the form
for (variable in collection) Example:
for p in pixels:
something for (p in pixels)
something;

Python uses the built-in function range() with Java uses a different form of the for to loop
for to loop over a range of integers. over a range of integers. Examples:
Examples: for (int i = 1; i < 10; i ++)
for i in range(1, 10) something;
something (i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
for i in range(1, 10, 2) something;
something (i takes on values 1, 3, 5, 7, 9)
(i takes on values 1, 3, 5, 7, 9)

Python indefinite looping statements have the Java has two forms of indefinite looping
form while condition: Example: statement. One has the form
while (condition) Example:
while x > 0:
something while (x > 0)
something;

The other has the form


do ... while(condition). Example:

do
something;
while(x > 0);

3
A Comparison of the Basic Syntax of Python and Java

Python conditional statements have the form Java conditional statements have the form
if condition: and an optional else part has the if (condition) and an optional else part has
form else:. The form elif condition: is the form else (no colon) There is no elif
allowed as an alternative to an else: form - else if is used directly. Examples:
immediately followed by an if. Examples:
if (x < 0)
if x < 0: something;
something
if (x < 0)
if x < 0: something;
something else
else: something different;
something different
if (x < 0)
if x < 0: something;
something else if (x > 0)
elif x > 0: something different;
something different else
else: yet another thing;
yet another thing
Java also has another form of conditional
statement (switch) which has no analog in
Python. Example:

switch(i)
{
case 0:
something 1;
something 2;
...
break;
case 1:
something else;
break;
....
default:
yet something else;
}

(The individual cases can include any number


of statements [ shown for the first case only,
but possible for any ], normally ending with
the special statement break; )

4
A Comparison of the Basic Syntax of Python and Java

The scope of a Python conditional or looping The scope of a Java conditional or looping
statement is denoted by indentation. (If statement is normally just the next statement.
multiple lines are to be included, care must be Indentation is ignored by the compiler
used to be sure every line is indented (though stylistically it is still highly desirable
identically). Examples: for the benefit of a human reader). If
multiple lines are to be included, the scope
if x < 0:
do something must be delimited by curly braces ({ , }).
do another thing regardless of the value of x (Optionally, these can be used even if the
scope is a single line.) Examples:
if x < 0:
if (x < 0)
do something
do something else do something;
do yet a third thing do another thing regardless of the value of x;
do another thing regardless of the value of x
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;

if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;

if (x < 0)
{
do something;
}
do another thing regardless of the value of x;

5
A Comparison of the Basic Syntax of Python and Java

A Python function definition has the form A Java function definition always occurs in
def function-name(formal-parameter-list): the context of some class. It has the form
body type function-name(formal-parameter-list)
Example: {
body
def disc(a, b, c): }
return b * b - 4 * a * c Example:

If there are no parameters, an empty list (()) class Solver


is used. {
....
The body is delimited by indentation, and double disc(double a, double b,
can be any number of lines. double c)
{
A function does not have to return a value; if return b * b - 4 * a * c;
it does not, no return statement is required, }
though return without a value (or with ...
}
None) can be used to end the function
execution early. The return type of a Java function must
always be specified. If it does not return a
value, void must be used.
If there are no parameters, an empty list (())
is used.
A type must be specified for each of the
parameters.
The body is always delimited by curly
braces ({ , }) even if the body is a single
line. Indentation is ignored by the compiler
(though stylistically it is still highly
desirable for the benefit of a human reader).
If the function does not return a value (its
type is void), no return statement is
required, though return without a value can
be used to end the function execution early.

A Python function is called by specifying its A Java function is called by specifying the
name followed by a list of actual parameters object or class to which it is to be applied
(that must be the same length as the list of followed by a dot, then its name followed by
formal parameters) Example: a list of actual parameters (that must be the
same length as the list of formal parameters,
discr(1, 2, 1) with each actual parameter compatible with
the declared type of the corresponding formal
parameter) Example:

solver.discr(1, 2, 1)

You might also like