Java Study Notes (2), java Study Notes

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

Java Study Notes (2), java Study Notes
Please note the shortcomingsI,Basic knowledge1, SDKLatest download

Search for oracle and go to the website. Select "Downloads"> "JavaSEI" to select the first download (in factJava Differences between 8u111 and 8u112In8u111On the basis of the optimization, upgraded to a lower level, there is no difference in actual use)

 

 

Next, make sure you select 32-bit or 64-bit, Linux or Windows. X86 indicates 32-bit.

 

Finally, select the first button in the figure to agree. If you do not select the button, the default button is the second one.

 

 

The latest version is displayed. If you want to download the old version, you can find it by searching.

 

2 , PATH Of Purpose

Java environment variables include path and classpath environment variables. During compilation, we cannot run JDK (Java development kit) and the java program you have compiled in notepad at the same time.SetThe actual environment variable of path isYesJDK installation pathIn this way, you can directly enter the program path you program when running, instead of entering the JDK path.

  3. Notes for beginners:

1. Forget to save the program after writing it in Notepad

2. After saving the code, enter the correct path when running cmd.

3. Compile the bytecode file: Enter the javac file name and press Enter.

4. Run the code: java class name. Note that this is not a file name.

5. Fixed statement writing errors, especially when writing an error to the main function, do not report an error during running. Because that is still a function, it is just a common function, so be sure to pay attention to it.

The Java source file extension is. java, and the bytecode file extension is. class.

For beginners,It is the best way to learn from self-perception by practicing, asking, debugging, reviewing, and summarizing yourself.

 

4. keywords and identifiers

1. Keywords in Java are used for some special purposes,Keywords in Java are case sensitive.

2. identifiers are the naming symbols used to define variables, classes, and methods in a program.

 

5. Variables

<1. Name: The first letter of the first word is in upper case, and the other letters are in lower case. The variable name must be known.

<2. Use:

YesDeclare one or more variables first, and then assign values.;

 AlsoYou can assign values directly when declaring a variable.

For example:

int a1,a2,a3,a4;a1 = 10;int a= 10;char c = 'a';short s= 10

Note: You must declare it before using it, and assign values before outputting it.

 

6. Java Data Types

<1,Basic Data Type:Common types include int, char, boolean, and byte.

<2. Reference Data Type:String, class, array, etc., store the spatial address of the data

<3. Notes for data type conversion :

All variables defined by short, byte, and char are automatically converted to int type during calculation.

Two different data types are computed, depending on the big data type.

 

<4. Automatic type conversion

The target type and source file type must be compatible.For example, the int type is not compatible with the char and string types.

For example:

Short s = 100; int I = s; // automatic type conversion int num1 = 10; float num2 = 10.0f; // The default value is double, and float is followed by f

Small Data to Big Data: The target type must be greater than the source type. For example, the int type can be converted to the double type, but in turn, an error occurs.

 

<5. Forced conversion type

Convert big data into small data:As mentioned above, automatic type conversion cannot be about converting the int type to the double type. It can be forced conversion, but it is recommended to use it less, because it may cause data loss.

 

Long l = 100000L; // The long integer must be followed by a Lint a = (int) l; // The forced type conversion is also performed by using a small value before the variable

 

 

6. hexadecimal Conversion

IntegerRepresentation of constantsÀHexadecimalRepresentation(Binary, octal, decimal, and hexadecimal)

 

1. hexadecimal Understanding

In life: there are other hexadecimal formats, such as Week (7hexadecimal 0-6), time (12 (0-11), and twenty-four hexadecimal (0-23)

ComputerRead using binaryFetchData in lifeA computer can only recognize 0 and 1, just like a switch.

 

2. hexadecimal Conversion :

Convert decimal to binary: divide the number into 2 and take the remainder until the quotient is 0. Connect the remainder from the bottom to the binary number.

 

Convert binary to decimal: multiply each bit of binary by 2 ^ (n-1) from left to right, and then add, that is, decimal.

 

The conversion principle of octal, decimal, decimal, and hexadecimal is the same.

 

Convert binary to octal:

Train of Thought 1: binary, decimal, and octal

Thought 2:OneOctal is equivalent to three binary values., Split binary three into one

010 100 101 = 245

The second approach is relatively simple, that is, the 8421 code, which is the most common BCD encoding. It is also the same principle to replace binary with hexadecimal:

OneThe hexadecimal format is equivalent to four binary values.To split binary into one bit.

0000 1010 0101 = a5

Hexadecimal representation: 1-9, 10-15 expressed in A-F

 

 

II. Operator. 1.1 Arithmetic Operators: +,-, *,/, %

 

Short s1 = 10; short s2 = 20; short s3 = s1 + s2; // an error is reported. When s1 and s2 are used for computation, they are automatically converted to the int type. // the correct method is used: int = s1 + s2; short s3 = (short) (s1 + s2); // common error:
  1.2 auto-increment and auto-increment: ++ ,--
Int a = 10; int B = a ++; B = 10 a = 11 // ++ after the value is assigned, int b1 = ++; b1 = 12 a = 12 // ++ values are calculated before the assignment, and the subtraction is the same.

 

1.3 value assignment operator: =, + =,-=, * =,/=

Common Errors:

Int a = 0; a + = 10; // a = a + 10; short B = 10; short c = 5; B + = c; // B = B + c // No problem. if it is converted to int, it is automatically converted to short // B = B + c; // The following method is incorrect.

 

 

1.4 Relational operators: >,<,>=, <=, == ,! =

Relational operators (comparison operators): Only one type of result is boolean, true or false)

 

If you determine the basic data type, compare the two values of the variable.

If it is a comparative reference type, compare the referenced address value

1.5 logical operators: &, | ,! , &, |  

& Differences and contacts:

Similarities: the results are the same

Difference: If the double & number is used for determination, if the condition is set to false, no results will be judged.

Part 2, but the ticket & number will continue to be judged

 

| And |

Similarities: the results are the same.

Difference: if | is used to judge, if the condition is true, no results will be judged.

Part 2, but the ticket | number will continue to be judged

 

 

1.6-bit operator:

It computes binary bits.

Bitwise operators can be used to calculate decimal integers. The calculation method is as follows:Convert decimal to binary for Calculation.

 

Binary is composed of 0 and 1, so the calculation result is either 0 or 1.

1-bit operator symbol: <1, & (And ):   When both are 1, I am 1, and the others are 0.

 

 

 

Int a = 6; int B = 3; System. out. println (a & B); // 2
/* Principle: 1111-1111 1111-1111 1111-1111 1111-0110 6 & 1111-1111 1111-1111 1111-1111 1111-0011 21111-1111 1111-1111 1111-1111 1111-0010 = 2 */

 

 

<2, | (OR ): When both are 0, they are 0, and the others are 1.

 

int a = 6;int b = 3;System.out.println(a|b);   // 7/*1111-1111 1111-1111 1111-1111 1111-0110  6|1111-1111 1111-1111 1111-1111 1111-0011  21111-1111 1111-1111 1111-1111 1111-0111  =7*/

 

<3, ^ (Exclusive or ):   The difference is 1, the same is 0

 

int a = 6;int b = 3;System.out.println(a^b);   // 5/*1111-1111 1111-1111 1111-1111 1111-0110  6^1111-1111 1111-1111 1111-1111 1111-0011  21111-1111 1111-1111 1111-1111 1111-0101  =5*/

 

 

 

<4 ,~ (Reverse) 0 is changed to 0.

 

int a = 6;int b = 3;System.out.println(~a);   // -7/*~1111-1111 1111-1111 1111-1111 1111-0110  61111-1111 1111-1111 1111-1111 1111-1001  =-7*/

 


6. If the result is "-7" and "-7", the result is also "6 ".
: The positive number is reversed to a negative number and then-1, and the negative number is reversed to a positive number.

 

 

 Negative expression: If the highest bit of the binary is 1, this number is a negative number..

 

/*1111-1111  1111-1111 1111-1111 1111-1111 : -10000 0000 0000 0000 0000 0000 0000 0000 : 0-2:1111-1111  1111-1111 1111-1111 1111-1110-3:1111-1111  1111-1111 1111-1111 1111-1101-4:1111-1111  1111-1111 1111-1111 1111-1100-5:1111-1111  1111-1111 1111-1111 1111-1011-6:1111-1111  1111-1111 1111-1111 1111-1010-7:1111-1111  1111-1111 1111-1111 1111-10010000-0000  0000-0000 0000-0000 0000-0111  = 7*/

 

Rule:Convert positive number to negative number-1

Negative number is reversed to positive number-1

 

Function: Data Encryption

Such as 123456 can 0-100 and a-z26 letters, plus &, |, ^ ,~ Application of equality Operators

For example: 24845845957512317580960 ---> 123456

 

1 . 7 shift operator: operate binary bits

1> right shift

2 <move left

3 >>> unsigned right shift

Right Shift: System. out. println (6> 1); // ---> 3 6/2 2*1 System. out. println (6> 2); // ---> 1 6/4 2*2 System. out. println (6> 3); // ---> 0 6/8 2*3 System. out. println (6> 4); System. out. println (9> 1); // ---> 4 9/2 2*1 System. out. println (9> 2); // ---> 2 9/4 2*2 System. out. println (9> 3); // ---> 1 9/8 2*3 System. out. println (9> 4); // ---> 0 9/16 System. out. println (-6> 1); //-6/2 -->-3Rule:The number of digits to be moved to the right is divided by the number of digits to the power of 2..

 

/*Shift left: System. out. println ("----- This is the result of the Left shift ----------"); System. out. println (6 <1); // ---> 12 6*2 2*1 System. out. println (6 <2); // ---> 24 6*4 2*2 System. out. println (6 <3); // ---> 48 6*8 2*2*2 System. out. println (6 <4); // ----> 96 6*16 2*2*2*2 System. out. println (-6 <2); // --->-24 rule: multiply the number of digits shifted to the left by the power of 2.*/

 

/*// >>> Unsigned right shift System. out. println (6 >>> 1); // 3 System. out. println (6 >>> 2); // 1System. out. println (-6 >>> 2); // 1073741822 principle: the unsigned right shift of a positive number is the same as that of a normal right shift.*/

 

 

Purpose:Increase computing speed. Bit operations are the fastest to execute.

For example, calculate 2*8 in the fastest way

2 <3 = 2*8 = 16

 

 

2.8. Ternary Operators

It is also called the three-object OPERATOR:

Structure:

Condition? Result of conditional execution: The execution result is not true;

 

Class Demo1 {public static void main (String [] args) {int a = 10; int B = 9; int c = B ++> =? A ++: B + a; // note that B ++> = a first executes B> = a, and then executes B ++, because ++ is assigned a value before the operation, it is often wrong here. Then don't forget B ++, and then execute B + a, so the c value is 20, instead of 11/19 System. out. println (a); // 10 System. out. println (B); // 10 System. out. println (c); // 20

 

 

// Requirement: three methods for exchanging two values in Java: // 1. int x = 10; int y = 20; int temp = x; x = y; y = temp; // 2. add or subtract int x = 10; int y = 20; x = x + y; y = x-y; x = x-y; // 3. displacement Calculation interchange x = x ^ y; y = x ^ y; x = x ^ y; // System. out. println (y ^ x); // System. out. println (x ^ y );

 

Related Article

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.