public class PassTest{
float ptValue;
public static void main (String [] args){
String str;
int val;
PassTest pt = new PassTest();
val = 11;
pt.changInt(val);
System.out.println("Int value is : " + val );
str = new String ("hello");
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is : " +pt.ptValue);
} public void changInt ( int value ){
value = 55;
}
public void changeStr ( String value ){
value = new String ("dfferent");
} public void changeObjValue(PassTest ref){
ref.ptValue = 99f;
}
}
这里的val和str的值怎么没有改变呀。。如果我要改天它们的值应该怎么修改呢?
这是书上的例题。看不懂了,请教下大家。。我试着改天val和str的值,可是编译通不过
float ptValue;
public static void main (String [] args){
String str;
int val;
PassTest pt = new PassTest();
val = 11;
pt.changInt(val);
System.out.println("Int value is : " + val );
str = new String ("hello");
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is : " +pt.ptValue);
} public void changInt ( int value ){
value = 55;
}
public void changeStr ( String value ){
value = new String ("dfferent");
} public void changeObjValue(PassTest ref){
ref.ptValue = 99f;
}
}
这里的val和str的值怎么没有改变呀。。如果我要改天它们的值应该怎么修改呢?
这是书上的例题。看不懂了,请教下大家。。我试着改天val和str的值,可是编译通不过
解决方案 »
- HashMap的效能因子是什么?
- 在string中如何实现对某个子母或单词的查找并输出要查找字段的首字母其在整个String中的索引位置?
- 在虚拟机器里的eclipse安装svn
- <forward name 和 path 分别表示什么意思
- JAVA面试题解惑系列(七)——日期和时间的处理
- 求助
- 关于检测输入得问题,困惑好几天了,希望大哥门解答
- 求教高手点拨“一个3W行代码工程量的Login”步骤的错误排查的经验。
- tomcat配置问题
- jb6-----"unknown license file format" ?????
- javascript 验证url 和radio特效
- 问一个数据的问题 ACCESS !在线
float ptValue; public static void main(String[] args) {
String str;
int val; PassTest pt = new PassTest();
val = 11;
pt.changInt(val);//调用方法changInt(val) 将11赋给临时变量 value 再对value进行操作
//只是改变了value的值,val自然不会改变
System.out.println("Int value is : " + val); str = new String("hello");//同上
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);//调用方法后,pt和ref都指向对象 new PassTest()
//就好比你(pt)和我(ref)都有一个房间(new PassTest())的钥匙,我们都可以进房;
//我进房间把房里的花瓶(ptValue)里的兰花(101f)换成茶花(99f);
//你再进房时,房里的花瓶里就一定会是茶花(99f)而非兰花(101f)了
System.out.println("Current ptValue is : " + pt.ptValue);
} public void changInt(int value) {
value = 55;
} public void changeStr(String value) {
value = new String("dfferent"); } public void changeObjValue(PassTest ref) {
ref.ptValue = 99f;
}
}
说明了一下,不知道你看得懂否
你最好找本书看一下,自己仔细体会下!
如果要调方法的话:
1.可以将方法的返回值赋给变量;
2.作为类的属性修改,就像变量pt.ptValue 一样.
我改了后一直通不过编译!
然后
public void changeVal(int val){
this.val=val;
}public void changeStr(String str){
this.str=str;
}
float ptValue; public static void main(String[] args) {
String str;
int val;
PassTest pt = new PassTest();
val = 11;
val = pt.changInt(55);
System.out.println("Int value is : " + val); str = new String("hello");
str = pt.changeStr("dfferent");
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is : " + pt.ptValue);
} public int changInt(int value) {
return value;
} public String changeStr(String value) {
return value; } public void changeObjValue(PassTest ref) {
ref.ptValue = 99f;
}
}
我把你代码改了下~!是调用方法的,你试试~!!!
public class PassTest {
float ptValue;
static int val=11;
static String str = new String("hello");
public static void main(String[] args) { PassTest pt = new PassTest();
pt.changInt(val);
System.out.println("Int value is : " + val); pt.changeStr(str);
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is : " + pt.ptValue);
} public void changInt(int value) {
value = 55;
this.val=value;
} public void changeStr(String value) {
value = new String("dfferent");
this.str = value;
} public void changeObjValue(PassTest ref) {
ref.ptValue = 99f;
}
}不好意思~~发错了~!应该是这个!!嘿嘿~!
符号: 类 tring
位置: 类 PassTest
tring str;
^
PassTest.java:10: 无法从静态上下文中引用非静态 变量 val
val = 11;
^
PassTest.java:11: 无法从静态上下文中引用非静态 变量 val
pt.changInt(val);
^
PassTest.java:13: 无法从静态上下文中引用非静态 变量 val
System.out.println("Int value is : " + val);
^
PassTest.java:15: 无法从静态上下文中引用非静态 变量 str
str = new String("hello");
^
PassTest.java:16: 无法从静态上下文中引用非静态 变量 str
System.out.println("Str value is : " + str);我看不懂(无法从静态上下文中引用非静态变量 )这句话。。
高手指点下!
Int value is : 55
Str value is : dfferent
Current ptValue is : 99.0
这个是结果啊~~!
你把我得代码全靠下来~!试试!!
因为我把你定义变量得位置变了,而且设置为静态得了!!呵呵~!
1、基本数据类型和字符串按值传递
2、数组和除字符串以外的所有对象按址传递
内存在程序运行是分四个块,栈、堆、还有两个部分(其中一个载入class类,还有一块存储类的成员变量和字符串)。
当我们声明一个变量是在栈中分配内存,如果是基本数据类型就直接赋值。如果是引用类型就会在堆空间中开辟内存,而在栈中存放的是一个地址,这个地址指向堆中一块内存,当我们调用一个方法的时候会将这个地址传给形参,这样就都指向了堆中同一块内存,对形参的改变理所当然的会改变原来的值(因为都指向同一块堆内存),但是string 类型是不可变的,所以这么改变不了,如果用stringbuffer这可以改变,但是又不能用new stringbuffer来改变,因为这么做其实是开辟了一个新的堆空间,你可以直接的用stringbuffer类的方法来改变这个串,有效的。
最好自己来测试一下。