Java實(shí)體類不要使用基本類型的知識點(diǎn)總結(jié)
今天來記錄一下,在項(xiàng)目中因?yàn)榛绢愋停a(chǎn)生的bug
包裝類:8種基本類型的包裝類
應(yīng)用場景:數(shù)據(jù)庫建立實(shí)體映射多用包裝類
這兩句話是重點(diǎn):就是建立實(shí)體類禁止使用基本數(shù)據(jù)量類型?。?!而用對應(yīng)的包裝類,
為什么呢,看以下場景。
JAVA代碼
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/** * 8中基本類型的對應(yīng)包裝類' * byte short int long double float boolean char * Byte Short Integer Long Double Float Boolean Character * 區(qū)別:(舉例int,其余相同) * 1、int默認(rèn)為0,integer默認(rèn)為null * 2、int是java的基本數(shù)據(jù)類型,integer是int的包裝類 * 3、integer必須new,int直接使用 */ /** * 場景一: * 創(chuàng)建對應(yīng)數(shù)據(jù)庫的實(shí)體類字段 * 1、創(chuàng)建一個類型(type),對應(yīng)數(shù)據(jù)庫的一個字段 * 2、注意:此存在嚴(yán)重問題,基本類型都默認(rèn)有值。如int 默認(rèn)為0 * 3、那在進(jìn)行數(shù)據(jù)庫新增的時(shí)候,如果不填,則會默認(rèn)為0。 * 4、會產(chǎn)生嚴(yán)重的bug,應(yīng)該改為包裝類的引用類型 */ //錯誤示范 private int type;//代表類型 //正確,設(shè)置為integer類型 private Integer typeT; </font></font></font>
所以,多用包裝類進(jìn)行賦值。
補(bǔ)充:
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/**
* 場景二:
* 自動裝箱And自動拆箱
*/
private void testBox() {
//原本轉(zhuǎn)換方式
int t = 10;
Integer ct = new Integer(t);
int tt = ct.intValue();
int i = 10;
//自動裝
Integer c = i;
//自動拆
int ic = c;
}
</font></font></font>
筆試題題如下?為什么一個為true,一個為false???
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/**
* 自動裝拆箱
*/
public static void main(String[] args) {
Integer integer0 = 127;
Integer integer1 = 127;
System.out.println(integer0 == integer1);//等于true
Integer integer2 = 128;
Integer integer3 = 128;
System.out.println(integer2 == integer3);//等于false
/** 源碼
* public static Integer valueOf(int i) {
* if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
* return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
* return new Integer(i);
* }
* 通過上我們發(fā)現(xiàn),如果他的int值在最高和最低之間,他直接返回cache內(nèi)的數(shù)據(jù)
* 否則, new Integer(i);
* 那么最高值:?=high 127 ,最低值:?=low -128,
* 所以:在-128至127內(nèi),他們引用的是緩存內(nèi)的數(shù)據(jù),地址相同,所以為true。超過此則為false
*
* private static class IntegerCache {
* static final int low = -128;
* static final int high;
* static final Integer cache[];
*
* static {
* // high value may be configured by property
* int h = 127;
* String integerCacheHighPropValue =
* sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
* if (integerCacheHighPropValue != null) {
* try {
* int i = parseInt(integerCacheHighPropValue);
* i = Math.max(i, 127);
* // Maximum array size is Integer.MAX_VALUE
* h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
* } catch( NumberFormatException nfe) {
* // If the property cannot be parsed into an int, ignore it.
* }
* }
* high = h;
*
* cache = new Integer[(high - low) + 1];
* int j = low;
* for(int k = 0; k < cache.length; k++)
* cache[k] = new Integer(j++);
*
* // range [-128, 127] must be interned (JLS7 5.1.7)
* assert IntegerCache.high >= 127;
* }
*
* private IntegerCache() {}
* }
*
*/
}</font></font></font>
以上就是本次介紹的全部相關(guān)知識點(diǎn),大家如果有任何補(bǔ)充可以聯(lián)系腳本之家小編。
相關(guān)文章
springboot運(yùn)行jar生成的日志到指定文件進(jìn)行管理方式
這篇文章主要介紹了springboot運(yùn)行jar生成的日志到指定文件進(jìn)行管理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳
這篇文章主要介紹了Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot淺析緩存機(jī)制之Redis單機(jī)緩存應(yīng)用
在上文中我介紹了Spring Boot使用EhCache 2.x來作為緩存的實(shí)現(xiàn),本文接著介紹使用單機(jī)版的Redis作為緩存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java實(shí)現(xiàn)規(guī)則幾何圖形的繪制與周長面積計(jì)算詳解
隨著計(jì)算機(jī)的發(fā)展,人們對圖形的計(jì)算要求會越來越高。在各行各業(yè)中的計(jì)算人員會對圖形的計(jì)算要有便利的要求,規(guī)則幾何圖形問題求解程序應(yīng)運(yùn)而生!本文將用Java編寫一個程序,可以實(shí)現(xiàn)規(guī)則幾何圖形的繪制與周長面積計(jì)算,感興趣的可以了解一下2022-07-07

