OpenStax-CNX module: m43473 1 How to make a "hello world" program in Java with Eclipse * Hannes Hirzel Based on How to make a "hello world" program in Java. by Rodrigo Rodriguez This work is produced by OpenStax-CNX and licensed under the Creative Commons Attribution License 3.0 Abstract This module teaches you how to create a simple Java program with the Eclipse IDE. The classic "hello world" program can help assure the programmer that he has a working java development environment. 1 Introduction In this module you will learn how to set up a 'hello world' type project in the Eclipse IDE (Integrated Development Environment) after you have downloaded and installed it. 2 Eclipse startup When you start up Eclipse the rst time a dialog pops up where you can indicate where to put the project les. For the tutorial we accept the default value. * Version 1.4: Mar 16, 2013 4:57 am +0000 http://cnx.org/content/m43050/1.1/ http://creativecommons.org/licenses/by/3.0/
OpenStax-CNX module: m43473 2 Then the Eclipse welcome screen appears. Then choose the menu 'File', 'New Project', 'Java Project'.
OpenStax-CNX module: m43473 3 Then ll in a project name.
OpenStax-CNX module: m43473 4 Click 'Finish'; then the welcome screen appears again. Close it.
OpenStax-CNX module: m43473 5 3 The rst Java class le A Simple Java Program We are going to write a simple hello world program in Java. It will have one class denition with one method. The method will print out hello world! to the system console. Click on the project and bring up the context menu.
OpenStax-CNX module: m43473 6 Fill in package name and class name.
OpenStax-CNX module: m43473 7
OpenStax-CNX module: m43473 8 The denition of class 'HelloWorld' The Program Add the following code the following code } class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); } Click on the 'Run' button.
OpenStax-CNX module: m43473 9 What is it doing? So what is this program really doing? First the program is surrounded by a class HelloWorld { }. This is a class denition. Everything in Java needs to be surrounded by a class, that is how code is organized. For now, think of the class as an encapsulation of your code that does what the class is intended to do. For example, we may have a class Car { } and we populate it with code about cars, or we can have a class PhoneBill { } and in it we put phone bill related code. The Main Function In our class we have piece of code like the following. public static void main( String[] args ){} This is a method. Classes are made up of methods and attributes. An attribute is a data type that describes the class. A method is just like a mathematical function. It has inputs and outputs, and can do any operation inside that the Java language allows. The String[] args is an input to the function. This input is used to pass in information by the compiler, so you do not need to worry about it right now. However, make sure to add it or else your program will not execute. Our main function does not return anything. This is denoted by the void part of the function. If our function had a String instead of a void, then the function would have to return a String, otherwise it would crash. The static part of the name tells the compiler that this class can be called by an outside program without creating an object of the class HelloWorld. This may sound confusing, but just think of it as the static gives the function main the ability to start from whoever wants to use it. Last, the public part just says that this function can be used from inside the class and from outside the class by another class. The Print Function Inside the {} of the main function, we have another piece of code that says System.out.println("Hello World!");. As you can guess, this line of code is the one that prints out "Hello World" to our console window.
OpenStax-CNX module: m43473 10 The console window is the window that java creates to create console based applications, applications with text only outputs on the screen. The part of the code that actually does the printing is println(), the System.out. part is actually a location. This System.out. part tells the Java compiler to look for println() function in the System libraries, then in the out libraries. 4 The Result When this runs, the compiler rst checks if you have any syntax errors. After this, it executes the main method of the main class. The main method only contains the println() line, so it does just that. It then ends and you are done. 5 Further reading Java Syntax Primer 1 Assignment and Equality in Java 2 Control structures in Java 3 Arrays in Java 4 6 See also "Hello World" Application 5 in the Java documentation (Netbeans and command line instructions). 1 http://cnx.org/content/m11791/latest/ 2 http://cnx.org/content/m31187/latest/ 3 http://cnx.org/content/m31246/latest/ 4 http://cnx.org/content/m31245/latest/ 5 http://docs.oracle.com/javase/tutorial/getstarted/cupojava/index.html
OpenStax-CNX module: m43473 11 7 Note This module is in draft status. The terms used need to be sorted out and claried. In particular method, function, object, static method and class.