Java Learning Note II: Data Types
1. integer: No decimal part, allowable negative number, Java integer divided into 4 kinds: int short long byte
1.1 int is most commonly used, an int type variable occupies 4 bytes in memory, the range of values from 2 147 483 6 to 2 147 483 647 is more than 2 billion, and if it is used to store values greater than 2 billion, it is best to use a long type.
1.2 Int and integer:
Data types in Java are divided into basic data types and complex data types. int is the former and integer is the latter.
Integer is the encapsulated class of int, which provides many conversion methods that must be used when storing data in ArrayList and HashMap, because the container can only be loaded with object, if only counting, data basic arithmetic, passing parameters such as int is enough.
1.3 How to use:
declares an int type variable int i=0; The initial value is: 0
Declares an integer type variable, which is actually an instantiated object operation, an integer i=new integer (0);
or an integer i=0; The initial value is null.
2. float type: float double
since float is a single precision, the valid digits are in the 6~7 bit, most of the cases are not sufficient, so in most cases, the double type is used in the program.
Use: float f=1.8f;
Double d=1.8d; or double d=1.8;
3.boolean type
The Boolean type has two values: false true. Mainly used for logical judgment.
4. variables and Constants
Java variable naming conventions: The Java language is case-sensitive, variables named a and a are different variables, variable names can start with $, letters, underscores, and can contain numbers, letters, and underscores. There are two types of naming conventions: Pascal (all words capitalized) and camel (all the first letters of the word except the first word).
Constants in Java are declared with the final keyword, constants can only be assigned once, once the constants are assigned, they cannot be changed, and constant names are generally named in all capitals.
5. Operators
5.1 Arithmetic operators: include plus (+), minus (-), multiply (*), divide (/), modulo (%)
An integer is thrown by 0 except for an exception, and the floating-point number is 0 apart from the result of Infinity or Nan;
Simplified format for two-tuple operators in assignment statements: X=x+4 can be written as: x+=4;
5.2 Self-increment/decrement operator: variable +1 or-1 simplified format for operation: x=x+1 writing x + +;
X=x-1 writing x--; It is divided into two types: prefix (++x): Advanced line +1 operation; suffix (x + +): after +1 operation;
5.3 Relational operators:
= =: detects equality.
! =: Detects whether it is not equal.
<: less than;: greater than; <=: less than or equal to; >=: greater than or equal to;
!: logical non; &&: Logic and; | | : Logical OR;
6. data type conversion
Conversion principle: From low-precision to high-precision conversion
byte->short->int->long|double; char->int;
Int->float;long->float|double; These three conversions are likely to result in loss of precision
When two values are two-dollar operation, the two operands are converted to the same data type, and the operation is
If one of the two operands has a double type, the other is converted to a double type;
Otherwise, if one is a float type, the other is converted to float type;
Otherwise, if one is a long type, the other is converted to a long type;
Otherwise, two numbers will be converted to int type.
Cast type: Double d=9.96; int i= (int) D; The value of D is 9;
Java Learning Note II: Data types