Talk:Java Training Course/JT02

From tehowiki
Revision as of 09:42, 24 September 2017 by imported>Gfis (explanation of names)
Jump to navigation Jump to search

An almost minimal Java program is:

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

That program must be stored in a file HelloWorld.java, since Java source files must have the same name as the class contained in it. Usually there is exactly one class in one Java source file.

With abbreviated names and minimal spacing the program almost fits on a single line:

public class HW{public static void main(String[]a){System.out.println("Hello, World!");}}

Upper/lowercase letters are different. Their distinction is (very) important in Java.

For the separation of words in Java names, underscore _characters would be possible, but they are deprecated. The CamelCase convention of embedded uppercase letters is used instead in names. Names may contain:

  • letters (the first character of a name must be a letter), even Unicode (non-ASCII) letters
  • digits
  • the underscore _, but this is deprecated or at least unusual
  • no hyphen - or dot . (the dot is used for property and method qualifying)

By convention, there are 3 types of names:

  • starting with lowercase: for ordinary variables, properties, methods, packages
  • starting with uppercase: for classes
  • all upperccase: for constants (static final)

The program is compiled into a file HelloWorld.class by

javac HelloWorld.java

The classfile maybe run by

java HelloWorld

It will output the desired text:

Hello, World!