The basics of Java SE -- java basic syntax

preface

  from Last article At the beginning, we'll introduce you to the relevant knowledge of Java. The first thing we'll introduce to you is the introduction and core of Java - the content of Java SE. I hope you must learn it well. This is the basis for learning java frameworks and other micro services later. You must learn it well. In addition, during this period, I will not only introduce you to the basic knowledge of Java, but also introduce you to the corresponding cases. You may have seen these cases more or less, but will give specific problem-solving ideas, that is, I will take you through the process of how to start with a problem, find the problem-solving ideas and put the ideas into the code, You will form a practical ability to solve problems, which will not only give you the learning of java knowledge, but also improve your practical ability. Therefore, it is strongly recommended that you follow my advice First article Study hard. After learning, I believe it will improve everyone's coding ability, whether it's java or yourself. In addition, from the beginning of this article, in order to let you have a systematic understanding and review of the content we introduced, I will give a general introduction to the knowledge points introduced in this article at the end of the article Over Xmind For your review, so that you can have a better grasp and understanding of the knowledge architecture of java.
  Last article This paper introduces the download, installation and configuration of Java environment, and verifies whether our Java environment is successfully installed by entering the command java version in CMD. Again, if you want to follow my blog to continue learning Java, you must follow Last article The installation method is on your own computer Install the jdk OK. This article introduces some of the basic syntax of Java, 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. Not a lot of code is involved. It is written for java knowledge points and zero foundation partners. If you have Java foundation, you can skip this part. Actually, you've seen me Python variables and simple data types It is not difficult to find that there are also contents in this part, and the ideas and knowledge points are the same, including loop as well as Conditional statement The same is true, but the grammar is different. Therefore, the ideas are the same, but the grammar of the language is different. Next, we will first introduce the relevant knowledge of annotations in java.

1, Notes

   notes are the interpretation and explanatory text of the code, which can improve the readability of the program. Therefore, it is very important to add necessary notes in the program. Each programming language has its own way of annotation. There are three types of annotations in Java:

  single line note. Its format is * * / / * *. The text from / / to the end of the line will be used as comment text. For example: / / this is a single line annotation text
  multiline comment. Its format is to enclose a long comment with / * and * /. For example:

/*
This is multiline annotation text
 This is multiline annotation text
 This is multiline annotation text
*/

  what we need to pay special attention to here is that multi line comments cannot be nested.

  document notes. It will start with / * * and end with * /

  in order to let you see this effect in the code, we still use Last article HelloWorld in Java program to realize the small knowledge point of annotation:

/*
	Java The most basic component of a program is a class.
	
	Class definition format:
		public class Class name{
			
		}
		
	This is the HelloWorld class I defined
*/
public class HelloWorld {
	/*
		This is the main method
		main Method is the entry method of the program, and the execution of code starts from the main method
	*/
	public static void main(String[] args) {
		// This is an output statement. The contents of "" can be changed
		System.out.println("itheima");
	}
}

  note that during code operation, IDEA will automatically skip the contents of these comments, so the execution effect is consistent with the results we have seen before; The specific implementation results are as follows:

2, Keywords

  keywords refer to words with special meaning given by the java language. Keywords are mainly characterized by:

  all letters of the keyword are lowercase
  common code editors highlight keywords, such as public, class, static, etc.

  the following are the common keywords in java:

3, Data type

1. Computer storage unit

  we know that computers can be used to store data, but whether it's memory or hard disk, The smallest information unit of a computer storage device is called "bit", which is also called "bit", usually represented by a lowercase letter "B". The most basic storage unit in a computer is called "byte" , usually represented by the capital letter "B", a byte is composed of 8 consecutive bits. In addition to bytes, there are some common storage units, and their 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 the data in Java must be clear about the data type. Data types in Java include basic data types and reference data types. Among them, the basic data types in Java mainly include:

   where e+38 represents the 38th power multiplied by 10. Similarly, e-45 represents the negative 45th power multiplied by 10. In addition, in java, integers are of type int by default, and floating-point numbers are of type double by default. Next, let's introduce the knowledge of constants.

4, Constant

   constant: the amount whose value cannot be changed during program operation. Generally speaking, constants in Java have the following categories:

   1. Multiple characters of string constants enclosed in double quotation marks (can contain 0, one or more), such as "a", "abc", "China", etc
   2. Integer constant, such as: - 10, 0, 88, etc
   3. Decimal constant decimal, such as: - 5.5, 1.0, 88.88, etc
   4. A character constant enclosed in single quotation marks, such as' a ',' 5 ',' B ',' medium ', etc
   5. Boolean constant Boolean value, indicating true and false. There are only two values: true and false
   6. The null constant is a special value, which is null

    of course, among these constants, except for null constants, other constants can be directly output using output statements. Next, we will further understand the application of constants through a small case. The specific implementation code is as follows:

/*
	Constant:
		The amount whose value cannot be changed during program operation.

	Constant classification:
		String constant: 	 Content enclosed in double quotation marks. "HelloWorld", "dark horse programmer"
		Integer constant: 		 Numbers without decimals. 666,-88
		Decimal constant: 		 Numbers with decimals. 13.14,-5.21
		Character constant: 		 The content enclosed in single quotation marks. " A','0 ',' me '
		Boolean constant: 		 Boolean value indicating true or false. true,false
		Null constant: 		 A special value, null. null
*/
public class ConstantDemo {
	public static void main(String[] args) {
		//string constant 
		System.out.println("HelloWorld");
		System.out.println("Welcome to my blog");
		System.out.println ("https://blog.csdn.net/Oliverfly1");
		System.out.println("--------");
		
		
		//integer constant 
		System.out.println(666);
		System.out.println(-88);
		System.out.println("--------");
		
		//Decimal constant
		System.out.println(13.14);
		System.out.println(-5.21);
		System.out.println("--------");
		
		//character constants 
		System.out.println('A');
		System.out.println('0');
		System.out.println('I');
		System.out.println("--------");
		
		//Boolean Literals 
		System.out.println(true);
		System.out.println(false);
		System.out.println("--------");
		
		//Null constant
		//Null constants cannot be output directly
		//System.out.println(null);
	}
}

   this program code includes the above constant type applications. The specific operation effects are as follows:

   after introducing constants, I'll introduce you to another data structure - variables. It is also important in our programming language. Although it is simple, it will be used often in the future.

5, Variable

   variable refers to the amount whose value can be changed during program operation. In fact, in essence, a variable is a small area in memory, and its value can change within a certain range. We generally follow a certain format when defining a variable, as follows:

// Data type variable name = initialization value// Declare variables and assign values
int age = 18;
System.out.println(age);

   of course, there can be another way, as follows:

// Declaration before assignment (assignment before use)
//Data type variable name;
//Variable name = initialization value;
double money;
money = 55.5;
System.out.println(money);

  you can also define multiple variables of the same data type in the same row, separated by commas. However, this method is not recommended to reduce the readability of the program. In order to make you better understand the definition and use of variables, we can help you better understand this knowledge point through a small case:

/*
	Variable definition format:
		Data type variable name = variable value;
		
	Basic data type:
		byte,short,int,long,float,double,char,boolean
		
	Use of variables:
		Value format: variable name
		
		Modify value format: variable name = variable value;
*/
public class VariableDemo01 {
	public static void main(String[] args) {
		//Define variables
		int a = 10;
		
		//Output variable
		System.out.println(a);
		
		//Modify variable
		a = 20;
		System.out.println(a);
	}
}

   the specific program execution effects are as follows:

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

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

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

/*
	Precautions for variable use:
		The name cannot be repeated
		The variable is not assigned and cannot be used
		long When defining a variable of type, in order to prevent the integer from being too large, L should be added after it
		float When defining variables of type, in order to prevent type incompatibility, add F after them
*/
public class VariableDemo02 {
	public static void main(String[] args) {
		//Defines a variable of type byte
		byte b = 10;
		System.out.println(b);
		
		//Define variables of type short
		short s = 100;
		System.out.println(s);
		
		//Defines a variable of type int
		int i = 10000;
		System.out.println(i);
		
		//Defines a variable of type double
		double d = 13.14;
		System.out.println(d);
		
		//Define variables of type char
		char c = 'a';
		System.out.println(c);
		
		//Define variables of type boolean
		//boolean b = true;
		//System.out.println(b);
		boolean bb = true;
		System.out.println(bb);
		System.out.println("--------");
		
		//Define variables of type long
		long l = 10000000000L;
		System.out.println(l);
		System.out.println("--------");
		
		//Define variables of type float
		float f = 13.14F;
		System.out.println(f);
	}	
}

  the specific implementation effects are as follows:

   so far, we have introduced the variable part to you. I hope you can have a clear understanding of this knowledge point through these two small cases. Next, let's introduce the relevant knowledge of brick replacement.

6, Type conversion

  in Java, some data types can be converted to each other. There are two cases: automatic type conversion and forced type conversion. First of all, we will introduce automatic type conversion:
  assign a value or variable representing a small data range to another variable representing a large data range. This conversion mode is automatic and can be written directly. For example:

double num = 10; // Assign 10 of type int directly to type double
System.out.println(num); // Output 10.0

  followed by cast; That is, assign a value or variable representing a large data range to another variable representing a small data range. In addition, the forced type conversion format: target data type variable name = (target data type) value or variable; Specific applications are as follows:

double num1 = 5.5;
int num2 = (int) num1; // Cast num1 of type double to type int
System.out.println(num2); // Output 5 (decimal places are discarded directly)

   the conversion of specific data types is shown in the figure below:

    however, for the above figure, we describe 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); // Output 97
  • 2. Integers are of type int by default. byte, short and char data will be automatically converted to type int.
byte b1 = 10;
byte b2 = 20;
byte b3 = b1 + b2; 
// The third line of code will report an error, b1 and b2 will be automatically converted to int type, and the calculation result is int. the assignment of int to byte requires forced type conversion.
// Amend to read:
int num = b1 + b2;
// Or:
byte b3 = (byte) (b1 + b2);

  what we need to pay attention to here is that boolean types cannot be converted to other basic data types. Next, we will make an application of data type conversion through a case to facilitate your understanding of this part; The specific implementation is as follows:

/*
	Type conversion
*/
public class ConversionDemo {
	public static void main(String[] args) {
		//Automatic type conversion
		double d = 10;
		System.out.println(d);
		
		//Defines a variable of type byte
		byte b = 10;
		short s = b;
		int i = b;
		
		//This is not possible. The types are incompatible
		//char c = b;
		
		//Cast type
		int k = (int)88.88;
		System.out.println(k);
	}
}

  the specific implementation effects are as follows:

7, Identifier

  finally, I'll introduce you to the relevant knowledge of identifiers in Java. Identifier is the name used by users in programming. It is used to name classes, methods, variables, constants, etc. In our daily java coding, the composition rules of identifiers are as follows:

  • 1. Consists of letters, numbers, underscores "" The dollar is composed of "$" and the first character cannot be a number.
  • 2. Keywords in java cannot be used as identifiers.
  • 3. Identifiers are case sensitive (case sensitive).

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

  • 1. Small hump naming: variable name, method name
       the first letter is lowercase, and the first letter of each word is capitalized from the second word.
  • 2. Big hump naming: class name
      the first letter of each word is capitalized.
  • 3. The identifier should be named according to the meaning of the name, such as username, studentNumber, etc.

  the specific naming rules are as follows:

8, Knowledge summary

  finally, we summarize all the knowledge points introduced in this paper through xmind; The specific knowledge points are summarized as follows:

summary

  from Last article Start. Let's introduce you to java, Last article This paper introduces the download, installation and configuration of Java environment, and verifies whether our Java environment is successfully installed by entering the command java version in CMD. Again, if you want to follow my blog to continue learning Java, you must follow Last article The installation method is on your own computer Install the jdk OK. This article introduces some of the basic syntax of Java, including the use of annotations, keywords in Java, and the introduction of constants, variables and data types. Finally, the content introduced in this paper 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. Not a lot of code is involved. It is written for java knowledge points and zero foundation partners. If you have Java foundation, you can skip this part. Now the development of Java is in full swing and occupies a pivotal position in the development field. Therefore, as a developer in the computer industry, it is necessary to learn Java. Let's travel 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 we will learn something. come on.

Keywords: Java JavaSE

Added by Asnom on Mon, 03 Jan 2022 01:03:14 +0200