This is the implementation with explanations of a Hello World simple program. It is useful to document keyword and Java modifiers, as well as how to execute a program.
Science 7 - LAND and SEA BREEZE and its Characteristics
2 jfh-yourveryfirstprogram
1. Riccardo Rigon
Java for Hydrologists
Hello World
R.Rigon-IltavolodilavorodiRemowolf
Tuesday, July 16, 13
2. “Copy the work of other smart people’
http://lin-ear-th-inking.blogspot.it/2013/03/10-step-program-for-developers.html
Tuesday, July 16, 13
3. Objectives
Writing and commenting “Hello World”
Introduction
•This is kind of a parallel of the section “Building a Java Program” by BE.
•Second section of DJE is a little different, but many notions can be useful.
R. Rigon
Tuesday, July 16, 13
4. Preliminary operations
if you are working within Eclipse
•Create a new Java project within Eclipse
•Create a new Java package
•Create a new Java file
For all of these operations, please refer to the Lars
Vogel Eclipse Tutorial
R. Rigon
Work alone
Tuesday, July 16, 13
5. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
this, which is one of the simplest program
It is saved in a file whose name must be: HelloWorld.java
Copy in the new file
R. Rigon
First Program
Tuesday, July 16, 13
6. A Java package is a mechanism for organizing Java classes into
namespaces similar to the modules of Modula.
Java packages can be stored in compressed files called JAR files,
allowing classes to download faster as a group rather than one at a
time.
Programmers also typically use packages to organize classes
belonging to the same category or providing similar functionality.
Classes in the same package can access each other's package-access
members.
package org.geoframe.first;
R. Rigon
Packages
Tuesday, July 16, 13
7. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
This, which is one of the simplest program
In Java one file can contain one single public class, which is a construct like
the above
Copy in the new file
R. Rigon
Modifiers
Tuesday, July 16, 13
8. public means :
is in the programming jargon a modifier. A private class is visible
outside the package that contains it.
It does not apply only to classes but also to Packages and Worlds.
Alternative to public for classes) are: private, protected (an no modifier)
•private means that the class is hided from other classes within the package.
•protected is a version of public restricted only to subclasses
* See also the Chapter “Access Control” in BE book, and for generic information also the Building a Java
program in DJE book
R. Rigon
Modifiers
Tuesday, July 16, 13
9. This, which is one of the simplest program
Hello World is the name of the class. In Java you need to build a class to do
something
Copy in the new file
package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
R. Rigon
Class Names
Tuesday, July 16, 13
10. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
this, which is one of the simplest program
To be executed the class needs to contain a main( ) method (or routine). The
argument of the method main is a String[ ] object. args is not used but the
compiler still needs it.
Copy in the new file
R. Rigon
main( )
Tuesday, July 16, 13
11. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
This, which is one of the simplest program
The main(String[ ] args ) method has usually 3 modifiers:
•public
•static
•void
Copy in the new file
R. Rigon
Methods’ Modifiers
Tuesday, July 16, 13
12. public modifier has already be seen as in the case of classes
here, however is applied to a method, a special method indeed. Since the method
is public, it is visible outside the class, and can be shared with other classes.
R. Rigon
Methods’ Modifiers
Tuesday, July 16, 13
13. static (a few things to explain here):
a static method
http://www.javatutorialhub.com/java-static-variable-methods.html
• It is a method which belongs to the class and not to the object (instance)
• A static method can access only static data. It can not access non-static data
(instance variables)
• A static method can call only other static methods and can not call a non-
static method from it.
R. Rigon
Methods’ Modifiers
Tuesday, July 16, 13
14. static (a few things to explain here):
a static method
http://www.javatutorialhub.com/java-static-variable-methods.html
• A static method can be accessed directly by the class name and doesn’t need
any object
• Syntax : <class-name>.<method-name>
• A static method cannot refer to “this” or “super” keywords in anyway
Side Note:
• main method is static , since it must be be accessible for an application to
run , before any instantiation takes place.
R. Rigon
Methods’ Modifiers
Tuesday, July 16, 13
15. void:
is the return type. Since main( ) does not return anything, the void keyword is
used
R. Rigon
Methods’ Modifiers
Tuesday, July 16, 13
16. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
System.out.println( )
Java comes with a lot of libraries. These do not need to be imported. This, for
instance is a method (println) of the class System.out
R. Rigon
The method and the System
Tuesday, July 16, 13
17. package org.geoframe.first;
public class HelloWorld {
! public static void main(String[] args) {
System.out.println("Pietro ciao!");
! }
}
System.out.println( )
This stackoverflow post clarify better what it does:
http://stackoverflow.com/questions/12002170/what-is-system-out-println-in-system-out-println-in-java
R. Rigon
The method and the System
Tuesday, July 16, 13
18. Execution
•Inside Eclipse
•Outside Eclipse
All of the above information is available from the Vogella website
•in OMS3
Please look at the appropriate slides
R. Rigon
Making it to work
Tuesday, July 16, 13
19. Thank you for your attention.
G.Ulrici,2000?
It ends here
Tuesday, July 16, 13