The storage of floats and doubles in the computer

(1) must be binary storage (2) the highest bit is also a symbol bit (3) float is 4 bytes, 32-bit double is 8 bytes, 64-bit (4) float, 8-bit index double, 11-bit index bit (5) float, 23-bit tail double, Fifty-two mantissa digits

(1) When the manta of a decimal exceeds 23 or 52, it is truncated, resulting in a discrepancy in the data, that is, an inexact number. (2) The range of numbers that float and double can represent is larger than integers such as long byte: 1 byte short: 2 bytes int: 4 bytes long: 8 bytes float: 4 bytes double: Eight bytes double is a little bit more accurate than float, so float can be up to 7 to 8 decimal places, so this is scientific decimal. Double can be up to 15 to 16 decimal places, 7 to 8 decimal places

  • Example: 8.25 Step 1: binary 8–>1000 0.25–>0.01

Combined: 1000.01

  • Step 2: Deal with the decimal point because the decimal point is hard to represent, it doesn’t represent at all

    Take the binary that you just saw and write it in scientific notation. Take the decimal number as an example, scientific notation: 256.87564 -> Scientific notation 2.5687564 * 10 to the 2nd power 0.000675 -> Scientific notation 6.75 * 10 to the -4 power

Binary scientific notation: 1000.01 –> Scientific notation 1.00001 * n ^ 3 1010100.0011 –> Scientific notation 1.0101000011 * N ^ 6

Discovery: After using scientific notation, the left side of the decimal point is always 1 (because the integer part of scientific notation must be non-zero and have 1 bit)

So, the 1 part of the integer is gone, and the decimal part is gone

  • Step 3: You need to store the mantissa and the power to the right of the decimal point. For floats, 8 bits are used to store the power, called the exponent bit. For doubles, 11 bits are used to store the power, called the exponent bit

For float, 32-1 sign bits -8 index bits = 23 mantissa bits for double, 64-1 sign bits -11 index bits = 52 mantissa bits

The eight basic data types are as follows: byte: 1 byte short: 2 bytes int: 4 bytes Long: 8 bytes float: 4 bytes Double: 8 bytes char: 2 bytes Boolean: 1 byte /1 bit Boolean can only be expressed as true or false in a program, with the underlying 1 representing true and 0 representing false

Since decimals cannot be accurately represented in the underlying computer, we cannot use == to determine that two floating-point numbers are equal.

double d1,d2;
/ /...
if(d1==d2){}// is an error expression that must be avoided
Copy the code

What about the underlying type char?

(1) All characters are converted to binary, that is, characters are represented by an integer, which is the encoding value of the character set: the table corresponding to the integer encoding character: The earliest computers supported only 128 characters, and one character set was called the ASCII character set. Some countries, such as Europe, have extended the ASCII character set to 256 characters. The extended 128 characters are not universal, and the corresponding characters of a particular code are different in different countries. Asian and other countries extended the character set, and many, many different character sets emerged. For example, In China, GB2312 (early),GBK,Big5 and other character sets are now used by default GBK. Later, with the development of the Internet, a broader character set was created, the Unicode character set, which supports all characters in the world.

Before the Unicode character set, character sets and encodings were unified. For example, GBK was both a character set and an encodings. After the Unicode character set, there are many factors to consider, such as how many characters a binary stream should represent, and so on. The Unicode character set sets its own set of standards for parsing, called encodings. There is utf-8, UTF – 16, etc

Binary data flow: 11110101 10101100 10100010 10101111 01010100 ISO8859-1 (ASCII extended 256 bits) 5 characters GBK: 3 characters UTF-8:2 characters

Char char char char char char char char char char char char char char char char

The ASCII character set is backward compatible with all ASCII character sets. Some special characters ‘a’–>97, ‘b’–>98… ‘A’–>65, ‘B’–>66 … 48, ‘0’ – > ‘1’ – > 49… null–>0 TAB–>9

How is the char type represented in a program?

(1) Enclose a character in single quotes, for example: ‘a’

(2) The decimal coded value can be directly expressed in the decimal coded value range: 0-65535

For example, the encoding value of ‘still’ is 23578 (in decimal), and the hexadecimal value is 5C1A’ \u5C1A’ (in decimal). For some special characters, we can escape them. For example: Line feed ‘\n’ TAB ‘\t’ starts with the next TAB stop, each TAB stop defaults to 8 bits backspace: ‘\b’ return: ‘\r’ Ends the line single quotes: ” double quotes: ‘”‘ \ itself: ‘\’

Note:

Char variables are converted to int when evaluated,

However, constants of type CHAR are evaluated as long as they are within the range of the char value.

Basic data type conversion

(1) Automatic type conversion

  • Byte –>short–>int–>long–>float–>double char–> B: Byte –>short–>int–>long–>float–>double char–> B: byte–>short–>int–>long–>float–>double char–> B: Operations between byte and byte,short and short,char and char, or byte,short, or char are automatically upgraded to int. C: Boolean does not participate in any conversions D: When multiple data types are mixed, use the largest of them

(2) Cast format: value/expression

  • Byte <–short<–int<–long<–float<–double char<– tip: Casting in this case is risky, with the potential for overflow or loss of accuracy

  • B: Casting hints can also be used when we want to intentionally indicate the data type of a variable/value: in this case, casting is not risky

  • C: Boolean does not participate in any conversion

Conversion of String type to primitive data type

(1) String and any data type “+”, the result is a String (2) any data type value, just add “”, is a String (3) String cannot be cast to the primitive data type

The operator

(1) Classification

1. Divided by function:

  • Arithmetic operator
  • The assignment operator
  • Comparison operators/relational operators
  • Logical operator
  • Conditional operators:? :
  • An operator

2. Divide by number of required operands:

  • Unary operators: +a, a++
  • Binary operators: a+b, a>b, a = b
  • Ternary operators:? :

Arithmetic operator

Add: + (1) if it is a basic data type (except Boolean), + sum (2) if there is a string, + splicing systems: – a: * in addition: / if two integer type division, only to retain the integer part mould: % (1) the remainder (2) the remainder of plus or minus, like the dividend Is: + negative: – Autoincrement: ++, autoincrement 1 Autodecrement: –, autodecrement 1

Take the increment as an example: (1) when the increment expression is a separate statement, there is no difference between the first and the last, the result is the same, both are the increment variable +1 for example: i++; ++i;

(2) When the increment expression is not a separate statement, but is part of another operation or statement, the ++ expression is preceded by a different statement. A ++ : a++ : a++ : a++ : a++ : a++ : a++ : a++ : a++ : a++ : a++ : a++ : a++

The assignment operator

The most basic assignment operator is the = function: assign the value/expression result to the right of = to the variable to the left of =, that is, modify the value of the left variable

(1)= must be a variable on the left-hand side. (2)= has the lowest priority and must be the last one

There are extended families of assignment operators: +=, -=, *=, /=, %=….

(1) Treat the right side of = as a whole, and then use the variables on the left of = and the results of the values/expressions on the right to do + and – operations. (2) +=, -=, *=, /=, %=…. If the range of the finished result type exceeds the variable on the left, it implies a cast, so the result may be wrong (overflow or loss of precision)

Relational/comparison operators

Greater than: > Less than: < equal to: == Greater than: >= Less than or equal to: <= Not equal to:! =

(1) Relational operators are commonly used in conditional judgment. (2) There are only two types of expression results for relational operators: true/false

Logical operator

Logic and: Both conditions are true and only both sides of the ampersand are true, Results only to true true & true result is true true & false results is false false is false false and true & false result is false logic or: as long as | | symbols on both sides of one side is true, The result is true true | true result is true true | | is true false true false results is true or false | false results is exclusive or false logic: ^ Only two sides of the ^ sign are false on one side and true on the other, True ^ true true false ^ false False false false false false False False False False False ! True to false! False to true short circuit and: && The result is true only if both sides of the am& are true true && true the result is true true && false The result is false false &&? Result is false && than & high efficiency, because when the left side of the && to false, on the right side of the watch, the result is false short circuit or directly as long as the | | symbols on both sides of one side is true, the result is true true | |? The result is true or false | | true result is true or false | | false result is false | | efficiency higher than |, because when | | left is true, just don’t see on the right, the result is true

(1) The logical operator is used to represent the relationship between two conditions (2) the result of the logical operator expression is still true and false (3) the logical operator expression is still used as a condition

Conditional operator, unique ternary operator

Format: conditional expression? Result expression 1: Result expression 2 Operation: If the conditional expression is true, take the result of result expression 1, otherwise take result expression 2

Bitwise operator (the most efficient operator)

The result is 1 only if the corresponding binary bits are both 1. 1&1 the result is 1. 1&0 The result is 0. 0&1 the result is 0. | as long as there is 1, the corresponding binary is the result of 1 1 1 | | 1 the result is 1 0 the result is 1 0 0 | | 1 the result is 1 0 the result is 0 bitwise xor: ^ The result is 1 only when the corresponding binary bits are 1 and 0. 1 ^ 1 the result is 0. 1 ^ 0 The result is 1

(3) When the number of bits to the left exceeds the total number of bits in the current data type, the total number of bits to the left is subtracted

Only if you move to the left << will you have a loop, so if you move to the left and out of bounds 1, you will be on the far right

When the number of digits to the right exceeds the total number of digits of the current data type, subtract the total number of digits. For example: >>> < span style = “color: RGB (51, 51, 51); color: RGB (51, 51, 51); color: RGB (51, 51, 51)

class Test12BitOperator{
	public static void main(String[] args){
		System.out.println(1 & 2);
		/ * 1:00 00 0000 0000 0000 0000 0000 0000 0001 00 0000 by 2:00 PM Sunday, 0000, 0000, 0000, 0000, 0000, 0010 & : 0000 0000 0000 0000 0000 */
		
		System.out.println(3 & 5);
		/* 3:00 00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0101 & : 0000 0000 0000 0000 0000 0000 0001 */
		
		System.out.println(-3 & 5);
		0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 1101 5:0000 0000 0000 0000 0000 0000 0000 0101 & : 0000 0000 0000 0000 0000 0101 */
		
		System.out.println(1 | 2);
		/ * 1:00 00 0000 0000 0000 0000 0000 0000 0001 at 00 0000 0000 0000 0000 0000 0000 0010 | : 0000 0000 0000 0000 0000 0000 11 */
		
		System.out.println(-3 | 5);
		0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1101, 5:0000 0000 0000 0000 0000 0000 0000 0101 | : 1111 1111 1111 1111 1111 1111 1111 1111 1101 (complement) */
		
		System.out.println(1 ^ 2);
		/ * 1:00 00 0000 0000 0000 0000 0000 0000 0001 00 0000 by 2:00 PM Sunday, 0000, 0000, 0000, 0000, 0000, 0010 ^ : 0000 0000 0000 0000 0000 0000 11 */
		
		System.out.println(-3 ^ 5);
		0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1101 5:0000 0000 0000 0000 0000 0000 0101 ^ : 1111 1111 1111 1111 1111 1000 (p) 1111 1111 1111 1111 1111 1111 1111 0111 (P) 1000 0000 0000 0000 0000 0000 0000 0000 0000 1000 (source code) -8 */
		
		System.out.println(~1);
		/* 1:0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 ~1: 1111 1111 1111 1111 1111 1110 (p) 1111 1111 1111 1111 1111 1111 11101 (P) 1000 0000 0000 0000 0000 0000 0000 0000 0010 (original code) -2 */
		
		System.out.println(1 << 5);
		/* 1:0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
		
		System.out.println(11 << 2); / / 44
		/ * 11:0000 0000 0000 0000 0000 0000 0000 1011 11 < < 2:0000 0000 0000 0000 0000 0000 0010 1100 * /
		
		System.out.println(1 << 35);//8 is equivalent to 1<<(35-32) 1, which should have disappeared, moves back on the right side
		
		System.out.println(1 >> 5);
		/* 1:0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 */
		
		System.out.println(100 >> 5);//3 5 times of 100/2 =100/32 Keep only integer parts and round down
		/* 100: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 */
		
		System.out.println(-1 >> 5);
		/ * 1: the original code: 1000 0000 0000 0000 0000 0000 0000 0001 radix-minus-one complement: 1111 1111 1111 1111 1111 1111 1111 1110 complement: 1111 1111 1111 1111 1111-1 >>5:1111 1111 1111 1111 1111 */
		
		System.out.println(-100 >> 35); // 3 times of -100/2 = -100/8 -13 round down
		/ * - 100: the original code: 1000 0000 0000 0000 0000 0000 0110 0100 radix-minus-one complement: 1111 1111 1111 1111 1111 1111 1001 1011 complement: 1111 1111 1111 1111 1111 1001 1100-100 >>35 is equivalent to -100>>3 111111111 1111 1111 1111 1001 1 (complement) 1111111 1111 1111 1111 1111 1111 1111 10010 1000000 0000 0000 0000 0000 0000 01101 (original code) */
		
		System.out.println(-1 >>> 5);
		/ * 1: the original code: 1000 0000 0000 0000 0000 0000 0000 0001 radix-minus-one complement: 1111 1111 1111 1111 1111 1111 1111 1110 complement: 1111 1111 1111 1111 1111-1 >>>5:0000 01111 1111 1111 1111 1111 111 (134217727) */}}Copy the code

The output

(1)System.out.println(xxx); (2) system.out.print (XXX); (3) system.out.printf (” format symbol “, list of variables); Java basically does not format the output decimal: %f, if you want to keep a few % after the decimal point. Several f

It is best not to use this method if you need to keep a few decimal places, and it will not output to the console during development

Integer: % D String: % S Boolean: % B char: % C

class Test13Out{ public static void main(String[] args){ System.out.println("hello"); System.out.println("world"); System.out.println("-----"); System.out.print("atguigu"); System.out.print("java"); System.out.println("-----"); System.out.print("atguigu\n"); System.out.print("java"); System.out.println("-----"); Double d = 13.45623; System.out.printf("%.2f\n", d); char c = 'a'; System.out.printf("%c" , c); }}Copy the code