[Java from entry to mastery | 1] From features to the first Hello World program

written in front

In the field of computer programming, Java is a widely used high-level programming language. It is popular among developers for its strong cross-platform performance, rich library and ecosystem, and easy-to-learn syntax. This article will guide you step by step to understand the characteristics of Java, how to install and configure the development environment, and how to write your first Java program.

1. Java programming language and its characteristics

Java is an object-oriented programming language first introduced by Sun Microsystems in 1995. Following are some key features of Java −

  • Cross-platform: Java programs have the same behavior on different operating systems, thanks to the existence of the Java Virtual Machine (JVM), which compiles Java code into bytecode and then executes it on different platforms.
  • Object-oriented: Java encourages the use of object-oriented programming paradigms to organize code through classes and objects to achieve code maintainability and reuse.
  • Rich class library: Java provides a large number of standard class libraries, covering various fields from basic data structures to network programming, which simplifies the development process.
  • Security: Java has powerful security features, including class loaders, security managers, etc., making it more reliable in the network environment.
  • Multi-threading support: Java has built-in multi-threading support to make it easier for developers to write concurrent programs.
  • Dynamics: Java supports features such as reflection and dynamic proxies that allow classes and objects to be inspected and manipulated at runtime.

Second, install and configure the Java development environment

Before you can start writing Java programs, you need to install and configure a Java development environment. Here are the basic steps:

  1. Download JDK (Java Development Kit): visit Oracle official website or OpenJDK project, and download the JDK version suitable for your operating system.
  2. Install JDK: Execute the installer and follow the instructions to complete the installation process. During installation, you can choose to install JRE (Java Runtime Environment) and other components.
  3. Configure environment variables: Add the JDK installation path to the system environment variables so that the system can find the Java executable file.
  4. Verify the installation: Open a terminal or command prompt and run java -versionthe command to ensure that the installation was successful and that the correct Java version information is displayed.

3. The first Java program

code example

Hello World In Java, writing and running your first program is a traditional way to get started. The following is an example of a simple Hello World program:

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

code explanation 

  • public class HelloWorld: defines a HelloWorldpublic class named .
  • public static void main(String[] args): mainThe method is the entry point of the Java program, it is the starting point of program execution.
  • System.out.println("Hello, World!");: This line of code outputs text to the console.

4. Data types and variables

In Java, data types and variables are the basic elements of building programs. Data types define the nature of variables and the types of data that can be stored, and variables are named containers for storing and manipulating data. Explain data types and variables in Java in more detail.

type of data:

Java data types can be divided into two categories: basic data types and reference data types.

basic data type

Basic data types (Primitive Data Types): Java's basic data types are predefined, store data values ​​​​directly, and do not refer to other objects. The basic data types are divided into the following categories:

  • Integer type:

    • byte: Occupies 8 bits, representing an integer ranging from -128 to 127.
    • short: Occupies 16 bits, representing an integer ranging from -32,768 to 32,767.
    • int: Occupies 32 bits, representing an integer ranging from -2^31 to 2^31-1.
    • long: Occupies 64 bits, representing an integer ranging from -2^63 to 2^63-1.
  • Floating point type:

    • float: Occupies 32 bits and is used to represent values ​​with decimals. The precision is about 6-7 decimal places.
    • double: Occupies 64 bits, a higher-precision floating-point number type, and the precision is about 15 decimal places.
  • Character type:

    • char: Occupies 16 bits and is used to represent a single Unicode character.
  • Boolean type:

    • boolean: Used to represent logical values, there are only two values: trueand false.

reference data type

Reference data types (Reference Data Types): Reference data types are references to objects, they do not directly store data, but store the address of the object. Reference data types in Java include:

  • Class: An object created by defining a class.
  • Interface (Interface): used to define the specification of the method, the class can implement the interface.
  • Array (Array): A container that stores multiple data elements of the same type.

variable:

In Java, variables are named containers for storing data. When declaring a variable, you need to specify the data type and name of the variable, and you can optionally assign an initial value to the variable. Variable naming rules include:

  • Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
  • Subsequent characters can be letters, numbers, underscores, or dollar signs.
  • Variable names are case sensitive.

Examples: Here are some examples showing how to declare and use variables of different types:

public class DataTypesAndVariables {
    public static void main(String[] args) {
        // 声明整数变量
        int age = 25;

        // 声明浮点数变量
        double salary = 50000.75;

        // 声明字符变量
        char grade = 'A';

        // 声明布尔变量
        boolean isStudent = true;

        // 声明字符串变量
        String name = "John Doe";

        // 声明引用类型变量(数组)
        int[] numbers = {1, 2, 3, 4, 5};
    }
}

In the above example, variables of different types are declared and given initial values. Note that the data type of a variable determines the range and type of data that the variable can store.

In summary, data types and variables are fundamental concepts in Java programming and they play a key role in building programs. Understanding and properly using different data types and variables will help you write more flexible, maintainable and extensible Java programs.

V. Conclusion

This article describes the

  1. Features of the Java programming language
  2. Explains how to install and configure a Java development environment
  3. Demonstrates how to write and run Java code through a simple Hello World program
  4. java data types and variables

From this starting point, you can continue to learn more concepts and technologies of Java, and gradually build more complex applications.

Java's wide range of applications makes it one of the indispensable skills for both novice and experienced developers.

Guess you like

Origin blog.csdn.net/weixin_36755535/article/details/132281091