Java entry basic learning (1)


(1) Notes

Add prompt information to the code and do not participate in the operation

	单行注释://
	多行注释:/*....*/
	文档注释:/**....*/   类似于帮助文档,是给用户使用的

(2) Keywords

Words with special meanings given by Java are
all lowercase
. There are special color marks in the development tools. The
Java language has 51 keywords.
Data type:
Boolean int long short byte float double char class interface
Flow control:
if else do while for switch case default break continue return try catch finally
Modifier:
public protected private final void static strictfp abstract transient
synchronized volatile native
Action :
package import throw throws extends implements this Super instanceof new
reserved words:
true false null goto const

(3) Constants:

Values ​​that cannot be changed during operation

(4) Classification of basic data types:

整数:byte short int(默认) long
小数:float double(默认)
字符:char
布尔:boolean
注意:字符串(String)不是基本数据,但是作为基本数据类型来使用

(5) Variables

Values ​​that can be modified during program operation
Definition format 1 : Data type variable name = initialization;
     for example: int x=10;
definition format 2 : Data type variable name; variable name = initialization;
    for example: int x;
           x=10;
Use of variables : use and assignment
Use: use variable names directly
Assignment: multiple assignments (one assignment is called initialization, and multiple assignments are called modification)
    For example: int x=10;
             x=20;
              System.out.print(x)
definition Multiple variable format 1 : Multiple variables are separated by a comma.
Data type variable name 1, variable name 2;
variable name 1=initialization;
variable name 2=initialization;

  	例如:   int x,y;
    		x=10;
    		y=20;

Define multiple variable format 2 : Multiple variables are separated by commas.
Data type variable name 1=initialization, variable name 2=initialization;

例如:int x=10,y=20;
	注意:如果某个变量没有进行初始化 例如:int x,y=20;(x没有值,y=20)

Precautions when using variables:
1. Variables must be initialized
2. The same variable name cannot be defined repeatedly
3. Initialization problem (for long type, add l after the constant, and float with f)

(6) Identifier

The names of custom variables, classes, and methods in the program

由数字,字母,符号(_ ,$) 组成
	定义规则:
		1,数字不可以开头。
		2,不可以使用关键字。
		3,区分大小写(大小写敏感)
	注意:有个业内规范,定义的变量名称要有意义

Identifier naming convention : camel case
variable : one word, all lowercase
multiple words, the first letter of the first word is lowercase, the other initials are uppercasedClass
name : one word, the first letter is uppercase
multiple words, all the first letters are uppercase

(7) Type conversion

自动类型转换(也叫隐式类型转换)
	把一个范围小的数据类型或变量赋值给一个范围大的数据类型或变量
	例如:double d=10;
强制类型转换(也叫显式类型转换)
	把一个范围小的数据类型或变量赋值给一个范围大的数据类型或变量
	例如:int i=(int)10.0;

(Eight) operator

Arithmetic operator
+-* /%

Note: Automatic type promotion is implied in the calculation expression (different data types are automatically promoted to a data type with a large data range). The forced type conversion may cause loss of precision

		++,--:自增 自减
		++:自增 
			++在前:先加后用
			++在后:先用后加

+Use for splicing
If there is only one +, there is a data type of String on both sides. For splicing,
if there are multiple + (without parentheses), execute from left to right. If the result of the first one is String, the rest + Is splicing.
For example: 10+"Hello"+20+30.
Note: If there are parentheses, perform the
assignment operator of parentheses first :
= += -=...
Shorthand: when calculating expressions, there may be forced type conversion.
Comparison operators :
== >= !=> <<=
Note: When judging equal, == Don't write less
String to judge whether it is equal to the format: String1.equals(String2)
Logical operator:
&(and and) |(or or)! (Non) && ||
! :Reverse the boolean result
For example: !true The result is false
&: Only the results on both sides are true, the result is true, one is false, the result is false
|: Only the results on both sides are fales, the result is false, and the result is false if one is true
The difference between true & and &&: && reduces resource consumption (optimizes the execution flow of logical operators)
Why: && when the left side is false, the right expression will not be calculated
& when the left side is false, the right side will still be calculated The expression of
single| is a bitwise OR, the result is a value, and the double OR sign || is a logical OR, and the result is a Boolean
ternary operator:
Format:
For example: (a>b)?a:b
If the condition is true, the result of the operation is expression 1;
if the condition is false, the result of the operation is expression 2;

(9) Scanner keyboard input

	1.导包
		import java.util.Scanner;
		放在类定义前面
	2.创建对象
		Scanner sc=new Scanner(System.in);
	3.接收数据
		int i=sc.nextInt(); 只能接受int数据
		String str=sc.next();接收数据转为String
		(无论如何输入,只需要在两边加上""就变成了String)
注意:以enter键(回车键) 结束当前数据输入   可以接收多次数据

(10) Output

System.out.println() wraps after output
System.out.print() does not wrap after output

exercise:

1.
Fill in the blank questions 1. Java technology is divided into three major versions according to different purposes, namely __JavaSE__, JavaEE and __JavaME____ 2. The Java
virtual machine is a virtual computer used to execute bytecode files. It is the core technology of Java and the foundation of Java cross-platform.
3. The command used to change the current directory in the DOS command is cd, and the command used to view the files and subfolders in the current directory is dir.
4. After installing JDK, in order to tell the computer the location of executable files such as javac.exe and java.exe, the environment variable that needs to be configured is ____PATH_________.
5. Using Java to develop an application program includes three steps: writing the source program, compiling the source program, interpreting and running. The extension of the bytecode file generated after the Java source program is compiled is class.
6.Java provides three types of annotations, namely ___single-line annotations, multi-line annotations_____ and ___document annotations .
7. What you should pay attention to when creating a Java source file is that the _____ file name is the same as the class name.
Two, multiple choice questions

  1. Which of the following options is correct about the Java cross-platform principle is (AC ). (Choose two items)

    A Java source program must first be compiled into a platform-independent bytecode file (.class), and then the bytecode file is interpreted as machine code to run
    B. The cross-platform principle of Java determines that its performance is higher than C/C++
    C . Java virtual machine is a virtual computer that can run Java bytecode files. Virtual machines on different platforms are different, but they all provide the same interface
    . D. Java language has the characteristics of one-time compilation and running everywhere, and it can run on all platforms

  2. The following options are the statements that correctly compile a Java source file is (A) (choose one)

    A. javac Test.java
    B. javac Test
    C. java Test
    D. java Test.class

  3. In Java, if the source file Test.java contains the following code, the result of program compilation and running is (B). (Choose one)

public class Test {
    
    
	public static void main(String[ ] args) {
    
    
		system.out.println("Hello!");
	}
}

A output: Hello!
B. Compilation error, prompt "Cannot parse system"
C. It runs normally, but nothing is output
D. An exception occurs during operation

  1. There is a Java program, where the public class name is A1, then the source file name for saving it can be (A ). (Choose one)

    A. A1.java
    B. A1.class
    C. A1
    D. None

III. True or False
1. Java language is an object-oriented language with good security, portability and platform-independent characteristics. (T) 2.
Java has the characteristics of compiling once and running everywhere, which means that the source code of Java can be compiled into machine code of the computer at one time and run on different platforms. (F)
3. Compared with C++, Java has eliminated pointers, no longer has multiple inheritance, and does not need to manually collect garbage. (T) 4.
Java bytecode file extension is class, which is a platform independent non-binary file. (F)
5. Suppose there is a Java source program file that only defines a class Hello with public attributes, then the command to interpret and execute this class is "java Hello.class". (F)
6. Comments are instructions made by programmers for readers and a means to improve the readability of programs. Java comments will not appear in the bytecode file, that is, the Java compiler will skip the comment statements when compiling. (T)
7. Javadoc is a tool for generating HTML help files from documentation comments. It can process the comments between "/ " and " /" in the Java source program and generate corresponding program documentation. (F)
Fourth, short answer questions
1. Java's cross-platform principle, and the difference between the C language cross-platform principle.
Answer: C is source code-level cross-platform. The compiled object files and executable files are not cross-platform. Java is compiled to generate .class files that can be executed on the Java virtual machine, so as long as there is a jvm virtual machine on the platform You can run the Java language.
2. What is JVM? What is JDK? What is JRE?
Answer: JVM is the Java virtual machine. It is the core part of the Java language to implement cross-platform. It is used to interpret and execute Java.class files; jdk is the java development kit; jre is the Java runtime environment.
3. What are comments? There are several types of comments? What are the functions of comments?
Answer: Comments are the interpretation text of the code in the Java language. Comments are divided into single-line comments, multi-line comments and document comments; comments can improve the readability of the code Sex.
4. The execution process of Java language.
Answer: First compile it into a .class file and then interpret and execute it through the Java virtual machine.
5. The role of environment variable path and classpath
A: Path is to help the system find the location of the application. Classpath tells the location of the class file of the virtual machine.
5.
Use Notepad to write a Java program for coding questions , and print the first four lines of the ninety-nine multiplication table. as the picture shows. (Think about whether there is a better solution).

public class Test3{
    
    
	public static void main(String [] args){
    
    
		System.out.println("1*1=1");
		System.out.println("1*2=2\t2*2=4");
		System.out.println("1*3=3\t2*3=6\t3*3=9");
		System.out.print("1*4=4\t2*4=8\t");
		System.out.println("3*4=12\t4*4=16");
	}
}

Java introductory basic learning (1)
Java introductory basic learning (2)
Java introductory basic learning (3)
Java introductory basic learning (4)
Java introductory basic learning (5)
Java introductory basic learning (6)
Java introductory basic learning (7)
java Introductory basic learning (8)
Java introductory basic learning (9)
Java introductory basic learning (10)
Common objects of
advanced java (1) Common objects of
advanced java (2) Bubble sorting of
advanced java The choice of advanced java Sort
java advanced object-oriented (encapsulation)
java advanced object-oriented (code block, inheritance)
java advanced object-oriented (polymorphism, abstraction, interface)
java advanced anonymous inner class, access modifier, package

Guess you like

Origin blog.csdn.net/qq_45798550/article/details/107893882