The emphases and difficulties of Java-java in Java

Source: Internet
Author: User

Java has a lot of content in the development of the project is not commonly used, but it is a very important part, in order to avoid forgetting, today re-review the focus and difficulties in Java, take this opportunity to record a convenient to find later.

This article is mainly divided into the following parts:

1. Conversion of the binary to the other

Interpretation and application of 2.Java median operator

Analysis of sorting algorithms used in 3.Java arrays

Analysis and application of binary finding method in 4.Java

Initialization of objects in 5.Java

The application of 6.Java abstract class in Template method model

7.Java Multi-Threading difficulties and issues to be aware of when designing multithreaded threads

Problems needing attention in the application of assemble frame in 8.Java

Analysis of IO difficulties and emphases in 9.Java

Analysis and application of 10.Java network programming

Application and skill of regular expressions used in 11.Java


Part I: Conversion of the binary to the other

1. Conversion between decimal and binary binary

Decimal Turn binary:

int num = 102;    StringBuffer sb = new StringBuffer (), while (num >= 1) {sb.append (num%2);  num = (int) (NUM/2); }system.out.println (Sb.reverse ());

Binary goto decimal:

String binarystr = "1100110"; int num = 0;for (int i = 0; i < binarystr.length (); i++) {num + = Integer.parseint (strin G.valueof (Binarystr.charat (Binarystr.length ()-1-i)) * MATH.POW (2, i);} SYSTEM.OUT.PRINTLN (num);


Part II: Interpretation and application of the Java bitwise operators

A total of 7 bitwise operators in Java are <<, >>, &, |, ^, ~, >>>


1. "<<"--left shift operator, number of left-shift operations multiplied by 2 left shift, for example 3<<2=3*22


2. ">>"--right shift operator, number of right shift numbers divided by 2 right shift number of times, e.g. 3>>2=3/22


3. "&"--with the operator, the binary equal bits of the two digits that participate in the operation are 1 o'clock the result value of the bit is 1, the remainder is 0, for example 3&2=0011&0010=0010, with the operator and the ">>>" The operator can be combined with the ability to implement decimal to hexadecimal, num&15-num>>>4, so that a set of operations can get a hexadecimal bit, and then more than 10 of the pass (num-10) + ' A ' to the hexadecimal bit


4. "|"--OR operator, the binary equal bits of the two digits of the participating or operation have at least one 1 o'clock result value for that bit 1, and the remainder is 0, for example 3|2=0011|0010=0011


5. "^"--XOR operator, binary equal bits of two digits that participate in an XOR operation are 1, the same is 0, a number XOR or the same number two times equals the original number. One of these applications is to exchange the values of two shaping variables without using a third variable.

int n = 4,m = 6;//at this time n = n ^ M's value n = n ^ m;  At this time m = n ^ m, since the last line of code executes after n = n ^ m, so here m = n ^ m = n ^ m ^ m = n (m = n in here = 4) m = n ^ m;//at this time m = n,n = n ^ m, so n = n ^ m = n ^ m ^ n=m (here n = m in m = 6) n = n ^ m;


6. "~"--Take the inverse operator, all the bits that participate in the inverse operation are given the opposite value, 0 becomes 1, 1 becomes 0, because a positive negative number or a positive value of a negative is equal to it and then add 1, so one counter is equal to that number multiplied by-1 then minus 1.


7. ">>>"--Unsigned right shift, high fill 0, function and right shift similar


Part III: Sorting algorithms commonly used in Java arrays

1. Select sort

Int[] attr = {3,6,5,85,2,44,1,46,67,0,45,4,134,123,112};for (int x = 0;x < Attr.length ()-1, x + +) {for (int y = x + 1; Y < Attr.length ();            y++) {if (Attr[x]<attr[y]) {attr[x] = attr[x] ^ attr[y];            Attr[y] = attr[x] ^ attr[y];        ATTR[X] = attr[x] ^ attr[y]; }}}for (int i in attr) {System.out.print (i + "");}

2. Bubble sort

int[] attr = {3,6,5,85,2,44,1,46,67,0,45,4,134,123,112}; For (Int x = attr.length ()  - 1;x >= 0; x--)  {     for (int y = 0; y < x;y++)  {         if (attr[y] < attr[y + 1])  {             attr[y] = attr[y] ^ attr[y + 1];             attr[x] = attr[y] ^ attr[y +  1];            attr[y] = attr[y]  ^ attr[y + 1];        }    }} for (int i in attr)  {    system.out.print (i +  " ");} 


Part IV: Analysis and application of binary search method in Java

The principle of binary lookup is to sort the array first (from small to large, if it is from large to small to need some change), then find the middle number in the array, and then compare the median to the number that needs to be looked up, if the number that needs to be found is less than the middle number, assign the maximum index to the intermediate result index + 1, otherwise the minimum index is assigned to the intermediate result-1. The code is as follows:

Int[] attr = {3,6,5,85,2,44,1,46,67,0,45,4,134,123,112};int min = 0;int max  = attr.length ();int mid =  (int)   (Min + max)  / 2; Arrays.sort (attr); Int key = 67;int keyindex = -1;while (Min <= max)  {        if (Key < attr[mid])  {         max = mid + 1;    } else  if (Key > attr[mid])  {        min =  mid - 1;    } else {         keyindex = mid;        break;    }         mid =  (int)   (Min + max)  / 2;} if (keyindex != -1) &NBSP;{&NBSp;   system.out.println (Attr[mid]);} 



Part V: Initialization of objects in Java

    1. Initializing static blocks of code

    2. Initialize Property default value

    3. Initialize Property display Setting value

    4. Initialize building blocks of code

    5. Initializing constructors


Part VI: The application of Java abstract class in template method mode

Here is a simple example code to illustrate, the code is as follows:

First, an abstract class is declared, and the function of this abstract class is to calculate the execution time of a piece of code public abstract class gettime {     Public final void getdoworktime ()  {        int  start = system.currenttimemillis ();         dowork ();         int end = system.currenttimemillis ();                 system.out.println (" Working hours: " +  (start - end));    }         public abstract void dowork ();} Declares a subclass of the GetTime class and implements the DoWork method public class subgettime extends gettime {      @Override     public void dowork ()  {         system.out.println ("Do some work"); &NBSP;&NBSP;&NBSP;&NBSP;}}PUBLIC&NBSp;class test {    public static void main (String[] args)  {        subgettime gettime = new subgettime ();         gettime.getdoworktime ();     }}// Here the DoWork method is declared as an abstract method, and then handed to the subclass to do the work that needs to be done, this way is the template method pattern, which is a pattern of behavior in the design pattern


This article is from the "Technology Not Home" blog, please be sure to keep this source http://chrischen.blog.51cto.com/9954795/1743505

The emphases and difficulties of Java-java in Java

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.