1, java comments
COMMENTS - notes explain the meaning of the code.
Comments are for yourself or others in the future to help understand the meaning of the code.
There are three types of notes:
1. Single line notes // Note Content
You can only write one line above or after the code.
2. Multiline comments /* Note content*/
On the top of the code, you can write multiple lines and include single line comments.
3. Document notes /** Note content * / [not commonly used, the inherited development environment used in the future will be automatically generated]
You can write multiple lines and include single line comments. You can edit an independent document through the javadoc.exe tool. Comments will not be executed. You can debug the program by adding comments.
For example;
//Create a java class named Holle /* public Represents an access restriction modifier for a java class class--Keywords for creating classes Holle--Name of the class [capitalize each word] */ public class Holle{ //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. /* publics--Is the access restriction modifier for this main method static--Is a static modifier [function: allocate space in advance] void--The main method has no execution result to return main--Method name [lowercase first, uppercase from the first letter of the second word] String args[]--Parameter list [there is a string list named args] //System.out.print--Print out the "Holle world" string on the console */ public static void main(String args[]){ //System.out.print -- print out the "Holle world" string on the console System.out.print("Holle world"); } }
2, java identifier
In the above example, we see some elements in Java, such as Java classes, methods, arrays... These elements have their own names, which are considered identifiers in Java.
The text / string used to name elements in java is the identifier
Naming rules for identifiers:
1. It consists of 26 English characters (a ~ Z, a ~ z), numbers (0 ~ 9), underscores () and dollar symbols ($).
2. Cannot start with a number [1Holle]
3. Strictly case sensitive [Holle] holle]
4. The identifier can be of any length. [try not to be too long]
5. Chinese cannot appear. [code may appear in Chinese]
6. Cannot be keyword. [keyword occupied]
3, Keywords for Java
The programming language of java gives special meanings to some words. These words with special meanings are keywords
Keyword occupied by java program
Keywords in java are:
Meaning of each keyword in java: https://baike.baidu.com/item/java%E5%85%B3%E9%94%AE%E5%AD%97/5808816?fr=aladdin
4, Variables in Java
Variable -- the data that may be modified [changed] at any time during the operation of the program is a variable.
Essence: it is the data storage space opened up in the process of variable program operation to store data
The function of using variables to store data: saving space
Composition of variables
Format: data type of variable Variable name = initial value of the variable;
For example: Int number = 234;
one The data type of the variable - determines the size of the storage space corresponding to the variable
2. Name of variable --- identifier [all letters and lowercase]
3. Initial value of variable - default. You can also set an initial value to match the data type
4. Scope of variable -- effective range of side wave and creation position of variable
5, Data types in Java
Data types in Java are divided into basic data types and conforming data types
1. 8 basic data types [native data types]
1.1 byte type -- type -- 1 byte [8-bit binary] - 128 ~ - 127 [- 2 to the 7th power ~ 2 to the 7th power - 1]
The value range cannot be exceeded when using assignment
1.2 short -- short--2 bytes [16 bit binary] - 32768 ~ 32767 [- 2 to the 15th power ~ 2 to the 15th power - 1]
The value range cannot be exceeded when using assignment
1.3 integer -- int------4 bytes [32-bit binary] - 2147483648 ~ 2147483647 [- 31st power of 2 ~ 31st power of 2 - 1]
The value range cannot be exceeded when using assignment
1.4 long integer -- int -- long -- 8 bytes [64 bit binary] - 9223372036854775808 ~ 9223372036854775807 [- 63rd power of 2 ~ 63rd power of 2 - 1]
When using assignment, "L" and "L" are required as suffixes
1.5 single precision floating point [decimal] - float--4 bytes, 7 ~ 8 are reserved as significant digits, which is not 100% accurate
When using assignment, "F" and "F" are required as suffixes
1.6 double precision floating point [decimal] - double---8 bytes Retain 15 ~ 16 significant digits, which is not 100% accurate
When using assignment, "d" and "d" can be used as suffixes or not.
1.7 character type -- char--2 bytes -- 0 ~ 65535
Machine [0 / 1] - hexadecimal -- code [97--a]
Character types can be converted to integers.
A single symbol enclosed in single quotes is a character type
\ Slash Escape character \
Double quotes Escape character \"
' Single quotation mark Escape character \'
Tab escape character \ t
Line feed Escape character \ n
For example:
public class DataType{ public static void main(String args[]){ /* Byte type -- byte -- 1 byte [8-bit binary] 128 ~ 127 [- 7th power of 2 ~ 7th power of 2 - 1] //The value range cannot be exceeded when using assignment*/ byte byte1=126; System.out.println("byte=="+byte1); //Incompatible type: conversion from int to byte may be lost //Because the default number type in java is int, the conversion of int from byte is not compatible //byte byte2=129; /* Short -- short -- 2 bytes [16 bit binary] - 32768 ~ 32767 [- 15th power of 2 ~ 15th power of 2 - 1] //The value range cannot be exceeded when using assignment*/ short short1 = 128; System.out.println("short=="+short1); /* Integer -- int--4 bytes [32-bit binary] - 2147483648 ~ 2147483647 [- 31st power of 2 ~ 31st power of 2 - 1] //The value range cannot be exceeded when using assignment*/ int numbet1=23; System.out.println("numbet=="+numbet1); //Error: too large integer: 2147483648 //Because it exceeds the maximum storage range of int data type, it will cause data overflow //int numbet2=2147483648; / / error: too large integer /* Long -- long--8 bytes [64 bit binary] - 9223372036854775808 ~ 9223372036854775807 [- 63rd power of 2 ~ 63rd power of 2 - 1] //When using assignment, "L" and "L" are required as suffixes*/ /*At this time, 127 defaults to int integer, but the long long integer data contains int integer, so it will not cause data overflow and can be successfully converted automatically*/ long long1=127;//(implicit data type conversion) System.out.println("long1=="+long1); long long2=127L; System.out.println("long2=="+long2); /* Single precision floating point [decimal] - float---4 bytes, 7 ~ 8 significant digits reserved, not 100% accurate //When using assignment, "F" and "F" are required as suffixes*/ //Because a single precision floating-point type has a larger range than an int integer, the type is implicitly converted float xiaoshu=124;//(implicit data type conversion) System.out.println("xiaoshu=="+xiaoshu);//124.0 float xiaoshu1=124F; System.out.println("xiaoshu1=="+xiaoshu1);//124.0 //Incompatible types: conversion from double to float may be lossy //Because the decimal is double by default in the program, it cannot be converted to single precision //float xiaoshu2=124.0;// Switching from double to float can be costly /* Double precision floating point [decimal] - double---8 bytes, 15 ~ 16 significant digits reserved, not 100% accurate //When using assignment, "d" and "d" can be used as suffixes or not.*/ double double1=126; System.out.println("double1=="+double1);//124.0 double double2=126.0; System.out.println("double2=="+double2);//124.0 double double3=126D; System.out.println("double3=="+double3);//124.0 /* Character type -- char--2 bytes -- 0 ~ 65535 Machine [0 / 1] - hexadecimal -- code [97--a] Character types can be converted to integers. A single symbol enclosed in single quotes is a character type \ Slash escape character\ "Double quote escape character\“ 'Single quote escape character \ ' Tab escape character \ t Newline escape character \ n */ System.out.println("\"Double quote escape character\""); System.out.println("\'Single quote escape character\'"); System.out.println("\\Oblique escape character\\"); System.out.println("Change careers\n Escape character"); System.out.println("Tab[ tab]\t Escape character"); } }