变量及其传递
2017-12-08 11:17
92 查看
1.变量类型(内存空间):
基本类型
引用型
例:
2.字段变量和局部变量 区别
(1)定义:
类中;方法中的变量
(2)内存中:
对象的一部分,堆中;存在栈中
(3)初始化:
可自动赋值;显示赋值
(4)语法:
可被public,private, static,final修饰;不能被访问控制符及static修饰
都可以被final修饰
例:
3.传递
Java是值传递,复制;对于引用型变量,传递值是引用值,不复制对象实体。
4.返回类型
基本类型
引用类型
基本类型
引用型
例:
public class MyDate { int year; int month; int day; public MyDate(int year, int month, int day){ this.year = year; this.month = month; this.day = day; } void addYear(){ year++; } public void display(){ System.out.println(year + "-" + month + "-" + day); } public static void main(String args[]) { MyDate m = new MyDate(2004, 9, 22); MyDate n = m; n.addYear(); m.display(); n.display(); } } 只复制引用,操纵的同一个对象
2.字段变量和局部变量 区别
(1)定义:
类中;方法中的变量
(2)内存中:
对象的一部分,堆中;存在栈中
(3)初始化:
可自动赋值;显示赋值
(4)语法:
可被public,private, static,final修饰;不能被访问控制符及static修饰
都可以被final修饰
例:
class Test() { int a;//自动赋值为0 void fun(){ int b; System.out.println(b);//无法通过,需要赋初值 } }
3.传递
Java是值传递,复制;对于引用型变量,传递值是引用值,不复制对象实体。
public class TransByValue { public static void main (String[] args) { int a = 0; modify (a); System.out.println(a);//result:0 值传递 int [] b = new int [1]; modify(b); System.out.println(b[0]); //result:1 引用类型 } public static void modify (int a) { a++; } public static void modify (int[] b) { b[0] ++; b = new int[5]; } }
4.返回类型
基本类型
引用类型
相关文章推荐
- Java变量及其传递
- 变量及其传递
- JAVA-2 数据类型,变量及其作用域
- 在SHELL中如何传递shell变量给awk
- R语言:基本变量及其之间关系探索分析
- 也谈C++深拷贝、浅拷贝和函数返回值作参数及其临时变量的生存期
- javascript的正则如何传递一个变量
- 007-多控制器管理及其控制器间的数据传递
- 隐藏变量的使用-前后台参数的传递控制。
- locale的设定及其LANG、LC_ALL、LANGUAGE环境变量的区别
- Ruby on Rails,通过实例变量从Controller向View传递数据
- Strict Standards: Only variables should be passed by reference 只允许传递变量
- 如何用Timer作为变量为FC/FB传递参数?
- mfc对话框窗口之间变量的传递
- 使用querystring,session变量以及server.Transfer方法来实现页面间的值传递
- locale的设定及其LANG、LC_ALL、LANGUAGE环境变量的区别
- 用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上
- 当查询的条数是作为变量传递时的解决方法
- SSIS中变量在包间的传递 推荐
- 浅谈Asp.net 多层架构中的变量引用与传递