Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions _sources/Java4Python/firstjavaprogram.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Now lets look at the same program written in Java:
}

What we see is that at the core there are a few similarities, such as a
main and the string “Hello World” However, there is a lot more stuff
main and the string “Hello World”. However, there is a lot more stuff
around the edges that make it harder to see the core of the program. Do
not worry! An important skill for a computer scientist is to learn what
to ignore and what to look at carefully. You will soon find that there
Expand Down Expand Up @@ -88,7 +88,7 @@ from compiling:

- Early detection of errors

- Faster Program Execution
- Faster program execution

The job of the compiler is to turn your java code into language that the
Java Virtual Machine (JVM) can understand. We call the code that the JVM
Expand All @@ -106,7 +106,7 @@ Now that we have run our hello world program, lets go back and look at
it carefully to see what we can learn about the Java language. This
simple example illustrates a few very important rules:

1. Every Java program must define a class, all code is inside a class.
1. Every Java program must define a class, and all code is inside a class

2. Everything in Java must have a type

Expand All @@ -115,7 +115,13 @@ simple example illustrates a few very important rules:

Lets take the hello world example a line at a time to see how these
rules are applied. On line 1 we see that we are declaring a class called
Hello. As rule 1 says all Java code resides inside a class. Unlike
Hello:

::

public class Hello {

As rule 1 says all Java code resides inside a class. Unlike
Python where a program can simply be a bunch of statements in a file,
Java programs must be inside a class. So, we define a class ``Hello``,
which is not a very useful class because it has no instance variables, and only one
Expand All @@ -130,19 +136,19 @@ is:

::

public static void main(String[] args)!
public static void main(String[] args)

Everything on this line is significant, and helps in the identification
of this method. For example the following lines look similar but are in
fact treated by Java as completely different methods:

- ``public void main(String[] args)``
- ``public void main(String[] args)``

- ``public static void main(String args)``
- ``public static void main(String args)``

- ``public static void main()``
- ``public static void main()``

- ``void main(String args)``
- ``void main(String args)``

Just digging in to this one line will take us deep into the world of
Java, so we are going to start digging but we are not going to dig too
Expand All @@ -166,7 +172,7 @@ instance, for example the math module contains many methods: sin, cos,
etc. You probably evaluated these methods using the names
``math.cos(90)`` or ``math.sin(60)``.

The next word, ``void`` tells the Java compiler that the method ``main``
The next word, **void** tells the Java compiler that the method ``main``
will not return a value. This is roughly analogous to omitting the
return statement in a Python method. In other words, the method will run
to completion and exit but will not return a value that you can use in
Expand All @@ -176,7 +182,7 @@ will return. This is in keeping with the rule that says everything in
Java must have a type. In this case we use the special type called
``void`` which means no type.

Next we have the proper name for the method: ``main``. The rules for
Next we have the proper name for the method: **main**. The rules for
names in Java are similar to the rules in Python. Names can include
letters, numbers, and the ``_``. Names in Java must start with a letter.

Expand All @@ -191,9 +197,14 @@ that if you call ``main`` somewhere else in your code and and pass it an
array of integers or even a single string, the compiler will flag it as
an error.

That is a lot of new material to digest in only a single line of Java.
That is a lot of new material to digest in only a single line of Java!
Lets press on and look at the next line:
``System.out.println("Hello World!");``. This line should look a bit

::

System.out.println("Hello World!");

This line should look a bit
more familiar to you. Python and Java both use the dot notation for
finding names. In this example we start with ``System``. System is a
class. Within the system class we find the object named ``out``. The
Expand All @@ -206,10 +217,13 @@ method in Java.

Now there is one more character on this line that is significant and
that is the ``;`` at the end. In Java the ``;`` signifies the end of a
statement. Unlike Python where statements are almost always only one
line long, Java statements can spread across many lines. The compiler
statement. Java statements can spread across many lines, but the compiler
knows it has reached the end of a statement when it encounters a ``;``.
This is a very important difference to remember. In Java the following
In Python, it is not required (or recommend) to use semicolons in this way,
but whitespace is meaningful.
In contrast, in Java semicolons are **required** to end statements, but
whitespace is not considered meaningful.
This is a very important difference to remember! In Java, the following
statements are all legal and equivalent. I would not encourage you to
write your code like this, but you should know that it is legal.

Expand All @@ -228,7 +242,7 @@ write your code like this, but you should know that it is legal.
;

The last two lines of the hello world program simply close the two
blocks. The first or outer block is the class definition. The second or
blocks using ``}``. The first or outer block is the class definition. The second or
inner block is the function definition.

If we wanted to translate the Java back to Python we would have
Expand Down
Loading