Java随笔-HelloWorld

143 阅读4分钟

HelloWorld

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

运行结果:

S:\JDK16\bin\java.exe ...
Hello World

解释说明

Java-HelloWorld.png

  1. public 访问权限是指本类及本类内部成员对其他类的可见性。Java中有四种访问权限,分别是public、protected、default(省略不写)、private。
访问权限本类同包子类其他
public
protected×
default××
private×××
  • public:所修饰的类、变量、方法均能被访问
  • protected:所修饰的成员能被子类、本类、同包类访问,专用于继承
  • default:本类和同包类均能访问,没有修饰符默认为该访问权限
  • private:只能在本类中访问,私有权限,常用于封装
  1. class class为类关键字,用于定义类,小写。不可与Class混淆。

  2. HelloWorld 类名,采用驼峰命名法命名,即每个单词首字母大写,内容不限,长度不限,不可使用关键字。

  3. static static(静态)用于修饰类的成员方法和类型成员变量,编写静态代码块可优化程序性能。

  • static修饰的成员属于类,随着类的加载而加载,不属于该类的实例化的某个对象,被类的所有成员共享(判断是否使用static关键字的条件),可通过类名调用;成员变量随着对象而存在,随着对象的消失而消失,成员变量只能通过对象名调用,
  • static声明的成员变量属于静态成员变量,静态变量存放在 Java 内存区域的方法区,静态变量在内存中只有一个拷贝(节省内存),JVM只为静态分配一次内存,成员变量存储在堆内存
  • 在静态方法中没有this关键字,对于本类,静态方法只能访问本类的静态的成员变量和静态的成员方法,不能通过this和super访问, static是不允许用来修饰局部变量
  • 静态代码块定义在类中的方法外, 静态代码块在非静态代码块之前执行,即在类加载的时候就运行了,而且只执行一次
  1. void void代表该方法没有返回值,或返回值为空。void属于基本类型,而Void(不可实例化的占位符类,修饰方法时必须返回null)属于类。

  2. main 全部小写,java程序入口方法,通过JVM直接启动,且类型必须为public static void,必须接收一个字符串数组的参数。java中方法命名首单词全部小写,其他单词首字母大写。

  3. String[] args main函数参数,类型必须为String[],名称为args,名称可以自行设定,使用args是为了和 java规范范例中main参数名保持一致。

  4. System.out.println("Hello World") 调用系统输出流输出“Hello World”且换行,若使用System.out.print("Hello World")则不会换行。 System代表系统,里面有很多系统级的属性和控制方法,由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类。system中包含了in、out和err三个成员变量,分别代表标准输入流(键盘输入)、标准输出流(显示器)和标准错误输出流(显示器)。

    /**
     * The "standard" input stream. This stream is already
     * open and ready to supply input data. Typically this stream
     * corresponds to keyboard input or another input source specified by
     * the host environment or user.
     */
    public static final InputStream in = null;

    /**
     * The "standard" output stream. This stream is already
     * open and ready to accept output data. Typically this stream
     * corresponds to display output or another output destination
     * specified by the host environment or user.
     * <p>
     * For simple stand-alone Java applications, a typical way to write
     * a line of output data is:
     * <blockquote><pre>
     *     System.out.println(data)
     * </pre></blockquote>
     * <p>
     * See the {@code println} methods in class {@code PrintStream}.
     *
     * @see     java.io.PrintStream#println()
     * @see     java.io.PrintStream#println(boolean)
     * @see     java.io.PrintStream#println(char)
     * @see     java.io.PrintStream#println(char[])
     * @see     java.io.PrintStream#println(double)
     * @see     java.io.PrintStream#println(float)
     * @see     java.io.PrintStream#println(int)
     * @see     java.io.PrintStream#println(long)
     * @see     java.io.PrintStream#println(java.lang.Object)
     * @see     java.io.PrintStream#println(java.lang.String)
     */
    public static final PrintStream out = null;

    /**
     * The "standard" error output stream. This stream is already
     * open and ready to accept output data.
     * <p>
     * Typically this stream corresponds to display output or another
     * output destination specified by the host environment or user. By
     * convention, this output stream is used to display error messages
     * or other information that should come to the immediate attention
     * of a user even if the principal output stream, the value of the
     * variable {@code out}, has been redirected to a file or other
     * destination that is typically not continuously monitored.
     */
    public static final PrintStream err = null;

System可供使用方法有:

在这里插入图片描述

System.out.常用方法有:

在这里插入图片描述

  1. { } 使用{}括起来的代码被称为代码块,根据位置和声明的不同,可分为局部代码块、构造代码块、静态代码块。
  • 局部代码块:局部位置,用于限定变量的生命周期
  • 构造代码块:在类中的成员位置,用{}括起来的代码。每次调用构造方法执行前,都会先执行构造代码块,每次调用构造方法都执行
  • 静态代码块:在类中的成员位置,用{}括起来的代码,且用static修饰,用于对类进行初始化,只执行依次 执行顺序:静态代码块--->构造代码块--->局部代码块