知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

      • Theory:Scanning the input
      • Theory:Arithmetic operations
      • Theory:Integer types and operations
      • Theory:Increment and decrement
      • Theory:Relational operators
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
      • Theory:Sizes and ranges
      • Theory:Switch statement
      • Theory:Declaring a method
      • Theory:The main method
        • The declaration of the main method
        • Invalid declarations of the main method
        • Conclusion
      • Theory:Type casting
      • Theory:Primitive and reference types
      • Theory:Array
      • Theory:Arrays as parameters
      • Theory:Iterating over arrays
      • Theory:Multidimensional array
      • Theory:Final variables
    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java basic
Jim
2022-06-23
目录

Theory:The main method

# The declaration of the main method

Java is primarily an object-oriented language. It means a Java program can be considered as a collection of objects that communicate via calling each other's methods. A typical Java program includes a lot of classes, interfaces, objects, and other concepts from object-oriented programming.

Even the simplest "procedural-style" program should have at least one class and the main method inside to start the program. The main method is the entry point for any application. It means that the execution of any program begins right here. Ever since Java 7, there has been no other way to start an application without this method (excluding the case when you start your application inside a special container for applications, but it is not considered in our materials).

Let's see an example of the simplest application that prints the text "Hello, Java" in the standard output:

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello, Java");
    }
}
1
2
3
4
5
6

Here is a class named Main. The class contains the main method for starting the program.

It is important to mention that a class containing the main method can have any name, but the main method should always have the name main.

Let's take a closer look at the declaration of the main method:

public static void main(String[] args)
1
  • the keyword public indicates that the method can be invoked from everywhere;
  • the keyword static indicates the method can be invoked without creating an instance of the class;
  • the keyword void indicates the method doesn't return any value;
  • the array variable args contains arguments entered at the command line, the array is empty if there are no arguments.

As you can see, even the simplest Java application contains a lot of concepts. All of them will be studied in the next topics related to methods and the object-oriented programming. Now you should just understand how to write and run a simple Java program with the main method.

# Invalid declarations of the main method

If the main method has an invalid declaration, two cases are possible:

  • your program cannot be compiled
  • your program is successfully compiled but can't be started

Your program cannot be compiled. This is the case when the main method declaration breaks the syntax of Java.

Examples:

  • invalid method declaration: no return value (even void).
public static main(String[] args)
1
  • invalid method declaration: a mistake in the keyword (pulic instead of public).
pulic static void main(String[] args)
1

A program can be compiled but cannot be run. This is the case when the main method has a correct declaration as a regular method but doesn't satisfy the specific requirement of the main method.

Examples:

  • invalid arguments (should be String[] args)
public static void main(String args) {
    System.out.println("Hello, Java");
}
1
2
3
  • the method declaration has no keyword static
public void main(String[] args) { 
    System.out.println("Hello, Java");
} 
1
2
3

In both cases, an error happens at runtime.

# Conclusion

So, the main method is the entry point of any Java program. It has a very specific syntax which you need to remember.

编辑 (opens new window)
#Java
上次更新: 2022/09/26, 16:55:15
Theory:Declaring a method
Theory:Type casting

← Theory:Declaring a method Theory:Type casting→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式