Comparing Python and Java CS102 Dean Zeller and Robert Carver Spring 2018
Hello World – Python & Java print() prints the supplied content to the user via console output. Console output to the user:
Data types & Variables Python Java Variables need no declaration. Variables can change type at any time. All variables must be declared in order to be used. Data type cannot change once declared.
Java Data types Reminder: all variables must be defined once and only once. Type Name Kind of Value Memory Used Range of Values byte Integer 1 byte -128 to 127 short 2 bytes -32,768 to 32,768 int 4 bytes -2,147,483,648 to 2,147,483,648 long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 float Floating-point ±3.40282347 × 10 +38 to ±1.40239846 × 10 −45 double ±1.79763931313486231570 × 10 +308 to ±4.94065645841246544 × 10 −324 char Single Unicode character All Unicode values from 0 to 65,535 boolean 1 or more bytes True or False
Java Data Types Data Types example:
Array/List indices start at 0 Arrays Python Java Uses Lists Define an empty list Add elements using append Variable length Uses arrays Arrays must be defined Add elements more complex Length must be specified Array/List indices start at 0 Python Example: Output:
Arrays Java - Example Assigns the array slot at the provided index to the given value. Output:
Mathematical Expressions Most operators are the same between Java and Python. Operation Python Java Usage Example Addition + a+b Same Subtraction - a-b Multiplication * a*b Division / a/b Power ** a**b Math.pow(a,b) Parenthesis () 3*(a+b) Assignment = c=a+b
Statement Blocks & Indentation Python Java Uses curly braces {} to denote blocks of code. All lines are ended using semi-colons (;). Uses indentation to denote blocks of code. Blocks start with a colon on the defining line. Python example: Denotes start of block Code Blocks
Java Example Blocks are set off by lines without semicolon Code blocks are enclosed with {} Blocks are set off by lines without semicolon Lines end with a semicolor
Loops – While Executes as long as the While condition is met. Python Java Output:
Do-While Loop Similar to a while loop, but the loop is always executed at least once. No Python counterpart. Do Loop Form do { body-statements; } while (test); While Loop Form while (test){ }
For Loop – Basic Form Python Java Output:
For Loop – Java for(int i = 0; i < 10; i++) { } Increment: Statement executed every time the loop executes. Initialization: Done once, at beginning. Condition: Boolean condition to continue execution of loop.
For Loop – Other Forms Python Java Start, End Start, End, Increment Output: Loop 1 Loop 2 Loop 3 1 2 3 4 5 5 6 7 8 9 10 20 30 40 50 60 70 80 90
For and While Loop Comparison (initialization) while(condition) { (statements) (increment) } for((initialization),(condition),(increment)) { (statements) }
For each loop Python Java Both Python and Java provide a form of the for-loop in which the only parameter is a list. The loop then executes for each item in the list, using the for-loop variable. Python Java Output: Hello Joe Hello Bob Hello Kathy Output: Hello Joe Hello Bob Hello Kathy
Credits Script outline and final editing by Dean Zeller Content and visual organization by Robert Carver Reference: Java An Introduction to Problem Solving, (8th edition) by Walter Savitch