1. Java overview
1.1 Java Language Background
Language: means of communication between people
Computer language: a special language used to communicate information between humans and computers
Java language is a computer language introduced by Sun Corporation (Stanford University Network) in 1995
The Father of Java: James Gosling
In 2009, Sun was acquired by Oracle, so we can visit oracle’s official website: www.oracle.com
Three versions of the Java language:
JavaSE: Standard version of the Java language, used for desktop application development and based on the other two versions
JavaME: A small version of the Java language for embedded consumer electronic devices
JavaEE: Enterprise version of the Java language for Web-oriented Web development
1.2 Principles of Java cross-platform
Instead of running Java programs directly, the Java compiler compiles Java source programs into platform-independent bytecode files (class files), which are then interpreted and executed by the Java Virtual Machine (JVM). Therefore, in different operating systems, you only need to install different Java VMS to achieve cross-platform Java programs.
Analysis: Is the JVM itself cross-platform?
A: The JVM itself does not allow cross-platform applications. Java applications do.
1.3 the JRE and JDK
Java Virtual Machine (JVM) : Indicates a Java Virtual Machine
Java Runtime Environment (JRE), a Java Runtime Environment, contains the JVM and Java core libraries (Java APIS).
The Java Development Kit (JDK) is called the Java Development tool, which contains the JRE and Development tools
Summary: We just need to install the JDK, which contains the Java runtime environment and virtual machine.
1.4 JDK Download and Installation (Application)
1.4.1 download
Obtain the JDK from the official website
www.oracle.com
Note: You need to download the JDK version for different operating systems.
1.4.2 installed
Foolproof installation, next step. However, the default installation path is in C:\Program Files. To facilitate unified management, you are advised to change the installation path and install all develop-related software in one directory, for example, E:\ Develop.
Note: The installation path should not contain special characters such as Chinese characters or Spaces.
1.4.3 Installation Directory of the JDK
Directory name | instructions |
---|---|
bin | This directory stores various JDK tool commands. Javac and Java are in this directory. |
conf | JDK configuration files are stored in this directory. |
include | This path holds platform-specific header files. |
jmods | This is where various JDK modules are stored. |
legal | This directory stores the authorization documents of each JDK module. |
lib | This directory contains some supplementary JAR packages for JDK tools. |
2. First demo
2.1 Common DOS Commands
Before we get to the INTEGRATED Development Environment, we need to compile and run Java programs using a command line window, so we need to know some common DOS commands.
1. Open the command line window: win + r open the running window, enter CMD, and press Enter.
2. Common commands and their functions
operation | instructions |
---|---|
Drive letter name: | Drive letter switch. E: Press Enter to switch to drive E. |
dir | View the contents of the current path. |
CD catalogue | Go to the single-level directory. cd itheima |
cd .. | Rollback to the previous directory. |
CD directory 1\ directory 2… | The multi-level directory is displayed. cd itheima\JavaSE |
cd \ | Go back to the drive letter directory. |
cls | Black screen. |
exit | Exit the command prompt window. |
2.2 Configuring Path Environment Variables
2.2.1 Why To Configure Environment Variables
To develop Java programs, you need to use development tools (such as javac.exe and java.exe) provided by the JDK. These tools are in the bin directory of the JDK installation directory. It is not possible to put all Java files in the BIN directory of the JDK, so the purpose of configuring environment variables is to make java-related commands in the bin directory available in any directory.
Steps:
1. Right-click “Computer “, choose” Properties “, go to the properties screen, choose “Advanced System Settings”
Step 2 Select “Advanced”
3. Create the JAVA_HOME system variable and enter the JDK directory
4. In the Path environment variable, create a Path for storing the bin file in the JDK directory
2.3 HelloWorld Case (Application)
The HelloWorld case is the output of the line “HelloWorld” on the computer screen.
It is customary for various computer languages to use this case as a first demonstration.
2.3.1 Java program development and running process
Developing Java programs requires three steps: writing programs, compiling programs, and running programs.
2.3.2 Preparation of HelloWorld case
1. Create a text file and rename it helloWorld.java.
2. Open the HelloWorld. Java file with Notepad and type the program content.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld"); }}Copy the code
2.3.3 Compilation and execution of HelloWorld case
Save the file, open the command line window, switch the directory to the directory where the Java file resides, compile the Java file to generate the class file, and run the class file.
Compile: javac file name. Java
Example: javac helloworld.java
Execute: Java class name
Example: Java HelloWorld
2.4 HelloWorld Case details
2.5 HelloWorld FAQ (Understanding)
2.5.1 BUG
A hidden defect or problem in a computer system or program is called a bug.
2.5.2 BUG resolution
1, have the ability to identify bugs: look more
2. Have the ability to analyze bugs: think more and look up more information
3. Ability to solve bugs: try more and summarize more
2.5.3 Common Questions about HelloWorld Case
1. Illegal characters. Symbols in Java are in English format.
2. Case. The Java language is case sensitive (case sensitive).
3. Display the extension of the file in the system to avoid the appearance of helloworld.java.txt file.
4. The Java file name after the command is compiled must contain the file suffix. Java
5. The class file name (class name) after the command is executed does not contain file suffixes
2.6 installation and use of Notepad++ (application)
2.6.1 why use Notepad++
Notepad++ is more powerful than notepad in Windows. In addition to making plain text files, it is also very suitable for writing computer program code. Notepad++ has line numbers to quickly locate problems, syntax highlighting, code folding, and more. And it’s free.
2.6.2 Notepad++ software installation
Installation: foolproof installation, always the next step. It is also recommended to install it in a unified development directory, such as E:\ Develop.
2.6.3Notepad++ software configuration
Once installed, make a simple configuration for ease of use: change the default language and encoding.
3. Java basic syntax
3.1 annotations
Comments are words that explain code and can improve the readability of a program, so it is important to add necessary comments to your program. There are three types of annotations in Java:
Single-line comment. Single-line comments are formatted with //, and the text from // to the end of the line is the comment text.
// This is a single line comment text
Copy the code
Multi-line comments. Multi-line comments are formatted to enclose a long comment with /* and */.
/* This is multi-line comment text */Note: Multi-line comments cannot be nested.Copy the code
Documentation comments. Document comments start with /** and end with */. (Later)
3.2 Keywords (Understanding)
Keywords are words that have been given special meaning by the Java language.
Keyword features:
All the letters of the keyword are lowercase.
Common code editors highlight keywords, such as public, class, static, and so on.
3.3 constant
Constant: An amount whose value cannot be changed during the execution of a program.
Class constants in Java:
String constants Multiple characters (can contain zero, one, or more) enclosed in double quotation marks, such as “a”, “ABC”, “China”, etc
Integer Constant An integer, such as -10, 0, or 88
Decimal Indicates a decimal number, such as -5.5, 1.0, or 88.88
Character constant A character enclosed in single quotation marks, for example: ‘a’, ‘5’, ‘B’, ‘middle’, etc
Boolean constants Boolean values that indicate true or false and have only two values true and false
Null constant A special value, null, of the value null
Except for empty constants, all other constants can be output directly using output statements.
public class Demo {
public static void main(String[] args) {
System.out.println(10); // Output an integer
System.out.println(5.5); // Outputs a decimal number
System.out.println('a'); // Output one character
System.out.println(true); // Print Boolean true
System.out.println("Welcome to the dark horse programmer."); // Outputs a string}}Copy the code
3.4 Introduction to variables
A variable is a storage space in memory that holds an amount (data) that changes frequently.
Variable definition format:
Data type variable name = data value;
Data type: Adds type restrictions to the data stored in the space. An integer? The decimal?
Variable name: The name you want to give your space, no difficulty
Data value: The value to be stored in the space, no difficulty
3.5 Data Types
3.5.1 Computer storage unit
We know that computers can be used to store data, but whether it is memory or hard disk, the smallest unit of information on the computer storage device is called a “bit”. We also call it a “bit” and it is usually represented by a lowercase letter “B”. The most basic unit of storage in a computer is called a byte,
Usually represented by a capital letter “B”, a byte is made up of eight consecutive bits.
In addition to bytes, there are some common storage units, which are converted in the following units:
1B (byte) = 8 bits
1KB = 1024B
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
3.5.2 Data Types in Java
Java is a strongly typed language, and data in Java must be explicitly typed. Data types in Java include basic data types and reference data types.
Basic data types in Java:
The data type | The keyword | Memory usage (bytes) | Value range |
---|---|---|---|
Integer types | byte | 1 | – 128 ~ 127 |
short | 2 | – 32768 ~ 32767 | |
Int (default) | 4 | Minus 2 to the 31st to 2 to the 31st minus 1 | |
long | 8 | Minus 2 to the 63rd to 2 to the 63rd minus 1 | |
Floating point types | float | 4 | Negative: -3.402823e +38 to -1.401298e-45 positive: 1.401298E-45 to 3.402823E+38 |
Double (default) | 8 | Negative: -1.797693e +308 to -4.9000000e-324 positive: 4.9000000E-324 to 1.797693E+308 | |
Character types | char | 2 | 0-65535. |
Boolean type | boolean | 1 | True, false |
Description:
E plus 38 means times 10 to the 38, and e minus 45 means times 10 to the minus 45.
In Java, integers are of type int by default and floating point numbers are of type double by default.
Data types are divided into basic data types and reference data types. Basic data types are numeric and non-numeric. Numeric data types are integers (byte, short, int, long), floating points (double), and characters (char). Reference data types are classified into class, interface, and array ([]).
3.6 variable
3.6.1 Definition of variables
Variable: An amount whose value can change during the execution of a program.
Essentially, a variable is a small area of memory whose value can vary within a certain range.
Variable definition format:
Datatype variable name = initialization value;// Declare variables and assign values
int age = 18;
System.out.println(age);
Copy the code
Or (extension)
// Declare first, then assign.Data type variable name; Variable name = initialization value;double money;
money = 55.5;
System.out.println(money);
Copy the code
You can also (extend)
Define multiple variables of the same data type on the same line, separated by commas. However, it is not recommended to use this method to reduce the readability of the program.
int a = 10, b = 20; // Define variables a and b of type int, separated by commas
System.out.println(a);
System.out.println(b);
int c,d; // Declare variables c and d of type int, separated by commas
c = 30;
d = 40;
System.out.println(c);
System.out.println(d);
Copy the code
3.6.2 Variable Modification
int a = 10;
a = 30; // Change the value of a variable
System.out.println(a);
Copy the code
If no data type is added before a variable, the value of an existing variable is modified.
3.7 Precautions for variables
-
Variable names cannot be repeated in the same pair of curly braces. A statement can define multiple variables, but they need to be separated by commas.
-
Variables must be initialized (assigned) before they can be used.
-
When defining a variable of type long, you need to add L after the integer (upper case is recommended). Because integers default to int, integers that are too large may exceed the int range.
When defining a variable of type float, you add F after the decimal (upper case is recommended). Because the default type of floating-point numbers is double, the value range of double is greater than float, and the types are incompatible.
-
The scope of a variable is valid only in the brace in which it is located.
3.8 Keyboard Input
We can use the Scanner class to get user input. The steps are as follows:
1. Guide package. The Scanner class is in the java.util package, so you need to import it. The statement that guides the package needs to be defined on top of the class.
import java.util.Scanner;
Copy the code
2. Create Scanner object.
Scanner sc = new Scanner(System.in);// Create Scanner object, sc represents the variable name, all other immutable
Copy the code
3. Receive data
int i = sc.nextInt(); // Returns the keyboard value as an int.
Copy the code
Example:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// Create an object
Scanner sc = new Scanner(System.in);
// Receive data
int a = sc.nextInt();
// Output dataSystem.out.println(a); }}Copy the code
3.9 the identifier
Identifiers are names that users use to program classes, methods, variables, constants, and so on. (Self-naming is an identifier.)
Java identifier composition rules:
The value consists of letters, digits, underscores (_), and dollar signs ($). The first character cannot be a digit.
You cannot use Java keywords as identifiers.
Identifiers are case sensitive (case sensitive).
Naming conventions for identifiers in Java:
Small camel name: variable name, method name
Capitalize the first letter of each word starting with the second.
Big camel name: class name
Capitalize the first letter of every word.
In addition, it is best to name identifiers by name
For example, username and studentNumber.