Chapter III Java FundamentalsOne, the computer conversion: binary, octal, decimal, hexadecimal mutual transfer
An overview of computer system numbering
- The number of numbers is a fixed set of symbols and a unified plan to represent numerical methods;
- The system used at the bottom of the computer is binary;
- Java programming is practical in decimal. The Java bottom still uses binary;
- There are eight binary and hexadecimal systems commonly used in computers;
The basic number of decimal 0~9, every ten.
10 is called "Radix", 10^n (10 of the Power of N) is called "Power".
The basic number of binary is 0, 1, every 2.
The cardinality of the binary is 2, and the right is 2^n (2 of the n-th square).
Hexadecimal base numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, every 16
Hexadecimal is a shorthand for binary, making it easy for professionals to write binary data.
The base of the 16 binary is 16, the right is 16^n (16 of the n-th square)
In Java code hexadecimal is prefixed with 0X or 0x (0 is a number, not a letter).
Example: 5E hex =5*16^1+14*16^0 = 80+14 = 94;
Binary to hexadecimal rule: the four-bit binary number is equivalent to one hexadecimal digit.
Decimal number Conversion binary: rule: continuously divided by 2, preserving the remainder, when quotient 0 is no longer in addition to 2.
Decimal converts hexadecimal: rule: keep dividing by 16, preserving the remainder, and no more than 16 when quotient is 0.
ii. variables and data types1. Overview
Java defines the basic data types, reference data types, and custom data types;
2. Empty StackRoom
- High efficiency of stack space access data;
- The data in the stack is managed according to the "Advanced and Out" way;
- The storage space of stack space is small, it can't store large amount of data;
- JMV data of the underlying data type is stored in the stack space;
3. Heap Space
- The efficiency of heap space accessing data is lowest;
- The location of data storage is randomly assigned;
- The space of storing data in heap space is large, which can store large capacity data;
iii. definition of variables and reasons for use of variables
Variable: variable amount;
Constant: an immutable amount;
Literal: The specific data stored in the variables and constants of Java becomes the literal amount;
The high-level language of the computer generally uses variables to manage the data stored in memory.
Variables manage data for reference types:
Iv. naming, defining, and initializing variables1, the name of the variable
- The first letter is an English letter, a $ or an underscore, consisting of letters, numbers, and underscores;
- The name of the variable follows the principle of the known meaning.
- Java variable names are not recommended for Chinese.
- The first letter of the variable name is not uppercase.
- Name a variable named after multiple words using the Hump name method. Eg:sumscore
- Variable names do not use the Java keyword.
2, the definition of variables
Define variables: Each variable must belong to one type.
Type variable name 1, variable 2, ..., variable n;
e.g: byte score; //Save single-branch score
Short age; //Save Age
int Sumscore; //Save totals
3. Initialization of variables
Assigned value =
E.g:age = 18;
Score = 60;
Sumscore = 600;
v. Scope of variables
1. Java uses a pair of curly braces as a range of statement blocks, called scopes.
2. Variables in the scope cannot be defined repeatedly.
3. Out of scope, the allocated memory space for the variable will be reclaimed by the JVM.
Six, the basic data type of packaging class
Java provides classes for the underlying data types, which are called wrapper classes, such as
1, packaging class in the encapsulation of some very practical methods and constants. For example: Byte.min_value is a constant in the byte class that holds the minimum value of the byte type data.
2. The wrapper class is used in the collection to customize the type of the collection element.
3. Use packing class
Minimum value of type Integer.MIN_VALUE:int: -2^31
Maximum value of Integer.MAX_VALUE:int type: 2^31-1
int Integer.parseint (String sinteger);
Effect: Converts an integer of type string to data of type int.
String integer.tobinarystring (int value);
Function: Converts the decimal number to binary and returns the result string type.
String integer.tohexstring (int value);
Function: Converts a decimal number to 16 and returns the result string type.
seven or two in complement1. Overview
The internal twos of a computer system stores data in a form.
The decimal data entered in the Java program is automatically converted to binary, and the Java internal is also binary for numeric operations, but the returned result is the binary.
2. The rules of complement
- In a computer system, values are stored in binary complement.
- The highest bits of the binary are the sign bits, 0 for positive numbers, and 1 for negative numbers.
- The positive value is itself, the value of the negative number is the highest bit (sign bit) unchanged, the other bits are reversed, plus 1.
- Two numbers are added, and if the highest bit (sign bit) has a carry, the carry is discarded.
3. Why use complement
- The symbol bit and other bits can be processed uniformly.
- The highest bit no longer represents a number, but is a sign bit, just binary the value, that is, half is 0 to positive, and half is negative. the eg:4 bit binary number has 16 numbers, in the complement expression, then half is 0~7, half is -1~-8. The 8-bit binary number has 256 numbers, in the complement, half is 0~127, and half is -1~-128.
- Subtraction can also be handled by addition.
4. The characteristics of the complement Operation
- The relationship between positive and negative numbers in a computer is reversed plus one.
- The complement operation is closed: The result of the operation is kept within the complement range, and the overrun is overflow.
- The 4-bit twos complement can represent the maximum number of 2^4 (16) and the number range is -8~7.
- The 8-bit twos complement can represent the maximum number of 2^8 (256) and the number range is -128~127.
- The 16-bit twos complement can represent the maximum number of 2^16 (65536) and the number range is -32768~32767.
- The 32-bit twos complement can represent a maximum number of 2^32, and the number range is -2^31~2^31-1.
5, the principle of the complement Operation
positive + negative = modulo
Modulo: The total number of data of a certain type, for example:
The 4-bit binary modulo is 2^4=16
Therefore, a negative number = modulo-positive number, which is the reason for the bitwise negation plus 1.
Eight, integer, floating point, character type1, Integer type
- Java has four types of integers: Byte, short, int, long.
- The result of the Java default integer calculation is the int type.
- The literal of an integer is of type int.
- If the literal exceeds the maximum value of type int, the literal is a long type, then L (or L) is used later to indicate that the value is a long type. Eg:long Longvalue = 300000000L
2. Floating-point type
- Floating-point types are used to represent the data type of decimals.
- Floating point number principle: that is, binary scientific counting method.
- The floating-point type of Java has float and double two kinds.
- The result of the Java default floating-point type calculation is a double type, which is also a double type. To assign a literal value to a variable of type float, add f (or F) after the literal. Eg:float f=5.3; A compile error will occur, the correct assignment is: float f=5.3f;
Floating-point number scientific calculation method:
- Decimal floating point number scientific notation: 219345=2.19345* (10^5)
- Binary floating point number scientific notation: 10111=1.0111* (2^100)
- Float types are 32 bits, 1 bits are sign bit, exponent 8 bits, 32 bits.
- The precision of float is 23 bits (that is, the number that can be accurately represented by 23 bits is truncated). Decimals are expressed in the mantissa length, such as the accuracy of pi=3.1415 is 4 bits.
- Float stores data in a range larger than int, but with less precision than int because the precision of int is 31 bits.
- Double type, 1-bit sign bit, 11-bit exponent, 52-bit mantissa.
- The double range is much larger than long, but the double is less accurate than long (the number of bits that the long stores data is 63 bits).
Characteristics of floating-point arithmetic:
Floating-point arithmetic is real arithmetic, because the computer intelligently stores integers, so the real numbers are approximate, so floating-point arithmetic is very slow and there is error.
3. Character Type
- The literal of a char type can be an English letter, a character, or a Chinese character, and is included by a single quotation mark. Eg: ' A '.
- The Java bottom uses a 16-bit integer to handle the character type, which is the Unicode encoded value of a character.
- Unicode encoding is a worldwide encoding method.
- The English part of Unicode encoding is compatible with ASCII code (ASCII denotes range 0~128), while English characters and numbers are encoded consecutively.
- Java is processed at the bottom level by Unicode code when processing data of type char.
Computer hardware system intelligence with the number of 0 and 1, we artificially made the rule that a number can also represent a character. Eg:65 represents a.
Reference: Geek College Course http://ke.jikexueyuan.com/zhiye/javaweb/
Java Web Learning Note Chapter III Java Foundation (i)