Java vs Python: Basic Program Structure

The Hello World program

The "hello world" program is one of the most common first programs taught in any language. Basically, you create a program that displays the phrase "hello world" after you execute the code. You can do this from command line or within the IDE.

Java

The typical java program to print "hello world" to the console will have the following structure.*

public class myProgram{
 public static void main(String args[]){
  System.out.println("Hello World");
 }
}

There are a few rules.

The name of the program must be defined after the class keyword. In this case, we picked the name "myProgram" Therefore, the name of the java source file must be "myProgram.java".

Notice all the keywords - public, static, void, main, etc. Notice all the characters - semicolons, parentheses, brackets. They are all needed. In fact, this is a barebones program. For now, understand that the class section identifies this class. And the "public static void main" section tells the compiler where to enter the program. In more complex programs, this class will have more code inside and outside of this section. The "System.out.println("Hello World");" line tells the compiler to print the line "hello world". It's the only thing special about this program.

Python

The Python program to print "Hello World" to the console is simpler:

print 'Hello World'
 

You can also write it as:

print "Hello World"


You may have noticed there is

- no need to specify the name of the program and have a filename to match

- no need to specify access modifiers (like public in Java)

- no need specify where the program should enter (like main in Java)

- no need for most punctuation (like semicolons and brackets in Java). Whitespace such as indentation and new lines are important in Python, however. Of course, if you were programming with good practices, you would be using whitespace similarly anyway.

- shorter commands ("print" basically does what "System.out.println" does)


Of course, this was a rather simplistic example. I will be posting more articles to my site - TechTedium. Cheers.


* If you are writing your own java programs, it's very helpful to use an IDE like Eclipse or IntelliJ. If you are writing your own python programs, you can usually get by with IDLE, the IDE that comes with Python. If you want more power, go for Eclipse or Wingware.

More by this Author

  • 4 Chords on a Piano
    2

    One of my new favorite iPad apps is the Virtuoso Piano. This free app is very nice and has nearly all the features an eager and noob piano player like myself would enjoy (there is also a Pro version with more features...


Comments 1 comment

Elise 5 years ago

Thanks for the detail on this one.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working