1. Hello, World source code

Java * brief prints Hello, World. * author McUlover666 */
class Hello
{
	public static void main(String[] args)
	{
		/ / print the Hello, World.
		System.out.printf("Hello,World.\n"); }}Copy the code

2.Hello,World compile and run

2.1. Compile and run

When compiling source files written in the Java language, use the following format at the command line:

Javac < file name >Copy the code

After successful compilation, the Java bytecode file that can be executed by the Java VM is obtained in. Class format.

When running the compiled Java bytecode, run it on the command line in the following format:

Java < class name >Copy the code

2.2. Compile and run the instance

As shown in the figure, first enter the directory where the source file resides in the command line, then execute the compile command, and run the program after compilation:

3.Hello,World source code details

3.1. Basic format of Java program

Java is a fully object-oriented language with the following three points:

  • The file format:.java;
  • Class definition: a Java file must have a class;
  • Main method: the program runtime needs a class name, and the main method is an entry to a class runtime.

3.2. The annotation

Java comment syntax is the same as in C:

  • / * * /betweenRepresents comment content, can comment more than one line;
  • //afterRepresents the comment content, can only comment a single line;
  • / * * * /betweenRepresents the annotation content, can annotate more than one line, used to extract the document;

3.3. Class definition

Class definitions are defined using the keyword class in the following format:

classThe < class name >{}Copy the code

3.4. The main method

The main method is an entry to the class runtime, in the following format:

public static void main(String[] args)
{}Copy the code

Where (in object oriented functions are called methods) :

  • public: indicates that the method is public
  • static: indicates that the method is static
  • void: The return value of this method
  • main: the method name
  • String[] args: method arguments (passed at command line runtime)

3.5. Summary and answer of core questions

  • Q.1 is there any requirement for the class name? A: The file name is written at compile time and the class name is written at run time. Therefore, the file name and the class name are generally the same, and the first letter of the class name is capitalized to facilitate compilation and execution.
  • Question 2. How many classes can there be in a Java file? A: Because you need to specify class names when running Java programs, you can have many classes in a Java file, but you need at least one. For example, the following test program:
class A
{
	public static void main(String[] args)
	{
		/ / print A
		System.out.printf("A\n"); }}class B
{
	public static void main(String[] args)
	{
		/ / print B
		System.out.printf("B\n"); }}Copy the code

  • Question 3. How are parameters of the main function used and passed?

    Answer: The argument to the main function is an array of stringsString[] args, the parameters are passed at run time, as shown in the following example:
class A
{
	public static void main(String[] args)
	{
		// Prints the first argument passed in
		System.out.printf("%s\n", args[0]); }}Copy the code

Welcome to subscribe to my wechat official account: “McUlover666”.