通过代码看内部,一点点~

来源:互联网 发布:java获取post请求报文 编辑:程序博客网 时间:2024/05/18 18:42
第一个:int a=1;//在全局数据区int main(){int b=1,i;//main函数的栈上,b、i、c的地址连续char *c=NULL;//c仍在main函数的栈上,但它是个指针,具体指向的内容不在这里c=(char*)malloc(8);//c指向的地方有内容了,这些内容在main函数的堆上printf("&a=%u\n",&a);printf("&b=%u\n",&b);printf("&i=%u\n",&i);printf("&c=%u\n",&c);printf("c=%u\n",c);system("pause");return 0;}运行结果:&a=4288516&b=1245024&i=1245012&c=1245000c=3889536第二个:union{int a1[2];long b1;short c1[5];char d1[8];}UNION_TEST;int main(){printf("sizeof(UNION_TEST)=%d\n",sizeof(UNION_TEST));printf("sizeof(int)=%d\n",sizeof(int));printf("sizeof(long)=%d\n",sizeof(long));printf("sizeof(double)=%d\n",sizeof(double));printf("sizeof(float)=%d\n",sizeof(float));printf("sizeof(short)=%d\n",sizeof(short));printf("sizeof(char)=%d\n",sizeof(char));system("pause");return 0;}运行结果:sizeof(UNION_TEST)=12 //这个,这个…… 这个是要对齐的,和max{所有单元}的那个看齐。 本例是4. //所以总的长度需要能够被4整除,而且容得下所有变量。sizeof(int)=4sizeof(long)=4sizeof(double)=8sizeof(float)=4sizeof(short)=2sizeof(char)=1第三个:int main(){char c1='8',c2='2';printf("%c,%c,%d,%d\n",c1,c2,c1-c2,c1+c2);//运行结果:8,2,6,106     //ASCII码的相减和相加char ch;ch='a+b';//bch='\0';//回车,看不见的ch='7'+'9';//pch=5+9;//那个音乐符//竟然都能通过!!system("pause");return 0;}第四个:public class Test {public static  void change(String str){str+="jack";}public static  void  main(String  []  args)  throws  Exception{String str="welcome";change(str);System.out.println(str);}}运行结果:welcome//这种传参方式,会有变量的一个拷贝,不会改变实参的值。//如果是引用或指针的话,就改变值了。指针是将那个位置的内容重写了;//引用是实参的一个别名,实际还是实参,换个名字罢了。第五个:class Player{int id=0;static int num=0;}public class Test {public static  void  main(String  []  args)  throws  Exception{Player p1=new Player();Player p2=new Player();p1.id=1000;p2.id=2000;p1.num+=p1.id;p2.num+=p2.id;System.out.println("Player.num=="+Player.num);}}运行结果:Player.num==3000//这个,这个,是实例变量和类变量。//我的理解是:实例变量各自为王,自己改自己,和别人没关系,是独立的;//类变量是全局的,一个实例改了变量,其他的也随着改变,有牵一发而动全身的意思。菜鸟一个,真心期待高手指点!
原创粉丝点击