Talk:Java Training Course/JT02: Difference between revisions

From tehowiki
Jump to navigation Jump to search
imported>Gfis
new
 
imported>Gfis
explanation of names
Line 5: Line 5:
     } // main
     } // main
  } // HelloWorld
  } // HelloWorld
It must be stored in a file <code>HelloWorld.java</code>, since Java source files must have the same name as the class contained in it.  
That program must be stored in a file <code>HelloWorld.java</code>, 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.


Upper/lowercase letters are (very) important in Java.  
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!");}}


For the separation of words in Java names, underscore characters would be possible, but they are deprecated. The CamelCase  
Upper/lowercase letters are different. Their distinction is (very) important in Java.
convention of embedded uppercase letters is used instead.
 
For the separation of words in Java names, underscore <code>_</code>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 <code>_</code>, but this is deprecated or at least unusual
* no hyphen <code>-</code> or dot <code>.</code> (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 <code>HelloWorld.class</code> by
The program is compiled into a file <code>HelloWorld.class</code> by

Revision as of 09:42, 24 September 2017

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!