The latest series of Java basic courses--Day01-Java basic introduction

Author Homepage: Programming Compass

About the author: High-quality creator in the Java field, CSDN blog expert
, CSDN content partner, invited author of Nuggets, Alibaba Cloud blog expert, 51CTO invited author, many years of architect design experience, resident lecturer of Tencent classroom

Main content: Java project, Python project, front-end project, artificial intelligence and big data, resume template, learning materials, interview question bank, technical mutual assistance

Favorites, likes, don't get lost, it's good to follow the author

Get the source code at the end of the article

Foreword:

Starting today, we will continue to output "Basics of Java Language for Object-Oriented Programming", explaining from the most basic Java grammar foundation to advanced usages of Java such as multithreading, network programming, and database programming. It is currently free to share, and later advanced courses may be available For a fee, friends who are interested in learning Java can quickly subscribe for free to learn.

​ day01 - Introduction to Java Basics

【Teaching content】

1-1 The history, characteristics and specifications of the Java language

1-2 Structure and specification of Java program

1-3 Introduction, installation and use of Java virtual machine

1-4 Data types, keywords and identifiers

1-5 Constants, variables, local variables

1-6 Data type conversion, operators and expressions

[ Key points and difficulties ] The focus is on the understanding of data types, constants, variables, and local variables. The hard part is data type conversion.

[Ability Requirements] (1) Understand the development history and characteristics of the Java language (2) Master the construction of the Java language development environment (3) Master the basic grammar of the Java language (4) Cultivate code habits that conform to enterprise specifications

1. Java background knowledge

Before we officially start, let's understand the background knowledge of Java, so that you can talk about it when you talk about Java with everyone in the future.

1.1 History of the Java language

  • Which company's product is Java?

    Java is an advanced computer programming language launched by Sun (Stanford University Network, Stanford University Network) in 1995 . But in 2009, Sun was acquired by Oracle (Oracle), so the Java language is currently all products of Oracle.

  • Where did the Java name come from?

    The name of this language in the early days was actually not called Java. It was called Oak (meaning oak tree) at that time. Why was it called Oak tree? The reason is that many oak trees were planted at the entrance of Sun Company, but later, when the trademark was registered, the Oak trademark had already been registered by other companies, so it was later renamed Java. So some people wonder why it is called Java? Java is an island in India, which is rich in coffee. It may be because the programmers of their company like to drink coffee, so they changed their name to Java.

  • Who is the founder of Java?

  • After talking about the origin of the Java name, let's talk about who is the ancestor of Java? There are many co-founders of Java, but the founder of Java generally recognized by the industry is James Gosling , known as the father of Java

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-5rP8ehCA-1689489428325)(assets/1660152660273.png)]

1.2 What Java can do

After understanding the history of the Java language, the next question that everyone is more concerned about may be what can Java do?

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-rrwGLlYa-1689489428327)(assets/1660141834075.png)]

In fact, Java can do a lot of things, it can do desktop application development, enterprise Internet application development, mobile application development, server system development, big data development, game development and so on.

1.桌面应用开发:能够在电脑桌面运行的软件
	举例:财务管理软件、编写程序用的IDEA开发工具等,可以用Java语言开发
	
2.企业级应用开发:大型的互联网应用程序
	举例:淘宝、京东、大家每天都用的tlias教学管理系统等

3.移动应用开发:运行的Android手机端的软件
	举例:QQ客户端、抖音APP4.服务器系统:应用程序的后台(为客户端程序提供数据)
	举例:服务器系统为用户推荐那你喜爱的视频

5.大数据开发:大数据是一个互联网开发方向
	举例:目前最火的大数据开发平台是Hadoop,就是用Java语言开发的

6.游戏开发:游戏本质上是给用户提供娱乐的软件,有良好的交互感受
	举例:我的世界MineCraft就是用Java语言开发的

Although Java can do a lot of things, not every direction is recognized by the market (for example, it is inconvenient to develop desktop applications in Java language, and it is recommended to use C# language). At present, the mainstream development direction of Java is to use Java to develop enterprise-level Internet applications (many companies' OA systems, customer relationship management systems, including Chuanzhi Podcast use teaching implementation management systems are all developed in Java language)

1.3 Java technology system

After talking about what the Java language can do, let's introduce the Java technology system to the students. The so-called technical system means that Java provides different technical versions to meet different application scenarios. There are mainly three versions.

  • Java SE (Java Standard Edition): called the Standard Edition, it is the basis of the latter two versions, that is, to learn the latter two versions, you must first learn JavaSE. What our basic class is learning at this stage is the technology in this version .

  • Java EE (Java Enterprise Edition): called the Enterprise Edition, it is a set of solutions for enterprise application development. In the following employment class courses, we will mainly learn the techniques in this version .

  • Java ME (Java Micro Edition): called the small version, it provides a solution for developing applications for mobile devices. At present, it has not been recognized (eliminated) by the market, and it has been replaced by application development based on the Android system .


The relevant background of the Java language is introduced here, and friends don’t need to remember these contents deliberately, just a simple understanding is enough. Below we briefly review

1.Java是什么?
	答:Java是一门高级编程语言
	
2.Java是哪家公司的产品?
	答:Java以前是Sun公司的产品,现在Java是属于Oracle公司的产品
	
3.Java之父是谁?
	答:詹姆斯●高斯林
	
4.Java主流的开发方向是什么?
	答:企业级互联网应用开发
	
5.Java技术平台有哪些?
	答:JavaSE(标准版)、JavaEE(企业版)、JavaME(小型版)

2. Java quick start

In the last chapter, we introduced the relevant background of Java to our friends. Do you want to use Java immediately? Don't worry, we have to install the Java development environment before we can use the Java language to develop programs (ps: just like you need to install WeChat before you can use WeChat to chat with friends).

The Java development environment mentioned here is actually a piece of software officially provided by Java, called JDK (full name is Java Develop Kit), which translates to Java Development Kit. We have to go to the official website to download JDK first, and then install it on our own computer before we can use JDK on our own computer to develop Java programs.

There are many versions of JDK. The following figure shows the history of JDK version update. The version marked with LTS is the long-term support version (meaning that Oracle will update it from time to time). At present, the most used version in the company is the JDK8 version. In the current set of courses, we will use the JDK17 version in order to introduce some new features.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-2kAvvSwN-1689489428329)(assets/1660143538211.png)]

The following has provided friends with detailed screenshots of the JDK download and installation process. You only need to follow the steps.

2.1 JDK download and installation

  • JDK download

This is the official URL for JDK download https://www.oracle.com/java/technologies/downloads/, you need to copy the URL to the address bar of the browser and press Enter

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-WpChRA23-1689489428331)(assets/1660527717279.png)]

After entering the URL, select the JDK17 version, find the Windows tab, and select the x64 Installer version. As shown below

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-UO3OAJ4y-1689489428333)(assets/1660527981411.png)]

After the download is complete, a JDK installation package will appear in the directory you downloaded, as shown in the figure below

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-fUgYZls2-1689489428334)(assets/1660528307458.png)]

At this point, the download of JDK is completed, and then you need to follow the steps below to complete the JDK installation.

  • JDK installation

Double-click the installation package, follow the guidance in the figure below, and click Next to install. It should be noted that after installing JDK, unlike installing QQ, an icon will be displayed on the desktop. After JDK is installed, there will be no icon on the desktop! ! !

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-0Vx822Bi-1689489428335)(assets/1660144855615.png)]

How to verify that the installation was successful?

Didn't you just remember the installation directory? Do you remember? If you have modified the directory yourself, open the directory you modified yourself (Yeah!! Forgot o(╥﹏╥)o, then I can’t help you, who told you not to listen carefully); if you haven’t modified the installation directory, By default in C:\Program Files\Java\jdk-17.0.3the directory.

Open the bin directory of the JDK installation directory in the file explorer, and you will find two command tools , which are the compilation tools and running toolsjavac.exe java.exe provided by the JDK , as shown in the following figure

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-btHWtfL5-1689489428338)(assets/1660145259521.png)]

Let's use javac.exe java.exethese two tools now to test whether the JDK is available

  1. Step 1: In the bin directory of JDK, enter cmd in the address bar and press Enter

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ODKLWaqR-1689489428340)(assets/1660529458474.png)]

After entering cmd and pressing Enter, a black window will appear, which is called the command line window in professional terms

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-MKH9KuH1-1689489428342)(assets/1660529493477.png)]

  1. Step 2: Enter Enter in the command line window javac -version, and then enter java -versionEnter

    If the following red box prompts the correct version number, which is consistent with the JDK version number we installed, it means that the JDK installation is successful

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bTAJrNSS-1689489428344)(assets/1660145482256.png)]
insert image description here

After completing the above steps, congratulations to my friend _ , you already have a Java development environment on your computer, and you can develop Java programs next.

2.2 cmd common commands

javac -versionTo test whether the JDK is installed successfully, you need to enter and in the black window. java -versionThis is actually the command for the JDK to check the version numbers of the compilation tool and the running tool.

This way of entering commands and interacting with the computer is called command line interaction. In other words, you can use commands to direct the computer to do things. Next, let's learn about several common commands in Windows systems, which may be used later.

The following are common commands and functions of the Windows system, you can try them yourself. It should be noted that after each command is typed, pressing Enter immediately means that the naming is executed.

E:  //切换到E盘
cd [目录]        //进入指定的目录
cd ..         //退回到上一级目录
cd /         //退回到根目录
dir             //显示当前目录下所有的内容
cls             //清空屏幕

2.3 Java entry program

We have installed the JDK in the previous section, and next, we will officially start developing the first entry-level Java program. According to international practice, the first case of learning any programming language is called Hello World , which means hello to the world and opens the door to communicate with the world from the program.

Steps to write a Java program

Writing a Java program requires 3 steps: write code, compile code, run code

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-jEvu8LJQ-1689489428346)(assets/1660145843138.png)]

  • Write code: any text editor can write code, such as Notepad that comes with Windows system
  • Compile code: convert the source code (.java file) that people can understand into a bytecode file (.class file) that the Java virtual machine can execute
  • Run the code: hand over the bytecode file to the Java virtual machine for execution

Write your first Java starter program

Follow the steps provided below to complete the writing, compiling and execution of the first Java entry program step by step.

Step 1 : Create a new text file with the suffix .java HelloWorld.java, and write the code in Notepad as follows.

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

Step 2 : Enter HelloWorld.javathe directory where the file is located, enter cmd in the address bar and press Enter to open the command line window here.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-fYqek4jF-1689489428348)(assets/1660146387184.png)]

Compile: Enter the compile command in the command line window javac HelloWorldto complete the compilation, and a HelloWorld.classfile will be generated after compilation.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bJzkRvkS-1689489428350)(assets/1660146644956.png)]

Step 3 : Then enter it java HelloWorldto run, and the results are as follows.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-0ZafHv2b-1689489428352)(assets/1660146816170.png)]

2.4 Common problems in Java programs

Just now when the friends wrote the first HelloWorld program, was it easy to report an error? I have observed that when writing code for the first time, 90% of the students will have some minor problems, such as writing wrong words! Write one less bracket! etc! What I want to tell you is that it is normal to write wrong code. A programmer who has made all mistakes is a real programmer .

Below we summarize the common problems in the program. Please pay attention to these problems when writing code

  • File extensions for Windows unchecked
  • I wrote the code, but forgot to save it
  • The file name and class name do not match.
  • English capitalization is wrong, words are spelled wrong, there are Chinese symbols, and the main method cannot be found.
  • Parentheses do not match.
  • Improper use of compilation or execution tools.

  • file extension does not open

If the checkmark of the file extension in the figure below is not checked, the file you create will be an ordinary text file (.txt) file instead of a java file.

The correct way is to check the file extension

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-D8KCqPaO-1689489428364)(assets/1660147216279.png)]

  • The file name and the class name do not match

You can see the picture below, the file name is HelloWorld, but the class name is Helloworldthe difference? One is an uppercase W and the other is a lowercase w. If you don't look carefully, you really can't see it.

**The correct way to write is the name of the file HelloWorld, and the name of the class is also calledHelloWorld**

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-hgoynE55-1689489428366)(assets/1660531741851.png)]

  • Wrong capitalization of words

In the picture below, the words string and system are not misspelled, here is strictly case-sensitive

The correct way to write is String and System

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-PJ8OunLq-1689489428367)(assets/1660531915677.png)]

  • The main method is wrong

As shown in the figure below, the name of the main method is written mian, which is wrong.

The correct way to write the main method: must be public static void main(String[] args){}, not a single letter can be wrong.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ShtXZV0U-1689489428368)(assets/1660532147208.png)]

  • wrong punctuation

In the figure below, print the semicolon at the end of the statement, and write the Chinese semicolon successfully

The correct way of writing should be an English semicolon. ; If you don’t look carefully, you really can’t see the difference. Be careful.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-rkIeGl9i-1689489428371)(assets/1660532298281.png)]

2.5 Composition of JDK

In the previous lessons, we have installed the JDK, and developed a Java entry program, compiled with the javac command, and run with the Java command, but there is not much introduction to the execution principle of the Java program.

Next, we will introduce the composition of JDK and the principle of cross-platform, which will help students understand the execution process of Java programs.

JDK consists of JVM, core class library, and development tools, as shown in the figure below

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-YduutRjm-1689489428372)(assets/1660147531310.png)]

Let's introduce what each part of the JDK is used for.

- 什么是JVM?
    答:JDK最核心的组成部分是JVMJava Virtual Machine),它是Java虚拟机,真正运行Java程序的地方。
    
- 什么是核心类库?
	答:它是Java本身写好的一些程序,给程序员调用的。 Java程序员并不是凭空开始写代码,是要基于核心类库提供的一些基础代码,进行编程。
	
- 什么是JRE?
    答:JREJava Runtime Enviroment),意思是Java的运行环境;它是由JVM和核心类库组成的;如果你不是开发人员,只需要在电脑上安装JRE就可以运行Java程序。
    
- 什么是开发工具呢?
	答:Java程序员写好源代码之后,需要编译成字节码,这里会提供一个编译工具叫做javac.exe,编写好源代码之后,想要把class文件加载到内存中运行,这里需要用到运行工具java.exe。 
	除了编译工具和运行工具,还有一些其他的反编译工具、文档工具等待...

The relationship between JDK and JRE can be summed up in one sentence: use JDK to develop programs and hand them over to JRE to run

2.6 Java's cross-platform principle

After learning the composition of JDK, we know that the execution of Java programs depends on the Java virtual machine. It is because of the Java virtual machine that Java programs have an important feature called cross-platform.

  • What is a cross-platform line?

    The so-called cross-platform means that programs developed in the Java language can run on a variety of operating systems. Common operating systems include Windows, Linux, and MacOS systems.

    If there is no cross-platform, the same application needs to be developed separately for each operating system if it wants to run on multiple operating systems. For example, WeChat has Windows version, MacOS version, Android version, IOS version

  • Why can Java programs be cross-platform?

    The principle of cross-platform is because different versions of the Java virtual machine are installed in different versions of the operating system . The operation of the Java program only depends on the Java virtual machine and has no direct relationship with the operating system. In this way, it can be compiled in one place and run everywhere .

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-fuehVf9F-1689489428374)(assets/1660147588001.png)]

2.7 JDK environment variable configuration

After the JDK is installed, let's learn a supplementary knowledge called the Path environment variable

  • What is the Path environment variable?

    The Path environment variable is the path of the system program, which is convenient for programmers to start the program in any directory of the command line window;

  • How to configure environment variables?

    For example, configure the QQ startup program to the Path environment variable to start QQ in any directory, and follow the steps below.

    **Step 1:** First find the directory where the QQ startup program is located C:\Program Files (x86)\Tencent\QQ\Bin, and copy this path

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-eYpgHjRm-1689489428375)(assets/1660538063180.png)]

    **Step 2:** Follow the steps below to find the Path environment variable.

    First find this computer, right-click properties, you can follow the interface below; click [Advanced System Settings], and then click [Environment Variables]

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-YToiBh3w-1689489428376)(assets/1660538424000.png)]

    After double-clicking Path, click New, paste in the QQ startup directory, don’t forget to click OK_

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-lQZIocPN-1689489428378)(assets/1660538760744.png)]

    **Step 3:** After configuring, check whether the configuration is successful

    1.Win+R 输入cmd回车,打开命令行窗口
    2.输入QQScLanucher,可以看到QQ启动了
    

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-9dWoWADY-1689489428379)(assets/1660539146158.png)]


  • Configure the JDK to the Path path

    Above we configured the startup directory of QQ to the Path environment variable location, then we configure the bin directory of JDK to the Path environment variable, so that javac and java commands can be started in any directory to complete compilation and operation.

    **Step 1:**Find the bin directory of JDK C:\Program Files\Java\jdk-17.0.3\binand copy it

    **Step 2:** Paste the JDK bin directory behind the Path environment variable

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-IGdXWLWc-1689489428381)(assets/1660539632325.png)]

    Step 3: Check whether the configuration is successful

    1.按住Win+R输入cmd 回车,打开命令行创建
    2.输入javac -version 看提示信息是否显示你安装JDK的版本号
      输入java -version 看提示信息是否显示你安装JDK的版本号
    【如果显示版本号都是JDK17就表示配置安装成功】
    

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-w9InbyfE-1689489428382)(assets/1660539955302.png)]

If you come here according to the previous operation, it means that the JDK environment variable has been configured, and you can run it in any directory by using the JDK command later.

3. Java development tools

When you wrote the code just now, you used Notepad to write it, but do you think it is not convenient to write code in Notepad? There is no prompt for writing code words in Notepad, and the format is not easy to adjust. After writing the code, we need to go to the command line and use the javac command to manually compile and then run it.

Is there a software that can integrate tools such as writing code, compiling, and running? Yes, this is the Integrated Development Environment (IDE for short). In addition, IDEA also has code prompts, code error checking and other functions, thereby improving the development efficiency of programmers.

There are many kinds of IDEs, such as Eclipse, MyEclipse, Intellij IDEA, JBuilder, NetBeans, etc. However, Intellij IDEA (hereinafter referred to as IDEA) is currently the most popular among these IDEs, which is regarded by many Java programmers as the best Java integrated development environment, so we use IDEA as a development tool to write codes in our courses. The company also recommends using IDEA as the development environment.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-1UWYsdAO-1689489428384)(assets/1660150643893.png)]

3.1 IDEA download and installation

In order to guide you to correctly complete the download and installation of IDEA, a separate document "IDEA Installation, Use, Configuration.pdf" is provided for friends. The document provides detailed installation and usage steps of IDEA. You only need to follow the document. Step by step operation on the line.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-FfeoaaFr-1689489428385)(assets/1660541810799.png)]

3.2 IDEA writes Java programs

After we installed IDEA in the previous section, we can use IDEA to develop a HelloWorld program to play with! The purpose is to let everyone know the steps to develop Java programs in IDEA.

If you want to write a Java program correctly in IDEA, you must first understand IDEA's project structure for managing Java programs.

  • Step 1: First, you must create a Project (project, also called project) in IDEA, which is collectively referred to as a project.
  • Step 2: You need to create a Module (module) in the Project, and a project can contain multiple modules
  • Step 3: You need to create a new Package (package) in the Module, and there can be multiple packages in a module
  • Step 4: You need to create a new Class (class) in the Package, and a package can contain multiple classes

Software engineering is actually similar to architectural engineering, and we understand it by comparing architectural engineering.

  • Project (engineering): you can understand it as the courtyard of the community
  • Module: You can understand it as every building in the courtyard of the community
  • Package (package): You can understand it as the first floor of each building
  • Class (class): You can understand it as the residents of each floor

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-38RgJCjn-1689489428387)(assets/1660542739892.png)]

In actual development, such as the Taobao website project, but due to the many functions, the Taobao website is divided into different modules, the home page is a module, the shopping cart is a module, and the order is also a module;

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yie8wEau-1689489428388)(assets/1660543086870.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ofxp8I7f-1689489428390)(assets/1660544338418.png)]

  • Create a projectProject

    There are many steps to create a project. The detailed guidance steps provided in "IDEA Installation, Use, Configuration.pdf" can be followed step by step.

    Open "IDEA Installation, Use, Configuration.pdf" with a browser and click the navigation bar on the left to locate the corresponding location. Each part has screenshot steps

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-vvqXtn3L-1689489428391)(assets/1660544715195.png)]

3.4 Various configurations of IDEA

When some students used IDEA to write programs just now, they may feel that the font is relatively small, the background color is black, and the comments are gray, which makes them hard to see. It may be bad for the eyes for a long time. We can adjust the font to be larger through IDEA-related settings, the background color is an eye-protecting color, and the comments are also adjusted to green.

  • theme configuration

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Qk4XIDK9-1689489428393)(assets/1660150404720.png)]

  • font configuration

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-JbZmpH2S-1689489428394)(assets/1660150433205.png)]

  • Background color configuration

Adjust the color value of the background color to 204, 238, 200; it is the eye protection color of bean paste green

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-8PV2dwDb-1689489428395)(assets/1660545292279.png)]

For more configuration, you can refer to "IDEA installation, use, configuration.pdf"

3.5 Common shortcut keys

After talking about IDEA-related configuration, let me tell you a very important IDEA usage skill. This is the shortcut key of IDEA. The so-called shortcut key is a combination of keys on the keyboard to help us generate code. Using shortcut keys can greatly improve our development efficiency.

The following are several common shortcut keys and their functions, you can try them yourself

hot key Functional effect
main/psvm、sout、… Quickly type the relevant code
Ctrl + D Copy the current row data to the next row
Ctrl + Y To delete the line, it is recommended to use Ctrl + X
Ctrl + ALT + L format code
ALT + SHIFT + ↑ , ALT + SHIFT + ↓ Move the current code up and down
Ctrl + / , Ctrl + Shift + / Comment on the code (we will talk about it when we talk about comments)

There are actually many shortcut keys. Here are the ones we use more at this stage. It doesn’t matter if you don’t remember them now. You will use them often in the future.

When we talk about some new knowledge points in the following courses, we will continue to tell you some new shortcut keys.

4. Java Basic Grammar

All the content mentioned above is to do some preparatory work for Java program development. We have not officially taught you how to write code.

If you want to write Java code, you must learn Java grammar. The most important thing to learn grammar is the following two points.

  • remember syntax format
  • Clarify what effect this grammatical format can achieve

Here I need to explain one thing to everyone: the syntax format is specified by the designer of the Java language, and we don't need to care why it is written like this, because it is written like this when it was created.

4.1 Notes

Let's start with the simplest grammar first, let's learn annotations first!

  • What are annotations?

    Comment is to explain the problem of the program, so that you and others can read the code

  • What kinds of annotations are there? What is the format?

    1.单行注释:
    	//后面根解释文字
    2.多行注释
        /*
        这里写注释文字
        可以写多行
        */
    3.文档注释
        /**
        这里写文档注释
        也可以写多行,文档注释可以利用JDK的工具生成帮助文档
        */
    
  • The following explains a piece of code with comments

    /**
    目标:学会使用注释
         这是程序的讲解注释
    */
    public class NoteDemo{
          
          
        //这是程序的主方法,是程序的入口
        public static void main(String[] args){
          
          
            System.out.println("我开始学习Java程序,好嗨皮~~");
            /*
            窗前明月光
            疑是地上霜
            举头望明月
            低头思故乡
            */
            System.out.println("播仔");
            System.out.println("deli");
        }
    }
    

Learn one more trick: it is cumbersome to manually add annotations every time, you can also use shortcut keys to add annotations

Ctrl + / 	单行注释(对当前行进行注释)
Ctrl + Shift + / 	对选中的代码进行多行注释。

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-umC5guxX-1689489428397)(assets/1660546999747.png)]

4.2 Literals

After learning annotations, let's learn a new knowledge point called literals.

  • What is a literal?

    Don’t be confused by this word, it’s actually very simple. We know that computers are used to process data, and literals are actually telling programmers the format in which data is written in the program. The following is the commonly used data writing format in the program

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-hpK3qVHi-1689489428398)(assets/1660150925625.png)]

  • Write a program that prints out various types of literal values ​​​​on the command line
/*
目标:需要同学们掌握常见数据在程序中的书写格式
*/
public class LiteralDemo{
    
    
    public static void main(String[] args){
    
    
        //1.整数
        System.out.println(666);
        
        //2.小数
        System.out.println(3.66);
        
        //3.字符: 字符必须用单引号引起来
        System.out.println('a');
        System.out.println('0');
        System.out.println('中');
        System.out.println(' '); //空格也算字符
        //特殊字符:\t表示制表符 \n表示换行
        System.out.println('\t'); //这相当于一个tab键,专业叫做制表符
        System.out.println('\n'); //这是换行的意思
        
        //4.字符串:字符串是双引号引起来的
        System.out.println("我爱你中国abc");
        
        //5.布尔值:只有两个值true和false
        System.out.println(true);
        System.out.println(false);
    }
}

To sum up: For literal quantities, you only need to be able to write all kinds of data correctly.

4.3 Variables

After learning literals, let's learn variables next. The learning path for variables is as follows

  1. First know what is a variable?

  2. Why use variables for learning?

  3. What are the application scenarios of learning variables?

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-71A1Er7V-1689489428400)(assets/1660548540262.png)]

  • What are variables?

Variables are used to record data in the program. It is essentially an area in memory, which you can understand as a small box.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-KZ10guJY-1689489428406)(assets/1660548847936.png)]

The variable definition format is as follows:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-wBavrGt8-1689489428410)(assets/1660151428759.png)]

When defining a variable, we declare the data type , where the data type is used to specify what type of data the variable stores .

For example, int a = 10;here intis to limit variables to only store integers; in addition to the data type int, Java also provides many other data types. Data types in Java are generally divided into two categories:

Basic data types , reference data types .

Today we mainly study basic data types. There are 4 types and 8 types of basic data types. Each data type occupies different memory space and the range of data that can be represented is also different. As shown below

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-66WHbjKU-1689489428413) (F:\Information\Latest Course Information\Basic Course 9-day Information\Java Basic Class Handout\ day02-Java type conversion, operator\assets\1660832924103.png)]

Next, let's feel the format of variable definition through a small case, for example:

int age = 18;

System.out.println(a);

When this code is executed int age = 18;, the JVM will apply for an area in the memory, in which an integer 18 is stored, and the name given to this area is age; it is equivalent to storing a data 18 in the box, the name of the box It is age. When we print age, we take out the data in the box from the box and print it.

The following defines various types of variables, and uses these 8 basic data types. But before starting to define, you need to add an additional note

Just write an integer or decimal literal, it also has a default data type

- 比如23,它默认就为int类型;如果加上后缀L,则为long类型;
- 比如23.8,它默认为double类型;如果加上后缀F,则为float类型;
public class TypeDemo1 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:掌握8种基本数据类型,用来定义变量。
        // 1、整型
        byte number = 98;
        System.out.println(number);

        short number2 = 9000;

        int number3 = 12323232; // 默认

        // 注意:随便写一个整型字面量,默认是int类型的,73642422442424虽然没有超过long的范围,但是它超过了本身int的范围了。
        // 如果希望随便写一个整型字面量是long类型的,需要在其后面加上L/l
        long number4 = 73642422442424L;

        // 2、浮点型
        //注意:
        //随便写一个小数字面量,默认当成double类型对待的,
        //如果希望这个小数是float类型的,需要在后面加上:F/f
        float score1 = 99.5F;
        double score2 = 99.8; // 默认定义方案。

        // 3、字符型
        char ch1 = 'a';
        char ch2 = '中';
        char ch3 = '国';

        // 4、布尔型
        boolean b1 = true;
        boolean b2 = false;

        // 引用数据类型:String.
        // String代表的是字符串类型,定义的变量可以用来记住字符串。
        String name = "黑马";
        System.out.println(name);
    }
}
  • Why use variables?

Using variables to record data makes data management more flexible. For example, we use an integer 10 in multiple places,

//1.假设4多个地方用到整数10; 现在我想把10改为20,这时你得一条语句一条语句的修改
System.out.println(10);
System.out.println(10);
System.out.println(10);
System.out.println(10);

//2.同样这里还是在多个地方用到整数10,你可以先用一个变量记录这个整数10
int x = 10;
//然后在需要用到整数10的地方,用x代替就行;
//如果我们把x值改了,那么后面用到x的值的地方就都一起改变了
System.out.println(x);
System.out.println(x);
System.out.println(x);
System.out.println(x);
  • Does the variable have an application scenario?

The application scenarios of variables are ubiquitous. As long as it is data that can change in the program, it can be stored in variables. For example: the balance in your Wechat wallet, your Wechat nickname, and your Wechat personalized signature; whether the balance is decreasing as you spend money, the nickname and personalized signature can also be frequently modified.

//比如:你现在有188.8元,一会要发100元,再收20元。
double money = 188.8;
//发100元
money = money - 100;
//再收20元
money = money + 20;
//再打印money记录的值是多少
System.out.println(money);


//再比如:公交车大人2元,小孩一元,公交车经过2站
//第一站:3个大人1个小孩上车
//第二站:1个大人1个小孩上车,请问一共上了多少人,一共收入多少钱?
//刚开始公交车箱子里没有钱,用money2表示箱子里的钱
int money2 = 0;
//刚开始公交车上也没有人,用count变量表示上车的人数
int count = 0;

//经过两站的人数:第一站3人,第二站1人,总的人数是3+1
count = 3+1;
//经过两站的钱数:
money2 = money2+3*2+1; //经过第一站后
money2 = money2+2+1; //经过第二站后

//打印人数和钱数
System.out.println(count);
System.out.println(money);
  • Note on variables

In the last class, I have already talked about the basic use of variables. Variables are used a lot in actual development. I need to explain the precautions for using variables.

The purpose of talking about these precautions is to let everyone know which ways of writing are allowed and which ways of writing are not allowed, to be able to distinguish right from wrong, and to know why they are wrong.

1.变量定义在哪个{
    
    }范围内,就只在哪个大括号内有效。变量的有效范围称之为变量的作用域
	{
    
    
		int a = 10;
		System.out.println(a); //这是是对的
	}
	System.out.println(a); //这里会出错

2.在同一个作用域内,不能有两个同名的变量
	{
    
    
		int a = 10;
		int a = 20; //这里会出错
	}
	
3.变量没有初始化只,不能直接使用
	int a; //仅仅定义了变量,但是没有初始值
	System.out.println(a); //这里会出错

4.变量可以定义在同一行
	如:int a=10, b=20; //a和b都是int类型

At this point, all the usage methods of variables, as well as the issues that need to be paid attention to in variables, are finished.

Let's summarize a few more points

  • Variables are used to record data in the program. Variables can be understood as small boxes in memory, and the things placed in the boxes are the data recorded by variables.
  • Variable definition format:数据类型 变量名 = 初始值;
  • The data of the tag recorder can be changed during the running of the program:变量名 = 值;

4.4 Keywords

After learning variables, let's get to know the keywords of Java.

  • What are keywords?

    Keywords are words with special meanings in the Java language. For example, use int to represent integers, double to represent decimals, and so on!

  • What are the keywords?

    The essence of our learning Java grammar is to learn the meaning of these keywords. There are more than 50 keywords in total. We don’t learn these keywords all at once. We will learn a little bit every day in the follow-up courses. Learned.

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatile while
  • How to identify those words are keywords?

Now we don't need to know what these keywords mean, we can mainly identify those keywords according to their characteristics

关键字的特点:
	1.关键字都是小写的
	2.关键字在idea中有特殊颜色标记(如果你没有修改关键字的颜色,默认是蓝色的)

Words framed in red in the picture below are keywords; words not framed are not keywords

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-HskR5Az8-1689489428416)(assets/1660553031642.png)]

4.5 Identifiers

Next, let's learn about identifiers. The so-called identifiers are actually the names we choose ourselves. Like the class names we took earlier, variable names are actually identifiers.

The purpose of talking about identifiers is to let everyone know the rules of naming, so that we can't just pick them up randomly. Some rules are mandatory, and errors will be reported if they are not followed. There are also some rules that we recommend everyone to follow, so that we can make the name more professional_ .

强制要求:必须遵守,不遵守就会报错
	1.最好是字母、数字、下划线、$组成
	2.不能以数字开头
	3.不能是Java的关键字

建议遵守:按照下面的方式取名字会显得更加专业
	1.所有的名字要见名知意,便于自己和别人阅读
		举例: class Student{
    
    } //一看这个类就知道表示一个学生
			  int age =10;    //一看这个变量就知道表示年龄
		
	2.类名:首字母大写(大驼峰命名)
		举例: class Student{
    
    }
		
	3.变量名:第二个单词开始首字母大写(小驼峰命名)
		举例: double money = 6.88;  
			  double applePrice = 7.5; 

At this point, congratulations, friends, all the content learned today is over, come on Ollie! ! !

Guess you like

Origin blog.csdn.net/whirlwind526/article/details/131750260