最新java常見的問題 java程序問題大全

字號(hào):

    人的記憶力會(huì)隨著歲月的流逝而衰退,寫作可以彌補(bǔ)記憶的不足,將曾經(jīng)的人生經(jīng)歷和感悟記錄下來,也便于保存一份美好的回憶。大家想知道怎么樣才能寫一篇比較優(yōu)質(zhì)的范文嗎?以下是我為大家搜集的優(yōu)質(zhì)范文,僅供參考,一起來看看吧
    java常見的問題 java程序問題篇一
     在我們寫java程序的過程中,其實(shí)里面有一些細(xì)節(jié)大家可能沒怎么注意,雖然一般沒有什么大問題,但俗話說的好,差之毫厘失之千里。所以我們一定要注意這些小細(xì)節(jié)。那在我們?nèi)粘5木幊讨校心男┪覀儾怀W⒁獾男〖?xì)節(jié)呢?下面跟yjbys小編一起來看看吧!
     錯(cuò)誤的寫法:
     string s = "";
     for (person p : persons) {
     s += ", " + e();
     }
     s = ing(2); //remove first comma
     正確的寫法:
     stringbuilder sb = new stringbuilder(() * 16); // well estimated buffer
     for (person p : persons) {
     if (() > 0) (", ");
     (e);
     }
     錯(cuò)誤的寫法:
     stringbuffer sb = new stringbuffer();
     ("name: ");
     (name + '\n');
     ("!");
     ...
     string s = ng();
     問題在第三行,append char比string性能要好,另外就是初始化stringbuffer沒有指定size,導(dǎo)致中間append時(shí)可能重新調(diào)整內(nèi)部數(shù)組大小。如果是jdk1.5最好用stringbuilder取代stringbuffer,除非有線程安全的要求。還有一種方式就是可以直接連接字符串。缺點(diǎn)就是無法初始化時(shí)指定長(zhǎng)度。
     正確的寫法:
     stringbuilder sb = new stringbuilder(100);
     ("name: ");
     (name);
     ("\n!");
     string s = ng();
     或者這樣寫:
     string s = "name: " + name + "\n!";
     錯(cuò)誤的寫法:
     if (eto("john") == 0) ...
     if (name == "john") ...
     if (("john")) ...
     if ("".equals(name)) ...
     上面的代碼沒有錯(cuò),但是不夠好。compareto不夠簡(jiǎn)潔,==原義是比較兩個(gè)對(duì)象是否一樣。另外比較字符是否為空,最好判斷它的長(zhǎng)度。
     正確的寫法:
     if ("john".equals(name)) ...
     if (() == 0) ...
     if (y()) ...
     錯(cuò)誤的寫法:
     "" + ()
     new integer(()).tostring()
     正確的寫法:
     f(())
     利用不可變對(duì)象(immutable)
     錯(cuò)誤的寫法:
     zero = new integer(0);
     return f("true");
     正確的寫法:
     zero = f(0);
     return ;
     錯(cuò)誤的寫法:
     int start = f("
    ") + "
    ".length();
     int end = f("");
     string name = ing(start, end);
     正確的寫法:
     saxbuilder builder = new saxbuilder(false);
     document doc = doc = (new stringreader(xml));
     string name = telement().getchild("name").gettext();
     錯(cuò)誤的寫法:
     string name = ...
     string attribute = ...
     string xml = "
    "
     +"
    "+ name +"
    "
     +"";
     正確的寫法:
     element root = new element("root");
     ribute("att", attribute);
     t(name);
     document doc = new documet();
     telement(root);
     xmloutputter out = new xmloutputter(ttyformat());
     string xml = string(root);
     錯(cuò)誤的寫法:
     string xml = xtfile("");
     因?yàn)閤ml的編碼在文件中指定的`,而在讀文件的時(shí)候必須指定編碼。另外一個(gè)問題不能一次就將一個(gè)xml文件用string保存,這樣對(duì)內(nèi)存會(huì)造成不必要的浪費(fèi),正確的做法用inputstream來邊讀取邊處理。為了解決編碼的問題, 最好使用xml解析器來處理。
     錯(cuò)誤的寫法:
     reader r = new filereader(file);
     writer w = new filewriter(file);
     reader r = new inputstreamreader(inputstream);
     writer w = new outputstreamwriter(outputstream);
     string s = new string(bytearray); // bytearray is a byte[]
     byte[] a = es();
     這樣的代碼主要不具有跨平臺(tái)可移植性。因?yàn)椴煌钠脚_(tái)可能使用的是不同的默認(rèn)字符編碼。
     正確的寫法:
     reader r = new inputstreamreader(new fileinputstream(file), "iso-8859-1");