Day 01 [preface, introductory program, constant, variable]

Keywords: Java

day 01 [preface, introductory program, constant, variable]

Today's content

Java History of language development
Java Construction of development environment
 to write HelloWorld program
 Constants and variables

Teaching objectives

Can calculate the conversion between binary and decimal numbers
Be able to use common DOS commands
understand Java Cross platform implementation principle of language
 understand JDK and JRE Composition and function of
 Ability to configure environment variables JAVA_HOME
 Able to write HelloWorld Program compilation and execution
 Understand the meaning of keywords
 Understand the meaning of identifiers
 All types of constants can be defined
 understand Java Basic data type classification in
 It can define variables of 8 basic dataset types

Chapter I development Preface

1.1 overview of Java language

What is the Java language

Java language is a high-level programming language launched by Sun Corporation (Stanford University Network) in 1995. The so-called programming language is
Computer language, people can use programming language to give orders to the computer to let the computer complete the functions people need.

Development history of Java language

1995 year Sun Company release Java 1. 0 edition
1997 Released in Java 1. 1 edition
1998 Released in Java 1. 2 edition
2000 Released in Java 1. 3 edition
2002 Released in Java 1. 4 edition
2004 Released in Java 1. 5 edition
2006 Released in Java 1. 6 edition
2009 year Oracle Oracle acquisition Sun Company, and released in 2011 Java 1. 7 edition
2014 Released in Java 1. 8 edition
2017 Released in Java 9. 0 edition

What can the Java language do

Java language is mainly used in the development of Internet programs. Common Internet programs, such as tmall, jd.com, logistics system, online banking system, and service
Server background processing big data storage, query, data mining and so on also have many applications.

1.2 basic computer knowledge

Binary

The data in the computer is different from the data in people's life. People's life adopts decimal numbers, while the computer adopts binary numbers, which only contains

0 and 1 are two numbers, every two into one, 1 + 1 = 10. Each 0 or 1 is called a bit.

Let's look at the conversion calculation between decimal and binary data.

Convert decimal data to binary data: divide by 2 to get the remainder
Conversion of binary data to decimal data: using 8421 encoding
Tips:
In the binary number system, each 0 or 1 is a bit called bit(Bit).

byte

Byte is the smallest storage unit in our common computer. Any data stored by the computer is stored in bytes. Right click the file attribute,
We can see the byte size of the file.

8 bits (binary bits) 0000 - 0000 is expressed as 1 byte, written as 1 byte or 1 B.

8 bit = 1 B
1024 B = 1 KB
1024 KB = 1 MB
1024 MB = 1 GB
1024 GB = 1 TB

Common DOS commands

Java language beginners, learn some DOS commands, will be very helpful. DOS is an early operating system, which has been taken by Windows system
For our developers, we need to complete some things in DOS, so we need to master some necessary commands.

get into DOS Operation window
Press Windows+R Keyboard, open the operation window and enter cmd Enter to DOS Operation window for.
open DOS After the command line, you see a path c:\user It means that the disk we are operating on is c Plate.
Command operation symbol
Drive letter switching command name:
View current folder dir
Enter folder command cd Folder name
Exit folder command cd..
Exit to disk root cd\
Clear screen cls
Common commands

The second chapter is the construction of Java language development environment

2.1 Java virtual machine - JVM

2.1 Java virtual machine - JVM

JVM(Java Virtual Machine ): Java Virtual machine JVM,Yes, run all Java The hypothetical computer of the program is Java programmatic
 Operating environment, yes Java One of the most attractive features. We wrote Java Code, all running in JVM above.
Cross platform: the operation of any software must run on the operating system Java The software can run in any operating system
 In general, this feature is called Java The cross platform nature of the language. This feature is provided by JVM The program we write runs in JVM On, and JVM
 Run on the operating system.

As shown in the figure, the Java virtual machine itself does not have cross platform functions, and there are different versions of virtual machines under each operating system.

2.2 JRE and JDK

JRE (Java Runtime Environment) : yes Java The runtime environment of the program, including JVM And the core class library required by the runtime.
JDK (Java Development Kit): yes Java Program development kit, including JRE And tools used by developers.

If we want to run an existing Java program, we just need to install JRE.

If we want to develop a new Java program, we must install JDK.

Tips:
Three relationships: JDK > JRE > JVM

2.3 JDK 9 installation diagram

Tips:
The installation path should not contain Chinese and spaces.

2. 4 JAVA_ Configuration of home environment variable

Configure environment variable action

To develop Java programs, you need to use the tools provided in JDK. The tools are in the bin directory of JDK 9 installation directory.

To use these tools on the DOS command line, you must first enter the bin directory of the JDK. This process will be very troublesome.

These tools cannot be used without entering the bin directory of JDK, and an error will be reported.

In order to facilitate development, if we want to use JDK development tools in any directory, we must configure environment variables and the meaning of environment variables
Is to tell the operating system which directory the JDK development tools we use are in.

To configure environment variables

Windows 7, 8 version

1. Right click the computer and select properties
2. Select advanced system settings
3. On the Advanced tab, click environment variables
4. Click new to create a new environment variable
  1. Variable name input JAVA_HOME, enter the variable value into the JDK 9 installation directory C: \ Java 9 \ JDK - 9.0.1

  2. Select the Path environment variable, double-click or click Edit

  3. At the beginning of the variable value, type% JAVA_HOME%\bin; Semicolons must be written in English.

  4. After the configuration of environment variables is completed, restart the DOS command line, enter javac command in any directory, and run successfully.

Windows 10 version

1. File explorer -- > right click this computer -- > select properties
2. Select advanced system settings -- > select environment variables
3. Click new system variable below to create a new environment variable, and enter the variable name into JAVA_HOME, enter the variable value into the installation directory of JDK 9
D:\ 02 _DevelopTools\Java\jdk-^9.^0.^1
  1. Select the Path environment variable, double-click or click Edit

  2. Click new and type% JAVA_HOME%\bin, must be in English format. Select the row, move it up to the top, and click OK.

  3. After the environment variable configuration is completed, restart the DOS command line and enter the javac command in any directory.

Chapter 3 ^ HelloWorld getting started^

3.1 description of program development steps

The development environment has been built, and we can develop our first Java program.

Three steps of Java program development: writing, compiling and running.

3.2 writing Java source program

1. stay d:\day 01 Create a new text file in the directory, and change the complete file name to HelloWorld.java,Where the file name is HelloWorld,after
 Prefix name must be.java^ . 
2. Open with Notepad
use notepad++Notepad software.
3. Type text in the file and save it with the following code:
File name must be HelloWorld ,Ensure that the file name and class name are consistent, and pay attention to case.
Each letter and symbol as like as two peas.

The first HelloWord source program is written, but this file is written by the programmer. The JVM can't understand it and can't run, so I
You must compile the prepared Java source file into a bytecode file that the JVM can understand.

3. 3 compiling Java source files

On the DOS command line, enter the directory of Java source files and compile with javac command.

Command:

give an example:

After the compilation is successful, there is no prompt on the command line. Open the d:\day 01 directory and find that a new file HelloWorld.class is generated
The compiled file is the Java executable file, called bytecode file. With bytecode file, you can run the program.

Java Compiler for source files javac.exe,stay JDK Of the installation directory bin Directory. However, due to the configuration of environment variables, you can use any object
 Record the use.

3.4 running Java programs

On the DOS command line, enter the directory of java source files and run them with java commands.

Command:

give an example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
javac Java Source file name.Suffix
javac HelloWorld.java
java Class name
java HelloWorld
java HelloWord Don't write, don't write, don't write .class
Java program.class Running tools for files java.exe ,stay JDK Of the installation directory bin Directory. However, because the environment variable is configured, you can
 To be used in any directory.

3.5 introduction procedure description

Compiling and running are two different things

Compilation: refers to the compilation of Java Source files translated into JVM cognitive class File, in the process, javac The compiler will check us
 Whether the written program has errors, errors will be prompted. If there are no errors, it will be compiled successfully.
Operation: refers to class Documents to JVM To run, at this time JVM Will execute the program we wrote.

About main method

main Methods: it is called the main method. The writing method is fixed and cannot be changed. main Method is the entry point or starting point of the program, no matter how much we write
 Fewer programs, JVM When running, it will start from main Method starts execution here.

3.6 add comment s

Note: it is the explanation and explanation of the code. The goal is to make it easier for people to understand the code. It is necessary to add comments to the code
Yes, it does not affect the compilation and operation of the program.
Java There are single line comments and multi line comments in
 Single line comment to //Beginning and ending of line feed
 Multiline comment to /*Start with*/end

3.7 keywords

Keyword: in the program, Java Defined words have special meanings.
HelloWorld In the case, the keywords that appear are public^ ,class^ ,^ static^ ,^ void^ Wait, these words have been
Java Well defined, all lowercase letters, notepad++Special color in.
There are many keywords. You can't memorize them by rote. You can remember where you learn.

3.8 identifier

Identifier: refers to the content defined by ourselves in the program. For example, the name of the class, the name of the method and the name of the variable are identifiers.
HelloWorld In the case, the identifier that appears has a class name HelloWorld . 
Type meaning data example
Integer constants all integers 0, 1567, - 9
Decimal constant all decimals 0. 0, - 0. 1, 2. 55
Character constants are enclosed in single quotes,Only one character can be written,Must have content 'a' , ' ', 'good'
String constants are enclosed in double quotes,Multiple characters can be written,You can also not write "A" ,"Hello" ,"Hello" ,""
Boolean constants have only two values (explained in process control) true , false
An empty constant has only one value (explained in the reference data type) null
Naming rules: mandatory requirements
The identifier can contain 26 English letters (case sensitive), 0 - 9 numbers, $(dollar sign) and_ (underline).
Identifier cannot start with a number.
Identifier cannot be a keyword.
Naming conventions: soft recommendations
Class name specification: the first letter is capitalized, and the first letter of each subsequent word is capitalized (big hump type).
Method name specification: the first letter is lowercase, and the first letter of each subsequent word is uppercase (small hump type).
Variable name specification: all lowercase.

Chapter IV constants

4.1 General

Constant: refers to the fixed data in the Java program.

4.2 classification

4.3 practice

Requirements: output various types of constants.
public class ConstantDemo {
	public static void main(String[] args){
	//Output integer constant
		System.out.println( 123 );
		//Output decimal constant
		System.out.println( 0.125 );
		//Output character constant
		System.out.println('A');
		//Output Boolean constant
		System.out.println(true);
		//Output string constant
		System.out.println("Hello Java");
	}
}
Data type keyword memory usage value range
Byte type byte 1 Bytes - 128 ~ 127
Short  short 2 Bytes - 32768 ~ 32767
integer int(Default) 4 bytes - 231 Power~ 2 The 31st power of- 1
Long integer long 8 Bytes - 2 The 63rd power of~ 2 The 63rd power of- 1
Single-precision floating-point  float 4 Byte 1. 4013 E- 45 ~ 3. 4028 E+ 38
Double precision floating point number double(Default) 8 bytes 4. 9 E- 324 ~ 1. 7977 E+ 308
character char 2 Bytes 0 - 65535
Boolean type boolean 1 Bytes true,false

Chapter V variables and data types

5.1 variable overview

Variables: constants are fixed data, so the amount that can change in the program is called variables.
In mathematics, letters can be used instead of numbers,for example x= 1 + 5 Or 6 =x+ 5 . 
In the program, you can use letters to save numbers for operation, improve the computing power, and solve more problems. such as x Save 5, x Also
 To save 6, so x The saved data can be changed, that is, the variables we explain.

Java requires that a variable can only save one data at a time, and the data type to be saved must be specified.

5.2 data type

Data type classification

Java data types fall into two categories:

Basic data types: including integer, floating point number, character and Boolean.
Reference data type: including class, array and interface.

Basic data type

Four categories and eight basic data types:
Java Default type in: integer type is int ,The floating point type is double . 

5.3 definition of variables

}
The format of variable definition includes three elements: data type, variable name and data value.

format

practice

Define variables of all basic data types. The codes are as follows:
long Type: it is recommended to add after the data L express.
float Type: it is recommended to add after the data F express.

5.4 precautions

Variable name: variable names cannot be the same within the same braces.
Variable assignment: defined variable. It cannot be used without assignment.
Data type variable name = Data value;
public class Variable {
    public static void main(String[] args) {
        //Define byte type variables
        byte b = 100;
        System.out.println(b);
        //Define short integer variables
        short s = 1000;
        System.out.println(s);
        //Defining integer variables
        int i = 123456;
        System.out.println(i);
        //Define long integer variables
        long l = 12345678900L;
        System.out.println(l);
        //Define single precision floating point variables
        float f = 5.5F;
        System.out.println(f);
        //Define double precision floating point variables
        double d = 8.5;
        System.out.println(d);
        //Defining Boolean variables
        boolean bool = false;
        System.out.println(bool);
        //Define character variables
        char c = 'A';
        System.out.println(c);
    }
}

Posted by Imagine3 on Fri, 17 Sep 2021 02:49:59 -0700