thingking in java 2.11练习(1)

题目:创建一个类,它包含一个int域和一个char域,它们都没有被初始化,将它们的值打印出来,以验证java执行了默认初始化。

        这个题目的第一个难点,就是对int域char域的理解,开始还以为自己前面看书不仔细,漏了什么概念,但讲作用域那节翻烂了也没看出跟这个有什么关系的。

        问题解决不了找度娘,万能的度娘告诉我们,这是翻译的问题,域对应英文filed ,也可译为字段。解答来自csdn论坛,地址:http://bbs.csdn.net/topics/380076163

        所以,这个题目其实就是叫你创一个类,类里有一个int 类型的成员变量和char类型的成员变量,它们都没有被初始化,将它们值打印出来来验证java执行了默认初始化。

      

        接下来码代码。

        

public class test1 {
    private  int i;
    private char str;

    public static void main(String[] args) {
        System.out.println("i : " +i);
        System.out.println("str : " +str);
    }
}

       但是编译器报错了,错误:non—static filed“str” can not be referenced from a static context.

       什么意思呢?就是,非静态字段(这里把filed翻成域似乎也读的通)不能从一个静态域中被引用,  main就是那个引用i和str的静态域。

       还是不理解,继续百度,这次答案从stackoverflow上找到:

       

You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.

At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.

So the class says "all cars have a color" and the instance says "this specific car is red".

In the OO world, you define the class and inside the class, you define a field of type Color

       原文地址:http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context

       巴拉巴拉一大堆,看了一遍,大致说的是类与一个实例化类的区别。回到代码我们发现对i和char我们并没有实例化,仅仅是定义了两个变量。但是题目要求不能对它们初始化。对于基本类型,实例化就相当于初始化(个人理解,有错请指出)

        解决方法:给它们加上static

public class test1 {
    private static int i;
    private static char str;


    public static void main(String[] args) {
        System.out.println("i : " +i);
        System.out.println("str : " +str);
    }
}


这次顺利运行,运行结果如下:

i : 0
str :  

所以这个题目就是  让我们验证了一下int char等基本类型即使我们没有对其初始化,但也被java执行了默认初始化。

我们对八种基本类型用同样的方法验证后得到:

boolean:  false
char:   
byte:  0
short:  0
int:  0
long:  0
float:  0.0
double:  0.0

值得一提的是,在书里,void也是基本类型之一,我们来看看void没有初始化时java对它做的默认初始化。

但当我用private static void aVoid;时编译器报了一个 illegal type的错误,说这是不合法的类型。为什么呢?

好吧,void是方法的基本类型,忘了括号,加上括号 :private static void aVoid();

还是报错:missing method body,or declare abstract。

两种解决方案,先加上方法体试试:

private static void aVoid() {
    }

不报错了。不过使用abstract 关键字时,仍提示有错。

按理说void是基本类型,我们尝试着system.out一下:

System.out.println("avoid : " +aVoid());

编译器提示:operator ‘+’ can not be applied to “java.lang.string”,‘void’

报错的意思就是不能把void转为string 类型,似乎没看懂,运行一下试试,编译器报错:Error:(27, 28) java: 此处不允许使用 '空' 类型。void就是那个空类型。

这里再尝试百度了一下void

百度百科对其的解释是:

void的字面意思是“无类型”,void *则为“无类型 指针”,void *可以指向任何类型的数据。
void几乎只有“注释”和限制程序的作用,定义一个void变量没有意义,不妨试着定义:
void a;
这行语句编译时会出错,提示“illegal use of type 'void'”。不过,即使void a的编译不会出错,它也没有任何实际意义。
void真正发挥的作用在于:
(1) 对函数返回的限定;
(2) 对函数参数的限定。

所以void只要记住是定义方法时会用到就行了。

以上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值