A few additions to the first day of study:
Environment variable setting: (Why do you want to set it? Because when running javac on the dos command line , if it is not run in the jdk/bin directory, an error will be reported. For convenience, set the path path, because if it is not in the jdk/bin directory When running javac , the system will automatically go to the path to find javac.ese )
Java_home=D:\jdk
Path=%Java_home%\bin
By the way, class is the most basic unit in java
Well, here are some lessons learned the next day:
1. Keywords:
Features: all lowercase letters; fonts will be color-coded in the advanced editor;
goto and const are reserved keywords and are generally not used
2. Custom Identifier:
Naming rules:
1. Identifiers can only be composed of numbers, letters, dollar signs, RMB signs, and underscores, using camel case nomenclature (the number under the word is a camel);
2. The identifier cannot start with a number;
3. To see the name and know the meaning;
4. The name is case-sensitive and generally does not limit the length;
5. Identifier names cannot be keywords .
Naming rules:
1. Class or interface:
Single word: the first letter is capitalized, the rest of the letters are lowercase;
Multiple words: Capitalize the first letter of each word and lowercase the rest.
2. Method or variable:
Single word: all lowercase letters;
Multiple words: The first word is all lowercase, the rest of the words are capitalized, and the rest are lowercase.
3. Constants:
Single word: all uppercase letters;
Multiple words: All uppercase letters, with underscores connecting each word.
3. Notes:
Types of:
Single-line comments: // Single-line comments can be nested;
Multi-line comments: /*
//
*/ Multi-line comments can nest single-line comments, but not multi-line comments.
Documentation comments: /**
*/
4. Constants:
Literal constants:
Types of:
Use " " for string constants ;
Numerical constants: integer, decimal;
Boolean constants have only two values, true and false ;
Use ' ' for character constants .
Note: Since java uses the unicode encoding system, 1 character = 2 bytes, and 1 Chinese = 2 bytes, so 1 Chinese = 1 character
custom constants?
5. Variables
There are eight basic data types in four categories:
Integer data:
byte 1 byte =8bit Value range: -128~127
short 2 bytes =16bit
int 4 bytes =32bit
long 8 bytes =64bit
Note: By default, numbers are of type int , unless you specifically declare it yourself
Float:
float 32bit=4 bytes
double 64bit=8 bytes
Note: By default, it is defined according to the double type, unless otherwise specified
Character type:
char 2 bytes
Boolean:
boolean 1 or 4 bytes
When defining a variable, it occupies 4 bytes
When defining an array variable, each element occupies 1 byte
It should be noted that the string type String is a reference data type, not a basic data type. Remember! Remember! !
Type conversion:
Default type conversion: the value range is automatically converted from small to large
byte,short,char--》int--》long--》float--》double
byte , short , char are automatically converted to int when participating in the operation
Note: When the first type of output is String , then the output is followed by String type
example:
System.out.println('a'+1);//98
System.out.println("hello"+'a'+1);//helloa1
System.out.println('a'+1+"hello");//98hello
System.out.println("5+5="+5+5);//5+5=55
System.out.println(5+5+"=5+5");//10=5+5
Coercion: target data type variable name = (target data type) converted data
例: byte b1,b2,b;
b=b1+b2;// This sentence will compile error, because b1 and b2 are both byte types, they are automatically converted to int when they participate in the "+" operation , so an error will occur
---------------------------------------------------------------------------------------------------------------------------------------------------------
float f1 = (float)12.345;
float f2 = 12.345f;
The difference between the two codes is: f1 is actually converted by a double ,
And f2 has been declared as float type when it is defined
-------------------------------------------------------------------------------------------------------------------------------------------------------------
byte b=150 ; //There will be a compilation error, because 150 exceeds the value range of byte
Correction: byte b=(byte) 150// Force type conversion, the output is -106=-128+ ( 150-128 ) ;
byte b1=(byte)128 output is -128
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The author has always had a doubt before: about the difference between byte b=1+3 ; and int a=1; int b=3;byte=a+b;
The author believes that although the former 1 and 3 are of int type by default , since they are constants, the assignment operation is from right to left, so first calculate 1+3=4 , and then see if 4 exceeds the range of byte . If it exceeds, then Coercion is required, if not, it is directly assigned; so the former is not wrong;
The latter is because a and b are variables, and the assignment operation also runs from right to left, so ( a+b ) is of type int as a whole , and it needs to be cast to prevent the program from reporting an error.
So I think the key point is: if you assign a constant, then you only need to see if the constant exceeds the value range of the target type;
If you assign a variable, you need to compare whether the target variable and the assigned variable type satisfy the default type conversion.