java 十六进制转十进制_JAVA知识-分析JAVA中的重点和难点
Java中有很多內(nèi)容在開(kāi)發(fā)項(xiàng)目的過(guò)程中并不常用,但是卻是很重要的部分,為了避免忘記,今天重新溫習(xí)了一遍Java中的重點(diǎn)和難點(diǎn),借此機(jī)會(huì)記錄一下方便以后查找。
本文主要分為以下幾個(gè)部分:
1.進(jìn)制的相互轉(zhuǎn)換
2.Java中位運(yùn)算符的解釋和運(yùn)用
3.Java數(shù)組中常用的排序算法分析
4.Java中折半查找方法的分析和運(yùn)用
5.Java中對(duì)象的初始化過(guò)程
6.Java抽象類在模板方法模式中的運(yùn)用
7.Java多線程的難點(diǎn)和設(shè)計(jì)多線程程序時(shí)需要注意的問(wèn)題
8.Java中集合框架運(yùn)用時(shí)需要注意的問(wèn)題
9.Java中IO難點(diǎn)和重點(diǎn)的分析
10.Java網(wǎng)絡(luò)編程的分析和運(yùn)用
11.Java中常用正則表達(dá)式的運(yùn)用和技巧
第一部分:進(jìn)制的相互轉(zhuǎn)換
1.十進(jìn)制和二進(jìn)制的相互轉(zhuǎn)換
十進(jìn)制轉(zhuǎn)二進(jìn)制:
int num = 102; StringBuffer sb = new StringBuffer(); while(num >= 1) {sb.append(num%2);num = (int) (num / 2); } System.out.println(sb.reverse());二進(jìn)制轉(zhuǎn)十進(jìn)制:
String binaryStr = "1100110"; int num = 0; for (int i = 0; i < binaryStr.length(); i++){num += Integer.parseInt(String.valueOf(binaryStr.charAt(binaryStr.length() - 1 - i))) << i; } System.out.println(num);第二部分:Java中位運(yùn)算符的解釋和運(yùn)用
Java中一共有7個(gè)位運(yùn)算符分別是<<、>>、&、|、^、~、>>>
1.“<<”--左移運(yùn)算符,參與左移運(yùn)算的數(shù)字乘以2的左移位數(shù)次方,例如3<<2=3*22
2.“>>”--右移運(yùn)算符,參與右移運(yùn)算的數(shù)字除以2的右移位數(shù)次方,例如3>>2=3/22
3.“&” --與運(yùn)算符,參與與運(yùn)算的兩個(gè)數(shù)字的二進(jìn)制等位都為1時(shí)結(jié)果值的該位為1,其余情況為0,例如3&2=0011&0010=0010,與運(yùn)算符和“>>>”運(yùn)算符結(jié)合可以實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)十六進(jìn)制的功能,num&15 -> num>>>4,這樣一組運(yùn)算就能得到一個(gè)十六進(jìn)制位,再將超過(guò)10的通過(guò)(num-10)+'A'轉(zhuǎn)換為十六進(jìn)制位
4.“|” --或運(yùn)算符,參與或運(yùn)算的兩個(gè)數(shù)字的二進(jìn)制等位至少有一個(gè)為1時(shí)結(jié)果值的該位為1,其余情況為0,例如3|2=0011|0010=0011
5.“^” --異或運(yùn)算符,參與異或運(yùn)算的兩個(gè)數(shù)字的二進(jìn)制等位如果不相同則為1,相同則為0,一個(gè)數(shù)字異或同一個(gè)數(shù)字兩次則等于原數(shù)字。其中一個(gè)應(yīng)用是在不使用第三個(gè)變量的情況下交換兩個(gè)×××變量的值。
int n = 4,m = 6; //此時(shí)n = n ^ m的值 n = n ^ m; //此時(shí)m = n ^ m,因?yàn)樯暇浯a執(zhí)行后n = n ^ m,所以這里m = n ^ m = n ^ m ^ m = n(這里的m = n中的n = 4) m = n ^ m;//此時(shí)m = n,n = n ^ m , 所以n = n ^ m = n ^ m ^ n=m(這里的n = m中的m = 6) n = n ^ m;6.“~” --取反運(yùn)算符,參與取反運(yùn)算的數(shù)字的所有二進(jìn)制位都取相反的值,0變成1,1變成0,因?yàn)橐粋€(gè)正數(shù)的負(fù)數(shù)或者一個(gè)負(fù)數(shù)的正數(shù)等于它取反然后加1,所以一個(gè)數(shù)取反則等于該數(shù)乘以-1然后減去1
7.“>>>” --無(wú)符號(hào)右移,高位補(bǔ)零,功能和右移類似
第三部分:Java數(shù)組中常用的排序算法
1.選擇排序
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.冒泡排序
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 + " "); }第四部分:Java中折半查找方法的分析和運(yùn)用
折半查找的原理是先將數(shù)組排序(從小到大,如果是從大到小則需要一些改變),然后找到數(shù)組中的中間數(shù),然后把中間數(shù)和需要查找的數(shù)進(jìn)行比較,如果需要查找的數(shù)小于中間數(shù)則將最大索引賦值為中間結(jié)果索引+1,反之則把最小索引賦值為中間結(jié)果-1。代碼如下:
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) {System.out.println(attr[mid]); }第五部分:Java中對(duì)象的初始化過(guò)程
第六部分:Java抽象類在模板方法模式中的運(yùn)用
這里舉一個(gè)簡(jiǎn)單的示例代碼來(lái)說(shuō)明,代碼如下:
//首先聲明一個(gè)抽象類,這個(gè)抽象類的作用是計(jì)算一段代碼的執(zhí)行時(shí)間 public abstract class GetTime {public final void getDoWorkTime() {int start = System.currentTimeMillis();doWork();int end = System.currentTimeMillis();System.out.println("工作時(shí)間:" + (start - end));}public abstract void doWork(); }//聲明一個(gè)GetTime類的子類,并實(shí)現(xiàn)doWork方法 public class SubGetTime extends GetTime {@Overridepublic void doWork() {System.out.println("做一些工作");} }public class Test {public static void main(String[] args) {SubGetTime getTime = new SubGetTime();getTime.getDoWorkTime();} }//這里的doWork方法聲明為抽象方法,然后交給子類去實(shí)現(xiàn)需要做的工作,這種方式就是模板方法模式,這是設(shè)計(jì)模式中行為模式中的一種第七部分:Java多線程的難點(diǎn)和設(shè)計(jì)多線程程序時(shí)需要注意的問(wèn)題
Java多線程中的難點(diǎn)和重點(diǎn)主要是線程安全的問(wèn)題,這里就主要說(shuō)一下線程安全的問(wèn)題,因?yàn)樵贘DK1.5后Java引入了Lock和Condition來(lái)代替synchronized、wait和notify,所以這里分兩種情況來(lái)討論。
首先在Java中創(chuàng)建線程的方式有兩種,第一種是繼承Thread類然后復(fù)寫(xiě)run方法,第二種方式是實(shí)現(xiàn)Runable接口并實(shí)現(xiàn)run方法。
繼承Thread:
public class SaveMoney extends Thread {@Overridepublic void run() {System.out.println("存錢(qián)");} } public class GetMoney extends Thread {@Overridepublic void run() {System.out.println("取錢(qián)");} } public class BankTest {public static void main(String[] args) {SaveMoney saveMoneyThread = new SaveMoney();GetMoney getMoneyThread = new GetMoney();saveMoneyThread.start();//啟動(dòng)線程,這里如果調(diào)用run是執(zhí)行run方法不是啟動(dòng)線程,需要注意getMoneyThread.start();} }實(shí)現(xiàn)Runable接口:
public class SaveMoney implements Runable {public void run() {System.out.println("存錢(qián)");} } public class GetMoney implements Runable {public void run() {System.out.println("取錢(qián)");} } public class BankTest {public static void main(String[] args) {new Thread(new SaveMoney()).start();//啟動(dòng)線程,這里如果調(diào)用run是執(zhí)行run方法不是啟動(dòng)線程,需要注意new Thread(new GetMoney()).start();} }下面就在JDK1.5之前和JDK1.5之后兩種情況下結(jié)合銀行取錢(qián)和存錢(qián)的例子來(lái)說(shuō)明線程同步的問(wèn)題
JDK1.5之前:
//首先創(chuàng)建一個(gè)用戶帳戶類 public class BankAccont {private String accontName = "";private Double totalMoney = 0d;public BankAccont(String accontName, Double initMoney) {this.accontName = accontName;this.totalMoney = initMoney;}public void saveMoney(Double money) {this.totalMoney += money;System.out.println("存了" + money + "塊錢(qián)");}public void getMoney(Double money) {this.totalMoney -= money;System.out.println("取了" + money + "塊錢(qián)");}public String toString() {System.out.println(this.accontName + "總共還有" + this.totalMoney + "元人民幣");} }//分別創(chuàng)建存錢(qián)和取錢(qián)的線程,使用實(shí)現(xiàn)Runable接口的方式這種方式可以輕松的讓不同的線程執(zhí)行相同的任務(wù),除非程序員打算修改或增強(qiáng)類的基本行為,否則不應(yīng)為該類(Thread)創(chuàng)建子類 public class SavaMoney implements Runable {private BankAccont accont = null;public SaveMoney(BankAccont accont) {this.accont = accont;}public void run() {//這里使用同一個(gè)鎖進(jìn)行同步synchronized(BankAccont.class) {while(true) {this.accont.saveMoney(100);}}} } public class GetMoney implements Runable {private BankAccont accont = null;public GetMoney(BankAccont accont) {this.accont = accont;}public void run() {//這里使用同一個(gè)鎖進(jìn)行同步synchronized(BankAccont.class) {while(true) {this.accont.getMoney(100);}}} } public class BankTest {public static void main(String[] args) {BankAccont accont = new BankAccont("張三", 1000);new Thread(new SaveMoney(accont)).start();new Thread(new GetMoney(accont)).start();accont.toString();} }JDK1.5之后:
//首先創(chuàng)建一個(gè)用戶帳戶類 public class BankAccont {private String accontName = "";private Double totalMoney = 0d;private final Lock lock = new RentrantLock();private final Condition condition_save = lock.newCondition();private final Condition condition_get = lock.newCondition();public BankAccont(String accontName, Double initMoney) {this.accontName = accontName;this.totalMoney = initMoney;}public void saveMoney(Double money) {lock.lock();condition_get.await();//這里引用不合適,只是一個(gè)示例this.totalMoney += money;System.out.println("存了" + money + "塊錢(qián)");condition_get.signal();//這里引用不合適,只是一個(gè)示例lock.unlock();}public void getMoney(Double money) {lock.lock();condition_save.await();//這里引用不合適,只是一個(gè)示例this.totalMoney -= money;System.out.println("取了" + money + "塊錢(qián)");condition_save.signal();//這里引用不合適,只是一個(gè)示例lock.unlock();}public String toString() {System.out.println(this.accontName + "總共還有" + this.totalMoney + "元人民幣");} }//分別創(chuàng)建存錢(qián)和取錢(qián)的線程,使用實(shí)現(xiàn)Runable接口的方式這種方式可以輕松的讓不同的線程執(zhí)行相同的任務(wù),除非程序員打算修改或增強(qiáng)類的基本行為,否則不應(yīng)為該類(Thread)創(chuàng)建子類 public class SavaMoney implements Runable {private BankAccont accont = null;public SaveMoney(BankAccont accont) {this.accont = accont;}public void run() {while(true) {this.accont.saveMoney(100);}} } public class GetMoney implements Runable {private BankAccont accont = null;public GetMoney(BankAccont accont) {this.accont = accont;}public void run() {while(true) {this.accont.getMoney(100);}} } public class BankTest {public static void main(String[] args) {BankAccont accont = new BankAccont("張三", 1000);new Thread(new SaveMoney(accont)).start();new Thread(new GetMoney(accont)).start();accont.toString();} }以上只是一個(gè)簡(jiǎn)單的示例,需要根據(jù)需要進(jìn)行修改。在設(shè)計(jì)多線程程序的時(shí)候需要多考慮線程同步的問(wèn)題(線程安全),在多線程中還有一個(gè)問(wèn)題就是Java中有哪些線程安全的集合?
Java中線程安全的集合分別是Vector(向量,已經(jīng)不常用了)、HashTable、Enumeration(枚舉),除了這幾個(gè)其余都為線程不安全集合。StringBuffer和StringBuider的差別也是StringBuffer為線程安全,StringBuider為線程不安全。
Java多線程相關(guān)的問(wèn)題,暫時(shí)只想到這些,再想起的時(shí)候再補(bǔ)充。
?著作權(quán)歸作者980254744所有:來(lái)自51CTO博客作者980254744的原創(chuàng)作品。
該篇文章我從別的地方看到的感覺(jué)有點(diǎn)幫助,所以我分享一下給大家看看。如果覺(jué)得好的話麻煩大家評(píng)論轉(zhuǎn)發(fā)一下。資料
下面是我自己積累的一些JAVA資料如果需要的話回復(fù)(資料)二字即可免費(fèi)獲取,或者點(diǎn)擊我下面鏈接獲取
石墨文檔?shimo.im總結(jié)
以上是生活随笔為你收集整理的java 十六进制转十进制_JAVA知识-分析JAVA中的重点和难点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 牙齿做根管治疗多少钱啊?
- 下一篇: 北京环球影城可以自带食物吗
