当前位置:网站首页>Identifier keyword literal data type base conversion character encoding variable data type explanation operator
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
2022-06-11 09:19:00 【lwj_ 07】
1、 identifier
/*
About java The identifier in the language
1、 What is an identifier ?
- stay java Any word in the source program that you have the right to name is an identifier .
- Identifier in Editplus Highlighted in black
- What elements can an identifier represent ?
* Class name
* Method name
* Variable name
* Constant names
* The interface name
...
2、 Naming rules for identifiers ?【 Not according to this rule The compiler will report an error This is grammar 】
* Only by Numbers 、 Letter 、 Underline _、 Dollar symbol $ form , No other symbols
* Cannot start with a number
* Case sensitive
* Keywords cannot be identifiers
* Theoretically, there is no length limit But it's better not to be too long
3、 Naming conventions for identifiers ?【 It's just a norm , It's not grammar , If you don't follow the compiler, you won't report an error 】
* It's better to see the name and the meaning
public class UserService{
public void (String username,String Password) {
}
}
* Follow the hump naming method
SystemService
UserService
* Class name 、 The interface name : title case , After that, each word is capitalized
* Variable name 、 Method name : Initial lowercase , After that, each word is capitalized
* Constant names : All capitals
*/
public class IdentifireTest01 //IdentifierTest01 Is a class name , The name can be changed
// main Is a method name
{ public static void main(String[] args){ //args It's a variable name
}
}2、 Face value
/*
About literal value :
* Face value :
-10、100 It's an integer literal
-3.14 It's a floating-point literal
-"abc" " Chinese " Enter a string literal
-'a' ' people ' It's a character literal
-true、false It's a Boolean literal
* Literal is data .
Be careful :
java All string literals in a language must be enclosed in double quotes
java All character literals in a language must be enclosed in single quotes
*/
public class ConstTest01
{
public static void main(String[] args){
System.out.println(10);
System.out.println(3.14);
System.out.println(" Chinese ");
System.out.println(' people ');
// System.out.println(' China '); Report errors : Compiler error , Because only a single character can be stored in single quotation marks
}
}
3、 Variable
/*
Variable
A variable is essentially a piece of memory , This space has data type 、 Variable name 、 Face value 【 data 】 form .
*/
public class VarTest01{
public static void main(String[] args){
int i=10;
System.out.println(i);
//int a,b,c=300; Report errors :a and b Not initialized ( assignment ) c assignment 300
//System.out.println(a);
//System.out.println(b);
//System.out.println(c);
}
}/*
*/
public class VarTest01{
public static void main(String[] args){
int i=100;
i=80;
System.out.println(i);
// Report errors : In the same scope , Variable name cannot be duplicate ( because i=100 Has taken up memory space ) But variables can be reassigned .
//int i =90;
//System.out.println(i);
}
}4、 Scope of variable ( Member variables local variable )
/*
What is the scope of a variable ?
* Out of the curly brackets, I don't know
*/
public class VarTest01{
// Notice the static It can't be omitted
static int k=90; // Equivalent to a global variable ( Member variables )
public static void main(String[] args){
// Variable i The scope of is main Method
// Throughout main Methods are effective and visible
int i=100; // Equivalent to local variable Only in main In this paper, we use the method of stay dosome You can't use
System.out.println(k); // You can visit k
// The following will write a for Loop statement
//for (int a=0;a<10 ;a++ ) // a The scope of the variable is the whole for loop for At the end of the cycle a Variable memory is freed
//{
//}
//System.out.println(a); // Report errors !!! cannot access a Variable
int j; // Equivalent to a global variable
for (j=0;j<10 ;j++ )
{
}
System.out.println(j);
}
public static void dosome(String[] args){
// Report errors !!!
// It is no longer accessible here main Methods i Variable
//System.out.println(i);
System.out.println(k); // You can visit k
}
} 
5、 data type
/*
The role of data types ?
There's a lot of data in the program , Every data has a related type , Different data types occupy different amounts of space
The role of data types is to guide JVM Allocate the corresponding memory space to the data during operation
Basic data type :( Including four categories and eight species )
Integer type :
int 4 byte
long 8
short 2
byte 1 Value range 【-128~127】
floating-point :
float 4
double 8
Boolean type :
boolean 1 Value range 【true,false】
Character : ( It means words )
char 2 Value range 【0~65535】
character string "abc" Not a basic data type , It belongs to the reference data type , Characters belong to the basic data type
* String in double quotation marks ""
* The characters are in single quotation marks ''
What is byte encoding ?
In order to enable computers to express words in the real world , We need human intervention , It is the responsibility of the person in charge to make a plan in advance " written words "
and " Binary system " The contrast between , This contrast is called : Character encoding .
Earliest character encoding :ASCII code
'a'--->97
'A'--->65
'0'--->48
java What kind of encoding does the language use ?
-java The language source code is unicode Encoding mode , therefore " identifier " It can be in Chinese .
public class Student {
}
Now in actual development , In general use UTF-8 There are many coding methods .【 Unified coding 】
*/A detailed description of data types :
char type
public class DataTypeTest
{
public static void main(String[] args){
char a='a';
System.out.println(a);
// Define a Chinese character and a Chinese character 2 Bytes char just 2 Bytes
// When it comes to 2 An error will be reported in Chinese
char b=' countries ';
System.out.println(b);
//ab Cannot use single quotation marks for string Report errors !!!
//char c='ab';
//System.out.println(c);
// An error will also be reported when the double quotation marks are used. The types are incompatible
//char d="a";
//System.out.println(d);
}
}
Escape character
/*
About java In language char The type of :
Escape character \
\n A newline
\t tabs
\' Ordinary single quotation marks
\\ Ordinary backslash
\" Ordinary double quotation marks
Escape characters appear before special characters , Will convert special characters into ordinary characters
Such as :System.out.println("\"hello world!\""); \ The quotation marks on the left and right sides will be converted to normal "
*/
public class DataTypeTest
{
public static void main(String[] args){
// * Ordinary characters n
char a='n';
System.out.println(a);
// * \n A newline
System.out.println("hello");
System.out.println("world!");
// Be careful : print and println The difference between : print No line break after output println Refers to line wrapping after output
System.out.print("hello");
System.out.println("world!");
// Compile error Report errors
//System.out.println(""hello world!"");
System.out.println("\"hello world!\"");
// * \t tabs ( amount to tab)
// emphasize : Tabs are different from spaces , their ASCII Different sizes
char i='\t';
System.out.print("A");
System.out.print(i);
System.out.println("B");
// * Requires backslash characters to be output on the console
//char c='\'; Report errors The backslash will follow ' Escaped And if you use c Variables need to be complete '' Therefore, an error was reported
//System.out.println(c);
char c='\\'; // The first backslash escapes the backslash to \
System.out.println(c);
}
}
About JDK Self contained native2ascii.exe command , You can convert text into unicode The coding form is as follows :

How to decode ?

Integer type
About java Integer type in language :
data type Occupied space size The default value is Value range
----------------------------------------------------------
bytes 1 0 【-128~127】
short 2 0 【-32768~32767】
int 4 0 【-2147483648~2147483647】
long 8 0L
1、 java In language “ Integer literal value ” Default as int Type to deal with , To make this “ Integer literal value ” Be treated as long type
To deal with it , Need to be in “ Integer literal value ” Followed by l or L, It is recommended to use L.
( Such as long x=123; 123 Be first JVM Judge the value range as an integer literal , And then assign it to long type )2、java There are three ways to express integer literals in language :
The first way : Decimal system 【 Is a default way 】
The second way : octal 【 When writing octal integer literals, you need to use 0 Start 】
The third way : Hexadecimal 【 When writing hexadecimal integer literals, you need to use 0x Start 】
public class DataTypeTest
{
public static void main(String[] args){
int a=10;
int b=010;
int c=0x10;
System.out.println(a); //10
System.out.println(b); //8
System.out.println(c); //16
// From the above 1 It can be seen from the text 123 The literal value of this integer is int type
//i Variable declaration is also int type
//int Type of 123 Assign a value to int Variable of type i, So there is no type conversion
int i=123;
System.out.println(i);
// error : Too large integer : 2147483648
// because 2147483648 First by JVM As a int type however int The value range of type is 2147483647 Therefore, an error is reported
//long z=2147483648;
//System.out.println(z);// Change literal value to long type (long Type has a wide range of values )
long z=2147483648L;
System.out.println(z);
}
}
Cast type :
The principle of cast : Add the cast character “( Conversion type )” Post compilation can pass However, the accuracy may be lost during operation .
Raw data ( Such as :long type ):
00000000 00000000 00000000 00000000 00000000 00000000 00000000 01010100
Data after forced rotation ( Such as strong into int type ):
00000000 00000000 00000000 01010100
long k=27564123L;
int e=(int)k;
System.out.println(e);
byte specific characteristics
public class DataTypeTest
{
public static void main(String[] args){
Analyze whether the following data can be compiled successfully ?
It is not successful to compile according to the following data learned so far
reason :50 yes int The literal value of the type ,a yes byte Variable of type , It's obviously a large capacity int Convert to small capacity byte
To convert a large capacity to a small capacity, you need to add a cast character The following program did not add a converter So wrong reporting
Be careful : But the program did not report an error The following code has been compiled This explanation : stay java In language , When an integer literal
Not more than byte If the value range of the type is The face value can be assigned directly to byte Variable of type
byte a=50;
byte b=127;
byte c=128;// Beyond the byte The value range of the type Report errors
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Floating point type
/*
About floating point types :
float Single precision 【4 Bytes 】
double Double precision 【8 Bytes 】
stay java In language , All floating-point literals such as 【3.0】 Default as double Type to handle
Think of the face value as float Type to deal with You need to add... After the face value F or f
Be careful :double and float In the internal binary storage of the computer, the approximate values are stored .
Such as 10÷3=3.333333333333...*/
public class DataTypeTest
{
public static void main(String[] args){
// Face value 3.0 The default type is double type No conversion
double i=3.0;
System.out.println(i);// Face value 5.0 The default is double type double Type ratio float Large type So big turns small Report errors
//( No cast character added It is also easy to lose precision if you add it )
//float a =5.0;
//System.out.println(a);float b =5.1F;
System.out.println(b);
}
}
Boolean data type
/*
About Boolean data types :
boolean*/
public class DataTypeTest
{
public static void main(String[] args){
// java among 0 and 1 Are not compatible Report errors
//boolean i =1;boolean loginuser = true;
if (loginuser)
{
System.out.println(" congratulations Login successful !");
}
else{
System.out.println(" User login failed !");
}}
}
Mutual conversion of basic data types
/*
About the conversion between basic data types :
Conversion rules :
1、 The remaining of the eight basic data types except boolean type 7 Data types can be converted to each other
2、 The conversion from small capacity to large capacity is called automatic type conversion , Sort the capacity from small to large :
byte<short<int<long<float<double<char
notes :
Any floating-point type, no matter how many bytes it takes Are larger than integer type
char and short The number of types that can be represented is the same , however char You can take a larger positive integer
3、 Converting large capacity to small capacity is called cast , You need to add a cast character , The program can
Compile and pass , However, the accuracy may be lost during the operation phase , So use... With caution
4、 When the face value of the whole number does not exceed byte,short,char Value range of , It can be assigned directly to byte,short,char Variable of type
5、byte,short,char When you're mixing , Convert each into int Type and then do the operationbyte a=5;
short b=10;
int k =15;( Try not to let algorithms get mixed up Try not to write int x = a+b)
6、 Mixed operation of multiple data types , First convert to the type with the largest capacity, and then do the operation*/
Arithmetic operator
/*
About java Programming operator : Arithmetic operator
+ Sum up
- Subtracting the
* The product of
/ merchant
% Mod 【 modulus 】++ Self adding 1
-- Self reduction 1Be careful : There are multiple operators in an expression , Operators have priority , Indefinite parentheses , Priority is raised .
There is no need to memorize the priority of operators*/
public class DataTypeTest
{
public static void main(String[] args){
int i =3;
int a =6;
System.out.println(i+a);
// The following is a ++ For example (-- identical )
// About ++ Operator 【 Self adding 1】
//int b =9;
//++ Operators can appear after variable names 【 Monocular operator 】
//b++;
//System.out.println(b); //10
// ++ Operators can appear before variable names
//++b;
//System.out.println(b); //10
int c =100;
int d =++c;
System.out.println(c); //101 ++c So 100+1
System.out.println(d); //101 Empathy ++c by 101int e =300;
int f =e++;
System.out.println(e); //301 e++ So 300+1
System.out.println(f); //300 int f =e++ Assign first e Then add and subtract So first of all e=300 Assign a value to f
}
}
Relational operator
/*
About java Programming operator : Relational operator!= It's not equal to
The result of a relational operator must be Boolean :true/false
*/public class DataTypeTest
{
public static void main(String[] args){
int a =10;
int b =10;
System.out.println(a!=b); //false
}
}
Logical operators
/*
About java Programming operator : Logical operators
& Logic and 【 also 】( Operators on both sides are true The result is true)
| Logic or 【 perhaps 】( As long as one of the operators on both sides is true The results for true)
! Logic is not ( Take the opposite !false Namely true !true Namely false )
^ Logical XOR ( As long as the operators on both sides are different The results for true)
&& Short circuit and
|| Short circuit orThe short circuit and logic are the same as the final operation result , It's just that there is a short circuit
Short circuit or is the same as logic or final operation result , It's just a short circuit or a short circuit
*/public class DataTypeTest
{
public static void main(String[] args){
System.out.println(6>5&6>3); //true Operation level : First calculate 6>5 6>3 Compare again
System.out.println(6>5&6>8); //false
System.out.println(6>5|6>9); //true
System.out.println(6>8&6>10); //false
System.out.println(!false); //true
System.out.println(false^false); //false
System.out.println(false^true); //true
System.out.println("======================");
// Logic and short circuit
int x=10;
int y =8;
// Logic and
System.out.println(x<y & ++x<y);
System.out.println(x); //11// Short circuit and
//x<y The result is false, The result of the whole expression has been determined to be false
// So the back ++x<y The expression is no longer carried out , This phenomenon is called short circuit phenomenon
System.out.println(x<y && ++x<y);
System.out.println(x); //10}
}From a certain point of view, short circuit is more intelligent . Because the following expression may not execute , So the execution efficiency is high .
String join operators
/* String connection rules :
Numbers + Numbers ---> Numbers 【 Sum up 】
Numbers + " character string " ---> " character string "【 String connection 】*/
public class A
{
public static void main(String[] args){
System.out.println(10+30); //40
System.out.println("10+30"); // 10+30
System.out.println(10+"30"); //1030
System.out.println(10+20+"30"); //3030 Calculate from left to right
System.out.println(10+(20+"30"));//20+"30" Become a string "2030" Then string "2030" And then with numbers 10 Add up result 102030
int a=10;
int b=30;
// How to output 10+30=40 Well ?
// The first method
System.out.println("10+30=40"); //10+30=40
// The second method 【 Require dynamic methods Is not a fixed variable a and b】
System.out.println("10+30="+a+b);
//"10+30=1030" Splice first "10+30="+a ( character string + Numbers Directly joining together ) namely "10+30=10" Again "10+30=10"+b namely "10+30=1030"
System.out.println("10+30="+(a+b)); //10+30=40
System.out.println("a+30="+(a+b)); // a+30=40
// ask : How to handle a and b I'm writing ?
System.out.println(a+"+"+b+"="+(a+b));// Splicing process :a+"+",a+"+"+b,a+"+"+b+"=",a+"+"+b+"="+(a+b)System.out.println("=============================");
/*
Reference type String
String yes SUN stay javaSE The string type provided in
String.class Bytecode fileString Is the reference data type ,s Is a variable name. ,"abc" yes String The literal value of the type
String s="abc";
Be careful :String ss=10; Compile error incompatible types
*/
String username="junker";
//System.out.println(" Login successful welcome username Come back !");// Login successful welcome username Come back !
System.out.println(" Login successful welcome "+username+" Come back !");// Login successful welcome junker Come back !}
}
Ternary operator
/*
Ternary operator / Ternary operator / Conditional operator1、 Rule of grammar :
Boolean expression ? expression 1: expression 2
2、 Implementation principle of ternary operator :
When the result of a Boolean expression is true When , Select expression 1 As a result of the execution of the entire expression
When the result of a Boolean expression is false When , Select expression 2 As a result of the execution of the entire expression
*/public class A
{
public static void main(String[] args){
// Boolean variables
boolean sex =true;
// Report errors It's not a statement
//sex ? ' male ':' Woman ';// The correct process is as follows :
char c = sex ? ' male ':' Woman ';
System.out.println(c); // male
// Report errors Incompatible types : " male " by String type ' Woman ' by char type
//char d =sex ? " male ":' Woman ';
//System.out.println(d);sex =false; // to boolean Variable reassignment
// It can also be written as String type
String i = sex ? " male ":" Woman ";
System.out.println(i); // Woman
}
}
Using the ternary operator :
Receive input from the user 3 It's an integer And output their maximum value as the result
public class A
{
public static void main(String[] args){
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.print(" Please enter the first integer :");
int a =s.nextInt();
System.out.print(" Please enter the second integer :");
int b =s.nextInt();
System.out.print(" Please enter the third integer :");
int c =s.nextInt();
int max =a>b?a:b;
max =max>c?max:c;
System.out.println(" The maximum value is :"+max);
}
}边栏推荐
- Version mismatch between installed deeply lib and the required one by the script
- OpenCV CEO教你用OAK(四):创建复杂的管道
- 【软件】ERP体系价值最大化的十点技巧
- 报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
- Comment l'entreprise planifie - t - elle la mise en oeuvre?
- Sword finger offer II 041 Average value of sliding window
- 206. reverse linked list
- 报错[error] input tesnor exceeds available data range [NeuralNetwork(3)] [error] Input tensor ‘0‘ (0)
- 2130. maximum twin sum of linked list
- ERP体系能帮忙企业处理哪些难题?
猜你喜欢

Modularnotfounderror: no module named 'find_ version’

Some learning records I=

Version mismatch between installed deeply lib and the required one by the script

报错ModularNotFoundError: No module named ‘find_version’

Résumé de la méthode d'examen des mathématiques

Machine learning notes - spatial transformer network using tensorflow

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用

Augmented reality experiment IV of Shandong University

Comparison and introduction of OpenCV oak cameras

Talk about how to customize data desensitization
随机推荐
Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
Machine learning notes - in depth Learning Skills Checklist
远程办公最佳实践及策略
[share] how do enterprises carry out implementation planning?
Openstack explanation (21) -- installation and configuration of neutron components
报错device = depthai.Device(““, False) TypeError: _init_(): incompatible constructor arguments.
[scheme design] scheme of household oximeter based on single chip microcomputer
Leveldb simple use example
When the enterprise makes a decision, which part should lead the ERP project?
Comparison and introduction of OpenCV oak cameras
Talk about reading the source code
Sword finger offer II 041 Average value of sliding window
20. valid brackets
1493. the longest subarray with all 1 after deleting an element
MSF基于SMB的信息收集
Sword finger offer 18 Delete the node of the linked list
1400. construct K palindrome strings
Pytorch installation for getting started with deep learning
kubelet Error getting node 问题求助
1721. exchange nodes in the linked list
