First day of Java Foundation

Source: Internet
Author: User
Tags arithmetic arithmetic operators binary to decimal

One: The representation of Java's different binary data

Binary representation in front plus 0b (b can be uppercase or lowercase)

Octal representation in front plus 0

Hexadecimal representation in front plus 0x

Conversion plot of any binary to decimal

decimal-to-decimal conversion

12345 = 10000 + 2000 +300 + 40 + 5

=1 *10^4 + 2 *10^3 + 3 *10^2 +4 *10^1 + 5 *10^0

Principle:

* Coefficient: Is the data on each one.

* Cardinality: X-Binary, which is the X-cardinality.

* Right: On the right, starting from 0, the number on the corresponding bit is the right of the Gai bit.

* Result: Add the power of the coefficient * radix to the Power

Example: Binary Turn decimal 0b100

0b for identity is the binary factor for 1,0,0 cardinality see identity as binary so cardinality is 2 right is 2,1,0

Result: 1* 2^2 + 0* 2^1 + 0* 2^0 = 4

Octal to decimal 0100

0 for the identity rep is the octal factor for the 1,0,0 cardinality look identified as octal so the cardinality is 8 right is 2,1,0

Result: 1* 8^2 + 0* 8^1 + 0* 8^0 = 64

hexadecimal to decimal 0x100

0x for the identity rep is the hexadecimal coefficient for the 1,0,0 cardinality of 16 right is 2,1,0

Result: 1* 16^2 + 0* 16^1 + 0* 16^0 = 256

Decimal conversion to any binary

Principle: In addition to (arbitrary binary) The remainder of the product is inverted

original code complement inverse code

* Original Code

is the binary fixed-point notation, where the highest bit is the sign bit, "0" is positive, "1" represents a negative number, and the rest represents the size of the value

* Anti-code

The inverse code of a positive number is the same as its original code, and the inverse of a negative number is the inverse of its original code, and the sign bit is unchanged.

* Complement

The complement of a positive number is the same as its original code; the complement of a negative number is the minus 1 of its inverse code.

II: Overview and format of variables

A: What is a variable

The *java language is a strongly typed language that defines a distinct data type for each data and allocates a different size of memory space in memory

Classification of data types in B:java

* Basic Data type

* Reference Data type

C: Basic Data Type Classification (4 classes 8 kinds)

* Integer type:

Byte takes one byte-128 to 127

Short accounts for two bytes -2^15 to 2^15-1

int occupies four bytes -2^31 to 2^31-1

Long takes eight bytes -2^63 to 2^63-1

* Floating point type

Float accounted for four bytes -3.403E38 to 3.40ee38 single precision

Double accounts for eight bytes, -1.798E308 to -1.798E308 pairs of precision

* Character type

Char occupies two bytes 0 to 65535

* Boolean type

The Boolean theoretically accounts for one-eighth bytes, because a switch can be determined to be true and false, but the Boolean type in Java does not explicitly specify his size

1 //Datetyoe Data Types2  Public classDatetype {3      Public Static voidMain (string[] args) {4         //integer Type5         byteb = 10;//takes one byte, -128~1276          Shorts = 20;//accounted for two bytes7         inti = 30;//accounted for four bytes8         Longx = 88888888888888L;//accounted for eight bytes if the long type is followed by L, it is better to write l, because the lowercase l is too much like one.9 System.out.println (b);Ten System.out.println (s); One System.out.println (i); A System.out.println (x); -          -          the         //floating-point types -         floatf = 12.3F;//accounted for four bytes -         DoubleD = 33.4;//Eight bytes decimal The default data type is double - System.out.println (f); + System.out.println (d); -          +         //Character Type A         Charc = ' a ';//accounted for two bytes at System.out.println (c); -          -         //Boolean type -          BooleanB1 =true; -          BooleanB2 =false; - System.out.println (B1); in System.out.println (B2); -     } to}
Main things to do with variables:

* Scope Issues

The same region cannot use the same variable life

* Initialization value problem

Local variables must be assigned before they are used

* A statement can define several variables

int a,b,c ...;

Implicit conversions and strong conversions of data types:

Implicit conversion case Columns:

A:int + int

B:byte + int results are accepted with byte and int respectively, see effect

*java has the default translation rules

Cast Case:

A: Cast issue

*int a = 10;

*byte B = 20;

*b = a + b;

B: Cast format

*b = (byte) (A + B);

C:

Pits for adding variables and constants
1  Public classDatetype {2      Public Static voidMain (string[] args) {3     byteB1 = 3;4     byteB2 = 4;5     byteB3 = B1 +B2;6     7 System.out.println (b3);8     /*from two aspects9 1, Byte and byte (or Short,char) when the operation will be promoted to the int type, two int type addition result is int typeTen 2, B1 and B2 are two variables, the value stored in the variable is changed, in the compile time can not judge the specific value, the addition may be more than the value of byte One      */ A     byteB4 = 3 + 4; - System.out.println (b4); -     //3 and 4 are constants,The Java compiler has a constant optimization mechanism, which is to assign the results of 3 and 4 to B4 directly at compile time. the     } -}
The range of long and float values who are big or small

* When mixing operations, Byte,short,char do not convert to each other, automatic type promotion to int type, other types of mixed operation when the small data type promoted to large

*byte,short,char--INT--long--float--double

*long:8 bytes

*float:4 bytes

*ieee754

* 4 bytes is 42 x bits

1 bits is the sign bit

8 bits is an index of

00000000 11111111

0 to 255

1 to 254

126 to 127

23 Bits is the mantissa for

Minus 127 for each index

*a: Their underlying storage structure is different.

The range of data represented by *b:float is larger than the range of long

*long:2*63-1

*float:3.4*10^38 > 2*10^38 >2*8^38 =2*2^3^38=2*2^114>2^63-1

Character and string participation operations
1  Public classDatetype {2      Public Static voidMain (string[] args) {3System.out.println (' a ' + 1);//98, because there is an ASCII code table, the a character corresponds to the int type of the4System.out.println ((Char) (' a ' + 1));//The sum is equal to 98, and 98 corresponds to the B in the ASCII code table.5     6System.out.println ("Hello" + ' a ' + 1);//any data type used? Connecting to a string will result in a new string7System.out.println (' A ' +1+ "Hello");8     }9}
char character type

A:char C = 97; 0 to 65535

B:java character Char in a language can store a Chinese character? Why?

OK. Because the Java language uses Unicode encoding. Each character in the Unicode encoding occupies two bytes. Chinese also occupies two bytes

So the characters in Java can store a Chinese character

Basic usage of arithmetic operators

A: What is an operator

* is the symbol that operates on constants and variables.

B: Classification of operators

* Arithmetic operator, assignment operator, comparison (relationship or condition) operator, logical operator, bitwise operator, trinocular (meta) operator

C: Classification of arithmetic operators

* +,-,/,%,++,--

D: Precautions

The a:+ number has three functions in Java, representing the plus sign, the addition operation, the string connector

B: Dividing integers can only get integers. If you want a decimal, you must change the data to a floating-point number type

c:/gets the quotient of the division operation,% gets the remainder of the division operation

% operator

When the left absolute is less than the right absolute value, the result is left

When the absolute value on the left is greater than the absolute right, the result is the remainder

When the absolute value on the left equals the right absolute value, the result is 0.

The symbol for the result of the% operator is only related to the left, not to the right

Any positive integer%2 result either 0 or 1 can be used as a toggle condition

The use of arithmetic operators + + and--

A: Use alone:

* Put in front of the operand and follow the same effect

Example: int a = 3; a++ equals A = a + 1, ++a also equals a = a + 1

B: Participate in the operation using:

* Put in front of the operand, increment or decrement first, and then participate in the operation.

* After the operand, first participate in the operation, and then increment or subtract.

Basic usage of assignment operators

A: What are the assignment operators

* Basic assignment operator: =

The data on the right is assigned to the left.

B: Extended assignment operator AH: +=,-=,*=,/=,%=

+ = Add to the left and right, any assignment to the left

Basic usage of relational operators and their considerations

A: What are the relational operators (comparison operators, conditional operators)

*==,!=,>,>=,<,<=

Precautions

* Whether your operation is simple or assignment, the result is a Boolean type

* "=" cannot be written as "="

First day of Java Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.