2011年9月11日 星期日

[Java] Static variable

電腦的記憶體在執行程式時, 會被分成三區: permanent, stack 和 heap。
  • permanent: 變數會一直活到程式執行完。
  • stack:
    • 變數則是在{}執行完就被移除。
    • ex: 用來存放function return address和function的local variables(包括參數)及保存暫存器值的記憶體空間。
    • ex: local variable既然在{}外的程式都看不到他, 很自然的選則就是在離開{}後就把他清掉, 好節省記憶體。
  • heap: 程式中動態配置記憶體所用的記憶體空間。


BSS(block storage segment): 用來存放程式中未初始化的全域變數的空間。
Data Segment: 用來存放已初始化的全域變數。
Text Segment: 用來存放程式碼(固定不變的部分)。

Data Segment and Text Segment 兩塊記憶體大小在程式執行前就固定了。

Static Variable
  • 在執行程式其間,此變數是共享的,所以可以節省系統空間與共享資源。
  • 生命周期較長,而且不易被系统回收,因此如果不能合理地使用Static Variable,就會是反效果,造成大量的空間浪費。
  • 因此,建議在符合以下所有條件時,才考慮使用Static Variable:
    • 會占用較多的空間。
    • 生命週期較長。
    • 所包含的數據穩定。
    • 有共享需求。



* Reference
- 程式記憶體位址的配置 - 程式專欄 - Yahoo!奇摩部落格
- java static静态变量 - - ITeye技术网站

2011年9月10日 星期六

[Javascript] note

* javascript no overloading


* js action in href

<a href="javascript:function()">....


[JPA] note

* java -> JPA -> hibernate

[Metamodel]
* 用處: select 時讓 JPA 知道attribute type 否則會將 attribute 視為string,ex:
  • 有 metamodel
    • root.get(EntityMetamodel_.attribute)
  • 沒有 metamodel
    • 若沒有寫在metamodel,則可在 root.get 地方寫成 root.<String>get("attribute")
    • 如果 type 是 string 可不轉,不過建議還是轉,以知道其type。

* Must
- 若 join table 時使用到 JoinPath 則一定要有 metamodel。


[Annotation]
* Transient : 不在table field的 attribute


[Java] Java doc

{@link Object#attribute}: Object.attribute

2011年9月5日 星期一

[Lifecycle] Activity Lifecycle


應用程式中所使用的 activity 會被堆疊在 stack 中 (task?)。

畫面上的呈現與 activity 狀態對應如下:
  • running <-> visible and get focus
    • 正在運行的 activity 便是 stack 最上面的那個 activity,會顯示在畫面上的。
  • onPause <-> visible but lost focus
    • 在畫面上仍是可見的,但已失去焦點。
    • ex: 最上的 activity 畫面獲得焦點且是可看見背景的,那麼原本的 activity ,即使是 onPause 也仍存活著,保持自己的狀態與訊息,仍然與 window manager 保持連接,但有可能會被系統殺掉。
  • onStop <-> non-visible and lost focus
    • 在畫面上已被其他 activity 完全擋住兒而不可見的,仍保有自己的狀態與資訊,但 window manager 已不再管理其訊息,系統資源不夠時會被殺掉。


當 activity is onPause or onStop,系統則可以清楚它,可能提示 user 是否結束或只是殺掉其 process。




* 當Progress Dialog啟動時Activity模糊化(blur)進入OnPause()狀態, 並以執行緒(Thread)模擬事件進度.



* Reference
- Android activity 堆栈(1) - 嘴嘴的小的日志 - 网易博客
- tsots的Android範例Source: ProgressDialog~ProgressBar~搭配Handler顯示執行進度

2011年9月4日 星期日

[Android] INSTALL_FAILED_INSUFFICIENT_STORAGE error

* Error
- message:
Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
Please check logcat output for more details. 
Launch canceled!
- It happens regardless of app size, or how much storage is available.


* Solution
  1. 先移除再重新安裝,可解決,但可能還是會再發生相同問題。
  2. If your test device is running Android 2.2 or later then add the android:installLocation attribute to your application's manifest file, with the value "preferExternal". This will force the app to be installed on the device's external storage, such as a phone's SD card.
  3. assets內過大的資源放在(載到)SD card,再重SD card 讀取。
  4. process internal memory default is 16M,可改變 RUNTIME_HEAP_MAX 的大小:
// 尚未試過以下這方法
#ifdef CUSTOM_RUNTIME_HEAP_MAX
#define __make_max_heap_opt(val) #val
#define _make_max_heap_opt(val) "-Xmx" __make_max_heap_opt(val)
opt.optionString = _make_max_heap_opt(CUSTOM_RUNTIME_HEAP_MAX);
#undef __make_max_heap_opt
#undef _make_max_heap_opt
#else
/* limit memory use to 16MB */
opt.optionString = "-Xmx16m";
#endif
mOptions.add(opt);


* Reference
- Solution: Android INSTALL_FAILED_INSUFFICIENT_STORAGE error - Stack Overflow *
- Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE - Android 问题&解答 - eoe·Android开发者门户 - Powered by Discuz!

API Release Version

[Java]
  • for each
    • from 1.5
  • @Override
    • from 1.5
    • 但在 1.5 不支援 interface 使用,因此必須使用 1.6。
    • 20120615
      • 1.7 也無支援

[JSP]
  • EL
    • from 2.0


* Reference
- 关于解决Java @Override的问题


2011年9月3日 星期六

[Web] CDATA

* 原本要在 xml 中使用特殊字元時,則必須寫成 &lt;、&gt;、、等,否則 xml 分析會出錯。


CDATA 區段提供了一種通知剖析器的方法,說明 CDATA 區段所包含的字元沒有標記。 寫法如下:


<![CDATA[  >   ]]>


* 也就是說在 CDATA 中的字元不會被解析。




* Reference
- CDATA 區段 - XML 標準

[Android] ANDROID_ID

* Definition
- A 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)
- 第一次啟動 Android 設備時隨機生成的64位數(以十六進制字符串表示), "應在"保持設備一生不變的. (當設備執行重置出廠值時, 該值可能改變.)

* from level 3

* How to get?
- Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
- but you will get "null" on emulator.

* Is it unique for each Device?


Update on 20111106
  • 透過設定中的選項恢復原廠設定後,ANDROID_ID 仍是相同。
  • 若是更新系統,是否仍會相同?
  • 重新刷ROM的話,是否仍會相同?


* Reference
- Settings.Secure | Android Developers
- 獲取 Android 設備的 ID,Settings.Secure.ANDROID_ID

[Design] Design Software

軟體開發不是光靠程式設計 | 專欄 | iThome online

* Coding只是開發過程中的一部份工作
把程式設計等同於軟體開發,當然是很大的誤解。除了程式設計之外,軟體開發中涉及更多的層面。而其中,是否擁有正確的軟體開發觀念,絕對影響到是否能更順利地開發出有品質的軟體。

* 你會需要有這麼一個版本讓所有的人(包括客戶、使用者、產品經理、出錢的大老闆)清楚看到一個可以運作的系統,他們會對系統應該要是什麼樣的面貌更有概念,才更知道軟體功能該如何調整。

* 許多有經驗的開發者會知道,可以設定一個遠大的目標,但是,要先做出一個功能不那麼強、但是足夠穩定、能夠正常運作的第一個版本,接著再以這個版本為基礎,持續地演化及改進。

* 在開發的時候,要讓需求、規格盡量保持不變,甚至在時間有限時、而期限又不允許更動時,還要適度將那些較不優先的功能自開發範圍中削除

[Database] CLOB and BLOB

[CLOB]
- Character Large OBject。
- 用於儲存大量的文字資料。


[BLOB]
- Binary Large Object。
- 用於儲存大量的二進位資料。


* Reference
- Java Gossip: 將檔案存入資料庫

[JAVA] BigDecimal compare


  • The BigDecimal.equals method does not think that 2.0 is equals to 2.00, but compareTo does.
  • equals
    • new Integer(0).equals(new BigDecimal(0)): false
    • new Integer(0).toString().equals(new BigDecimal(0).toString()): true



* Reference
- Java BigDecimal equals vs. comareTo | Xinotes

[Database] Oracle ROWNUM and ROWID

[ROWNUM]

* Definition
- 執行結果資料的列數
- Is a pseudo-column that returns a row's position in a result set.
- Is evaluated AFTER records are selected from the database and BEFORE the execution of ORDER BY clause.
- Only exists for a row once it is retrieved from a query. It represents the sequential order in which Oracle has retrieved the row. Therefore it will always exist, be at least 1, and be unique (among the rows returned by the query).


* 運作方式:
  1. Oracle executes your query.
  2. Oracle fetches the first row and calls it row number 1.
  3. Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
  4. Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
  5. Go to step 3.
- 使用 ROWNUM > (condition) 是不合理的,因為在第三步時會不符合條件而不在結果內,第四步查出來的 ROWNUM 仍然是1,所以永遠也不會達成條件。
- 同樣道理,ROWNUM 如果單獨用 = ,也只有在 ROWNUM=1 時才有用。

* 因此,如果需要查詢 ROWNUM 在某區間的資料時,就必須使用子查詢的方式,但數據量大時會影響速度。
- ex: select * from(select rownum no ,id,name from student) where no>2;。
- 注意子查詢中的 rownum 必須要有别名,否則還是會查不到。因為 ROWNUM 不是真正屬於某個表的列,若沒有別名會無法判斷是子查詢還是主查詢的 ROWNUM。

* 還有,若是和 ORDER BY 同時使用的話,也必須使用子查詢,因為 ROWNUM 會比 ORDER BY 先被執行而指定,指定完列數後才會被 ORDER BY,這樣一來,最後的 ROWNUM 也亂掉了。


[ROWID]

* ROWID actually represents the physical location of the record/row in the database. That being the case, it is (according to Oracle documentation) the fastest way to retrieve a particular row. Faster than an index, even.

* Also both referred to as pseudo-columns. That is, they are not "real" columns that will show up when you DESC a table. They don't actually exist anywhere in the database. But they're available for you to use.



* Reference
- ROWNUM - Oracle FAQ
- OracleBlog: ROWNUM and ROWID
- Oracle的rownum原理和使用 - DCTM + SAP - ITeye技术网站 **

[Database] SELECT and ORDER BY

[SELECT order]

SELECT a.FIELD1, (SELECT b.FIELD1 FROM TABLEB b WHERE b.FIELD1 = a.FIELD1) AS c

FROM TABLE a

WHERE a.FIELD1 > 0;

* c 每次 SELECT 都會執行一次,以參考 a.FIELD1 產生欄位內容。


[ORDER BY]

* 如果order by 條件不是unique 不能確定select結果的順序會一樣

因為不知道oracle是怎麼運作來決定的 所以不知道順序的依據

不過select出來的資料不會有誤