Java Introduction
This guide is intended to function as a basic Java reference for students who are familiar with Python or C++. There are a number of more complete guides on the Internet; here are a few:
- Java for Python Programmers by Brad Miller
- From Python to Java by Ken Lambert
- Think Java: How to Think Like a Computer Scientist, an introductory text by Allen Downey and Chris Mayfield
- Oracle’s official Java Tutorials, especially the Learning the Java Language trail
The documentation for the Java standard library API can be found at https://docs.oracle.com/javase/8/docs/api/. Make sure to use this reference!
Java shares features with both Python and C++. Here is a breakdown of a few of the more prominent features:
Python | C++ | Java | |
---|---|---|---|
Syntax | |||
Blocks | Indentation-based | Explicit via braces | Explicit via braces |
Statement Termination | Newline | Semicolon | Semicolon |
Type Annotations | None | Required | Required |
Semantics | |||
Variable Declaration | Implicit | Explicit | Explicit |
Memory Management | Garbage-collected | Manual (new and delete) | Garbage-collected |
Allocation | Always dynamic (via reference) | Either static (e.g. on stack) or dynamic (via pointer) | Static for primitives (e.g. int) and dynamic for objects |
Environment | |||
Compilation | None required | To native machine code | To JVM bytecode |
Typical Execution | Interpreted | Natively executed | Interpreted or JIT-compiled |
This guide provides a collection of mostly-matching examples in all three languages to give some impression of the parallels. They are divided into the following sections:
- Expressions, Statements, and Basic Syntax
- Standard Libraries
- Declarations
- Common I/O Operations
- Sorting
Expressions, Statements, and Basic Syntax
Description | Python | C++ | Java |
---|---|---|---|
Variable declaration and assignment |
|
|
|
Conditionals |
|
|
|
While Loops |
|
|
|
For Loops |
N/A |
|
|
For-Each Loops |
|
|
|
Function Calls |
|
|
|
Comments |
|
|
|
Auto-documentation Comments |
|
A variety of auto-doc formats exist for C++, but Doxygen is commonplace:
|
|
Allocating dynamic arrays |
Python does not have arrays. |
|
|
Dynamically allocating an object |
|
|
|
Deallocating a dynamically allocated object |
N/A |
|
N/A |
Standard Libraries
Description | Python | C++ | Java |
---|---|---|---|
Dynamically allocating a string object |
|
|
|
Comparing two strings |
|
|
|
Dynamically allocating and initializing a canonical list |
|
|
|
Dynamically allocating and initializing a canonical dictionary |
|
|
|
Declarations
Description | Python | C++ | Java |
---|---|---|---|
Functions |
|
|
Java has no concept of top-level functions. However, classes may be given static methods.
|
Classes |
|
|
|
Interfaces |
Python has no formal interfaces, though abstract base classes are sometimes used:
|
|
|
Inheritance |
|
|
or
|
Parametric Polymorphism |
N/A – Python does not provide any compile-time typechecking, so type parameters would have no meaning. |
|
|
Common I/O Operations
Description | Python | C++ | Java |
---|---|---|---|
Printing a string |
|
|
|
Printing a formatted message |
or
|
|
|
Opening a file |
|
|
|
Closing a file |
|
|
|
Reading a line from a file |
|
|
|
In order to read lines from standard input, Java programs often use BufferedReader
around the InputStream
known as System.in
. For instance, consider the following program:
public class Example {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Hello! What is your name? ");
String name = userInput.nextLine();
System.out.printf("Hello, %s!\n", name);
}
}
Sorting
Description | Python | C++ | Java |
---|---|---|---|
Sorting a list |
|
|
|
Sorting a list via a custom comparison |
|
|
|