The basics of Java SE (2) - java basic grammar (1)

foreword

  Starting from the last article , we will introduce you to the relevant knowledge of java. The first thing we will introduce to you is the introduction and core of java - the content of Java SE.I hope everyone must study hard. This is the basis for learning frameworks and other microservices in Java later. Be sure to study them well. In addition, during this period, I will introduce you to the basic knowledge of Java and also introduce the corresponding cases. Maybe you have seen these cases more or less, but will give you specific solutions to the problem., that is, I will take you through the process of how to start from a problem to find a problem-solving idea and put the idea on the code. Over time, you will develop a practical ability to solve problems, which will give you not only the learning of java knowledge , has an essential improvement in your practical ability. Therefore, I strongly recommend that you follow my first article to study carefully. After learning, I believe that you must have a good understanding of both Java and your own coding ability. promote. In addition, from the beginning of this article, in order to give you a systematic understanding and review of the content we have introduced, I will summarize the knowledge points introduced in this article at the end of the article through Xmind for everyone to review and let everyone understand. This knowledge framework of java has a better grasp and understanding.
  The last article introduced the download, installation and configuration of the java environment, and verified whether our java environment was successfully installed by CMDentering the command java-versionin it.Again, if you want to follow my blog to continue learning java, you must install jdk on your computer according to the installation method in the previous article .. This article introduces some of the basic grammar of java, mainly including the use of annotations, keywords in java, and the introduction of constants, variables and data types. This part is relatively simple, but the foundation is very important, and the code involved is not a lot. It is written for java knowledge points and zero-based friends.If you have java foundation, you can skip this part. In fact, it is not difficult to find that I have seen Python variables and simple data types . There are also this part of the content, and the ideas and knowledge points are the same, including loops and conditional statements , but the syntax is different. Therefore, the ideas are the same, but the grammar of the language is different. Next, the first thing to introduce to you is the knowledge of annotations in java.

1. Notes

  Comments are explanations and descriptions of the code, which can improve the readability of the program, so it is very important to add necessary comment texts to the program. Each programming language has its own way of annotation, among which, the annotations in Java are divided into the following three types:

  Single line comment . Its format is to use **//**, and the text from // to the end of the line will be used as comment text. For example: // 这是单行注释文字
  multi-line comments . Its format is to use/*and*/Enclose a longer comment. E.g:

/*
这是多行注释文字
这是多行注释文字
这是多行注释文字
*/

  Here we need to pay special attention to:Multi-line comments cannot be nested

  Documentation comments . It will /**start with and */end with

  In order to let everyone see this effect in the code, we still use the HelloWorld.javaprogram in the previous article to realize this small knowledge point of annotation:

/*
	Java程序中最基本的组成单位是类。
	
	类的定义格式:
		public class 类名 {
			
		}
		
	这是我定义的HelloWorld类
*/
public class HelloWorld {
    
    
	/*
		这是main方法
		main方法是程序的入口方法,代码的执行是从main方法开始的
	*/
	public static void main(String[] args) {
    
    
		// 这是输出语句,""里面的内容是可以改变的
		System.out.println("itheima");
	}
}

  Note that during code operation, IDEA will automatically skip the content of these comments, so the effect of execution is the same as the results we saw before; the specific execution results are as follows:

2. Keywords

  Keywords are words that are given special meanings by the java language. The main characteristics of keywords are:

  The letters of keywords are all lowercase
  . Commonly used code editors have highlighted keywords , such as public, class, static, etc. that we can see now.

  Following are the keywords we commonly see in java:

3. Data Type

1. Computer storage unit

  We know that computers can be used to store data, but whether it is a memory or a hard disk, the smallest unit of information in a computer storage device is called a "bit", which we also call a "bit", usually with a lowercase letter "b" "Express. The most basic storage unit in a computer is called a "byte", usually represented by a capital letter "B", and a byte is composed of 8 consecutive bits. In addition to bytes, there are some common storage units, the conversion units are as follows:

  1B (byte) = 8bit

  1KB = 1024B

  1MB = 1024KB

  1GB = 1024MB

  1TB = 1024GB

  After understanding how our data is stored in the computer, let's introduce the data types in java.

2. Data types in Java

  Java is a strongly typed language, and data in Java must have a clear data type. Data types in Java include basic data types and reference data types. Among them, the basic data types in Java mainly include:

  Among them, e+38 means multiplied by 10 to the 38th power, and similarly, e-45 means multiplied by 10 to the negative 45th power. In addition, in java, integers are of type int by default, and floating-point numbers are of type double by default. Next, we will introduce you to the knowledge of constants.

4. Constants

  Constant: A quantity whose value cannot be changed during program execution. Generally speaking, constants in Java have the following categories:

  1. String constant Multiple characters enclosed in double quotation marks (can contain 0, one or more), such as "a", "abc", "China", etc.
  2. ​Integer constant integer, for example: -10 , 0, 88, etc.
  3. Decimal constants decimals, such as: -5.5, 1.0, 88.88, etc.
   ​4. Character constants are a character enclosed in single quotation marks, such as: 'a', '5', 'B', 'in ' etc
  _ _    _ _ _

  Of course, among these types of constants, except for empty constants , other constants can be directly output by using the output statement. Next, we use a small case to further understand the application of constants. The specific implementation code is as follows:

/*
	常量:
		在程序运行过程中,其值不可以发生改变的量。

	常量分类:
		字符串常量:	用双引号括起来的内容。"HelloWorld","黑马程序员"
		整数常量:		不带小数的数字。666,-88
		小数常量:		带小数的数字。13.14,-5.21
		字符常量:		用单引号括起来的内容。'A','0','我'
		布尔常量:		布尔值,表示真假。true,false
		空常量:		一个特殊的值,空值。null
*/
public class ConstantDemo {
    
    
	public static void main(String[] args) {
    
    
		//字符串常量
		System.out.println("HelloWorld");
		System.out.println("欢迎关注本人博客");
		System.out.println ("https://blog.csdn.net/Oliverfly1");
		System.out.println("--------");
		
		
		//整数常量
		System.out.println(666);
		System.out.println(-88);
		System.out.println("--------");
		
		//小数常量
		System.out.println(13.14);
		System.out.println(-5.21);
		System.out.println("--------");
		
		//字符常量
		System.out.println('A');
		System.out.println('0');
		System.out.println('我');
		System.out.println("--------");
		
		//布尔常量
		System.out.println(true);
		System.out.println(false);
		System.out.println("--------");
		
		//空常量
		//空常量是不能直接输出的
		//System.out.println(null);
	}
}

  This program code includes the applications of the above types of constants. The specific operation effect is as follows:

  After introducing the constants, I will introduce another data structure - variables. It is also more important in our programming language. Although it is simple, it will definitely be used frequently in the future.

5. Variables

  A variable is a quantity whose value can change while a program is running. Essentially, a variable is a small area in memory whose value can vary within a certain range. We generally follow a certain format when defining a variable, as follows:

// 数据类型 变量名 = 初始化值; // 声明变量并赋值
int age = 18;
System.out.println(age);

  Of course, there can also be another way, as follows:

// 先声明,后赋值(使用前赋值即可)
//数据类型 变量名;
//变量名 = 初始化值;
double money;
money = 55.5;
System.out.println(money);

  You can also define multiple variables of the same data type on the same line, separated by commas. But it is not recommended to use this method, which reduces the readability of the program. In order to let everyone better understand the definition and use of variables, we can use a small case to help you better understand this knowledge point:

/*
	变量定义格式:
		数据类型 变量名 = 变量值;
		
	基本数据类型:
		byte,short,int,long,float,double,char,boolean
		
	变量的使用:
		取值格式:变量名
		
		修改值格式:变量名 = 变量值;
*/
public class VariableDemo01 {
    
    
	public static void main(String[] args) {
    
    
		//定义变量
		int a = 10;
		
		//输出变量
		System.out.println(a);
		
		//修改变量
		a = 20;
		System.out.println(a);
	}
}

  The specific program execution effect is as follows:

  Of course, we only need to access the variable name when using the variable. When we use variables in the future, we should also pay attention to some matters, as follows:

  1. Within the same pair of curly braces , variable names cannot be repeated.
  2. Variables must be initialized (assigned) before they are used .
  3. When defining a variable of type long , you need to add L after the integer (both upper and lower case, uppercase is recommended). Because integers are of type int by default, integers that are too large may exceed the int range.
  4. When defining a variable of type float , you need to add F after the decimal (both upper and lower case, uppercase is recommended). Because the default type of floating-point numbers is double, the value range of double is larger than float, and the types are incompatible.

  For beginners, this point is easy for everyone to make mistakes. In order to better avoid this mistake, we will show you another case, so that everyone can have a clear grasp of the knowledge point of variables; specific The implementation is as follows:

/*
	变量使用的注意事项:
		名字不能重复
		变量未赋值,不能使用
		long类型的变量定义的时候,为了防止整数过大,后面要加L
		float类型的变量定义的时候,为了防止类型不兼容,后面要加F
*/
public class VariableDemo02 {
    
    
	public static void main(String[] args) {
    
    
		//定义byte类型的变量
		byte b = 10;
		System.out.println(b);
		
		//定义short类型的变量
		short s = 100;
		System.out.println(s);
		
		//定义int类型的变量
		int i = 10000;
		System.out.println(i);
		
		//定义double类型的变量
		double d = 13.14;
		System.out.println(d);
		
		//定义char类型的变量
		char c = 'a';
		System.out.println(c);
		
		//定义boolean类型的变量
		//boolean b = true;
		//System.out.println(b);
		boolean bb = true;
		System.out.println(bb);
		System.out.println("--------");
		
		//定义long类型的变量
		long l = 10000000000L;
		System.out.println(l);
		System.out.println("--------");
		
		//定义float类型的变量
		float f = 13.14F;
		System.out.println(f);
	}	
}

  The effect of the specific implementation is as follows:

  So far, the part of our variables has been introduced to you. I hope you can have a clear understanding of this knowledge point through these two small cases. Next, I will introduce you to the relevant knowledge of type changing bricks.

Six, type conversion

  In Java, some data types can be converted to each other. There are two cases: automatic type conversion and forced type conversion. The first thing to introduce to you is automatic type conversion:
  assigning a value or variable representing a small data range to another variable representing a large data range. This conversion method is automatic and can be written directly. E.g:

double num = 10; // 将int类型的10直接赋值给double类型
System.out.println(num); // 输出10.0

  Next is coercion; that is, assigning a value or variable representing a large data range to another variable representing a small data range. Also, cast the format:target data type variable name = (target data type) value or variable;The specific applications are as follows:

double num1 = 5.5;
int num2 = (int) num1; // 将double类型的num1强制转换为int类型
System.out.println(num2); // 输出5(小数位直接舍弃)

  The conversion of specific data types is shown in the following figure:

  However, for the above figure, we explain the data type conversion as follows:

  • 1. The conversion of char type data to int type is calculated according to the corresponding int value in the code table. For example, in the ASCII code table, 'a' corresponds to 97.
int a = 'a';
System.out.println(a); // 将输出97
  • 2. Integers are of int type by default, and data of byte, short and char types participating in operations will be automatically converted to int type.
byte b1 = 10;
byte b2 = 20;
byte b3 = b1 + b2; 
// 第三行代码会报错,b1和b2会自动转换为int类型,计算结果为int,int赋值给byte需要强制类型转换。
// 修改为:
int num = b1 + b2;
// 或者:
byte b3 = (byte) (b1 + b2);

  What we need to pay attention to here is: The boolean type cannot be converted to and from other basic data types. Next, we use a case to make an application of the data type conversion, so that everyone can understand this part of the content; the specific implementation is as follows:

/*
	类型转换
*/
public class ConversionDemo {
    
    
	public static void main(String[] args) {
    
    
		//自动类型转换
		double d = 10;
		System.out.println(d);
		
		//定义byte类型的变量
		byte b = 10;
		short s = b;
		int i = b;
		
		//这是不可以的,类型不兼容
		//char c = b;
		
		//强制类型转换
		int k = (int)88.88;
		System.out.println(k);
	}
}

  The specific implementation effect is as follows:

7. Identifier

  Finally, I will introduce you to the relevant knowledge of identifiers in java. Identifiers are names that users use when programming to name classes, methods, variables, constants, etc. The composition rules of identifiers in our daily java coding are as follows:

  • 1. It consists of letters, numbers, underscore "_", dollar sign "$", and the first character cannot be a number.
  • 2. You cannot use keywords in java as identifiers.
  • 3. Identifiers are case-sensitive (case-sensitive).

  Of course, we make the following conventions for the naming of identifiers in Java:

  • 1. Small camel case naming: variable names and method names
      start with lowercase letters, and capitalize the first letter of each word starting from the second word.
  • 2. Big camel case naming:
      The first letter of each word in the class name is capitalized.
  • 3. It is best to name the identifier so that it can be known by the name , such as username, studentNumber, etc.

  The specific naming rules are as follows:

Eight, knowledge summary

  Finally, we summarize all the knowledge points introduced in this article through xmind; the specific knowledge points are summarized as follows:

Summarize

  Start with the previous article . We will introduce the relevant knowledge of java to you. The last article introduced the download, installation and configuration of the java environment, and verified whether our java environment was successfully installed by CMDentering the command java-versionin it.Again, if you want to follow my blog to continue learning java, you must install jdk on your computer according to the installation method in the previous article .. This article introduces some of the basic grammar of java, mainly including the use of annotations, keywords in java, and the introduction of constants, variables and data types. Finally, the content introduced in this article is made into a mind map through xmind, which is more convenient for everyone to learn and summarize. This part is relatively simple, but the foundation is very important, and the code involved is not a lot. It is written for java knowledge points and zero-based friends.If you have java foundation, you can skip this part. Now the development of java is in full swing, and it occupies a pivotal position in the development. Therefore, as a developer in the computer industry, it is necessary to learn java. Let's swim together in the ocean of java! ! ! Life is endless and struggle is endless. We work hard every day, study hard, and constantly improve our ability. I believe that we will learn something. come on! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324154829&siteId=291194637