1.1 notes
Note: used to mark and explain the meaning of the code. It is generally used to facilitate you to view it later and help others understand the meaning of your code.
- Comments are usually written on the top of the code to indicate the meaning of the following code. Comments are not executed. You can debug the program by adding comments.
- Note classification:
- Single line note: only one line can be written, which can be written on or after
Format: / / comment content
- Multi line comments: you can write multiple lines or single line comments on the code
Format: / * comment content*/
- Document comments: you can write multiple lines and include single line comments. You can edit an independent file through the javadoc.exe tool and write it on the code. [not commonly used, the integrated development environment used in the future will be automatically generated]
Format: / * * Note content*/
- 3. Comment code demonstration
//Create a Java class named Hello /* Class definition format: public class Class name{ } public-----Is the access restriction modifier of this java class class-----Keywords for creating classes Hello-----Name of the class [capitalize each word] {}---Class body */ public class Hello{ //The main method of the current Java program [main method], the entry of the program //When the program is written and executed, the main method should be found. It can't be executed without the main method /* public-----Is the access restriction modifier for this main method static-----Static modifier [allocate space in advance] void-----The main method has no execution result to return main-----Method name [initial lowercase, method name starts with the second word and initial uppercase] (String args[])-----In the parameter list, there is an array of string type called args {}-----Method body */ public static void main(String args[]){ //Print the "Hello, world" string to the console System.out.println("hello,world"); } }
An error occurred during use: unmapped character encoding GBK.
Solution: save Notepad code as, change the code UTF-8 to ANSI, and then overwrite the source file.
1.2 Java keywords
Keyword: a word with special meaning given by the Java language. The letters of the keyword are all lowercase.
1.3 Java identifier
Identifiers: java classes, methods, arrays... These elements have their own names. These names are considered identifiers in java. They are text / strings used to name elements in java.
- Define rules
- It consists of 26 English letters (A~Z, A~Z), numbers (0 ~ 9), underscores () and dollar symbols ($);
- Cannot start with a number;
- Strictly case sensitive; [hello and hello are different]
- The identifier can be of any length; [try not to be too long]
- Chinese cannot appear; [Chinese may be garbled]
- Cannot be keyword; [keyword occupied]
1.4 Java variables
(1) Variables: the data that can be modified and changed during program operation is variables. In essence, variables are actually the data storage space opened up during program operation to store data.
(2) Format: data type of variable Name of variable = initial value of variable;
(3) Composition of variables:
- The data type of the variable - determines the size of the storage space corresponding to the variable;
- Name of variable ---- identifier [all lowercase];
- Initial value of variable - default. You can also set an initial value to match the data type;
- Scope of variable ---- the effective range of the variable and the creation location of the variable.
1.5 Java data types
1. Basic data type [8]
- Byte type -- byte -- 1 byte [8-bit binary]
Range: 128 ~ 127 (- the 7th power of 2 ~ the 7th power of 2 - 1). When using assignment, it cannot exceed the value range.
Example:
public class DataType{ public static viod main(String args[]){ //Defines a variable of type byte byte a = 10; System.out.println("a="+a); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType a=10
- Short -- short -- 2 bytes [16 bit binary]
Range: - 32768 ~ 32767 (- 15th power of 2 ~ 15th power of 2 - 1), the value range cannot be exceeded when using assignment.
Example:
public class DataType{ public static void main(String args[]){ //Defines a variable of type byte byte a = 10; System.out.println("a="+a); //Define variables of type short short b = 1000; System.out.println("b="+b); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType a=10 b=1000
- Integer ----- int [integer default] - 4 bytes [32-bit binary]
Range: - 2147483648 ~ 2147483647 (- 31st power of 2 ~ 31st power of 2 - 1), the value range cannot be exceeded when using assignment.
Example:
public class DataType{ public static void main(String args[]){ //Defines a variable of type byte byte a = 10; System.out.println("a="+a); //Define variables of type short short b = 1000; System.out.println("b="+b); //Defines a variable of type int int c = 1000000; System.out.println("c="+c); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType a=10 b=1000 c=1000000
- Long -- long -- 8 bytes [64 bit binary]
Range: - 9223372036854775808 ~ 9223372036854775807 (- 63rd power of 2 ~ 63rd power of 2 - 1). When using assignment, "L" and "L" are required as suffixes
Example: the default integer type is int. when the value is in the range of int, it will be int, but the final output result is the same, because there is implicit data type conversion. However, if the value exceeds the range of int, L must be added for long.
public class DataType{ public static void main(String args[]){ //Define variables of type long long d = 200; System.out.println("d="+d); long d1 = 200L; System.out.println("d1="+d1); /* long d2 = 20000000000; Error: too large integer: 20000000000 */ long d2 = 20000000000L; System.out.println("d2="+d2); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType d=200 d1=200 d2=20000000000
- Single precision floating point [decimal] - float -- 4 bytes
Range: 7 ~ 8 significant digits are reserved, which is not 100% accurate. When using assignment, "F" and "F" are required as suffixes.
Example:
public class DataType{ public static void main(String args[]){ //Define variables of type float /* float e = 13.14; Error: incompatible type: conversion from double to float may be lost Decimal is double by default, so float needs to add F and F */ float e = 13.14f; System.out.println("e="+e); float e1 = 1314; System.out.println("e1="+e1); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType e=13.14 e1=1314.0
- Double precision floating point type [decimal] - double [floating point number default] - 8 bytes
Range: 15 ~ 16 significant digits are reserved, which is not 100% accurate. When using assignment, you can use "d" and "d" as suffixes or not.
Example:
public class DataType{ public static void main(String args[]){ //Defines a variable of type double double dou=133; System.out.println("dou=="+dou); double dou1=133.0; System.out.println("dou1=="+dou1); double dou2=133.0d; System.out.println("dou2=="+dou2); } }
Output is:
E:\Learning records\Lesson Two Java Basic syntax and variables>javac DataType.java E:\Learning records\Lesson Two Java Basic syntax and variables>java DataType dou==133.0 dou1==133.0 dou2==133.0
- Character type -- char -- 2 bytes 0~65535 A single symbol enclosed in single quotes is a character type
Example:
public class DataType{ public static void main(String args[]){ //A single symbol enclosed in single quotes is a character type char ch1='a'; System.out.println("ch1=="+ch1); char ch2=37000;//In the character type, the digital output is Chinese characters System.out.println("ch2=="+ch2); int num1='b';//Characters in integer type are output as numbers System.out.println("num1=="+num1); // \Slash Escape character \ //Illegal escape character System.out.println("F:\\20210907\\Java Basics\\20211027Java Basics(2)\\test"); //Double quotes Escape character \" System.out.println("Zhang San said:\"Hello\""); //'single quote Escape character \' System.out.println("Zhang San said:\'Hello\'"); //Tab escape character \ t System.out.println("hello Hello"); System.out.println("hello\t Hello"); //Line feed Escape character \ n System.out.println("hello\n Hello"); } }
Output is:
ch1==a ch2==Miao num1==98 F:\20210907\Java Basics\20211027Java Basics(2)\test Zhang San said:"Hello" Zhang San said:'Hello' hello Hello hello Hello hello Hello
- Boolean -- Boolean -- 1 byte true/false execute judgment
Example:
public class DataType{ public static void main(String args[]){ boolean boo1=true; System.out.println("boo1=="+boo1); } }