淺談JAVA內(nèi)存分配與參數(shù)傳遞
JAVA中方法的參數(shù)傳遞方式只有一種:值傳遞。
JAVA內(nèi)存分配:
1.棧:存放 基本類型的數(shù)據(jù)、對象的引用(類似于C語言中的指針)
2.堆:存放用new產(chǎn)生的數(shù)據(jù)
3.靜態(tài)域:存放在對象中用static定義的靜態(tài)成員
4.常量池:存放常量
5.寄存器
6.非RAM存儲
class BirthDate{ private int day; private int month; private int year; public BirthDate(int d,int m,int y){ day=d; month=m; year=y; } }
public class Test{ public static void main(String[] args){ int date=9; Test test=new Test(); test.change(date); BirthDate d1=new BirthDate(7,7,1970); } public void change(int i){ i=1234; } }
public class TestTransfer{ public static void main(String[] args){ int a=6; int b=9; swap(a,b); System.out.println("交換結(jié)束后,a的值是"+a+";b的值是"+b); //a=9,b=6 } public static void swap(int a,int b){ int tmp=a; a=b; b=tmp; System.out.println("swap方法里,a的值是"+a+";b的值是"+b); //a=6,b=9 } }
前
public class TestTransfer{ public static void main(String[] args){ DataSwap ds=new DataSwap(); ds.a=6; ds.b=9; swap(ds); System.out.println("交換結(jié)束后,ds.a的值是"+ds.a+";ds.b的值是"+ds.b); //a=9,b=6 } public static void swap(DataSwap ds){ int tmp=ds.a; ds.a=ds.b; ds.b=tmp; System.out.println("swap方法里,ds.a的值是"+ds.a+";ds.b的值是"+ds.b); //a=9,b=6 } } class DataSwap{ public int a; public int b; }
以上所述是小編給大家介紹的JAVA內(nèi)存分配與參數(shù)傳遞詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot利用隨機鹽值實現(xiàn)密碼的加密與驗證
這篇文章主要為大家詳細介紹了SpringBoot如何利用隨機鹽值實現(xiàn)密碼的加密與驗證,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-02-02Spring中BeanFactoryPostProcessors是如何執(zhí)行的
BeanFactoryPostProcessor是Spring容器提供的一個擴展機制,它允許開發(fā)者在Bean的實例化和初始化之前對BeanDefinition進行修改和處理,這篇文章主要介紹了你知道Spring中BeanFactoryPostProcessors是如何執(zhí)行的嗎,需要的朋友可以參考下2023-11-11Jaxb2實現(xiàn)JavaBean與xml互轉(zhuǎn)的方法詳解
這篇文章主要介紹了Jaxb2實現(xiàn)JavaBean與xml互轉(zhuǎn)的方法,簡單介紹了JAXB的概念、功能及實現(xiàn)JavaBean與xml互轉(zhuǎn)的具體操作技巧,需要的朋友可以參考下2017-04-04