PEIGANG's profile铁匠铺PhotosBlogLists Tools Help

Blog


    September 24

    由一个javaGUI程序想到的

    最近帮师兄写了一个很小的java界面程序,然后去调用一个exe程序,去完成实际的工作。 程序在不打包的时候运行的一点问题都没有。但一到打包问题就来了。先是一些图片资源没有正确载入,这也是个老问题了,以前就遇到过,当时没有仔细解决就放过去了。这次饶不过去了,还好很快从网上找到了解决的办法。于是,把界面程序和exe程序打成了一个jar包,交给师兄。
    结果没几天,师兄就和我说那个jar包在离开工程目录的时候没法用。我当时还把jar包拷到其他目录去验证过图片资源的载入问题,但就是没有再多点几下按钮,试试程序好不好用。
    从这事我就想到以前看到义明在开发的时候由于没有做一些测试而出现的一些问题,当时我还对自己说,以后自己可要多测试一下自己程序,不要犯同样的错误。没想到,我自己确造出了一个大大的笑话:能跨平台运行的java程序,让我搞成了连硬盘分区都跨不了的蹩脚程序。
    经常是对自己的程序过于自信了,能运行就行,没有质量可言。
    October 09

    Parameter Passing in Java

    In Java, you may use a function which has parameter, then what does the parameter pass into the function? The answer is value, without any doubt. Let us start the insight from the data type. In Java, there are two types of data: one is basic data type, another is object data type. The basic data type contains byte, short, int, long, float, double, boolean and char. If you declare a variable of one of the eight basic data type in program, then the variable will stand for the memory address of the data. When you declare an object variable, the variable will store a specific memory address which the object data start from. We call object variable store a reference of the object and the basic data type variable store the value of the data.

    Then, if you pass a parameter into a function, a copy of the parameter will be automatically created in the program stack. So a basic data type parameter will have a copy of value, and an object data type parameter will have a copy of reference. But we can see the reference as a special type of value and we can call it reference value.

    Before following discussion, we will introduce another two conceptions: mutable and immutable. If an object has some operations like: setValue(Datatype value), etc. These operations can change the object member variable, i.e. change the state of the object. We call this object a mutable object, the class of this object a mutable class. If an object does not any operation which can change the member variable, we call it a immutable object, the class a immutable class.

    If you understand above, then let us talk about a more important issue: if a function does some operations to the parameter passed-in, will the parameter has some change after the function call? There are three conditions. If you pass in a parameter belongs to one of the basic data types, the parameter will not change in any case. If you pass in a mutable object and call the object’s ‘set’ operation, then the object will change after function call. If you pass in an immutable object or a mutable object, but do not call its ‘set’ operation, the parameter will not change also.

    September 10

    无题

    这几天在一边写着代码,一边进行着调试。在排错之后,联想到以前看过的一些编程规范,感到那上面说的程序员很容易犯的错误,我就犯了这些错误。

    举一个编程规范的例子:要注意一些边界条件的判断。这一条的大意是说在进行一些大于或小于的判断时,要注意是否存在等号的问题,即大于等于或小于等于。以前看这条规范时,对自己说在编程时要注意,可实际情况是在编程时还是没有注意。直到程序运行时出现意想不到的错误后,在代码行里艰难地看来看去,想来想去才发现是边界条件的问题。

    不过话又说回来,让一个没有经历过这种错误的人看那些规范,我想他也并不知道该怎么去注意,就像我自己一样。直到碰上了这样的错误,排除错误之后,一拍脑门想起来:哦,是这个问题呀!嗯,以后可得注意了。这时,也就会真正感到那些规范说的确实是很有道理的。 如果这样说的话,那些规范并不能很有效的防止错误的发生。因为,没有经验的人看了之后,并不能很好的理解,在实际操作时,也就不会在脑子里绷着这么一根弦。

    而对于那些犯过这样的错误,有经验的人来说,他们根本就没有必要再去看规范了,因为,他们已经知道这种错误很容易在那里发生,从而去避免。通过上面的论述,好像可以得出一个结论,那就是规范是没有什么实际用途的,它只能在人们犯过错误之后用来发一下感慨。这就像是在说:看的不会,会的不看。看过未必会,不看还是会。

    上面说的都是些思想垃圾。规范终究还是有它自己的作用的,不过我想更重要的可能还是经验。至于经验怎么获取,我想大家都有自己的体会。如果不知道的话,嗯,那只能问一下Bjarne Stroustrup......^_^

    August 28

    Java中迭代器使用的一点注意

            学习Java编程时,用到了ListIterator迭代器,容器类是Vector。在进行一次迭代时,出现了错误,调试了挺长一段时间才发现自己在使用中的错误之处。
    错误代码片断如下:
    while(iter.hasPrevious())
    {
         if (some condition)
         {
               tmpObject = (SomeObject)iter.previous();
               tmpObject.someOperation();   
         }
    }
    在这里iter是ListIterator的一个实例,SomeOjbect是Vector中存储的对象类型。
    这个错误可能对于一个Java程序老手根本不会出现,但对于我这个菜鸟再加上有过使用STL迭代器的惯性,很容易就写下了上面的错误代码。错误在于:在Java中,迭代器的previous()或next()操作不仅返回下一个元素,它还负责推进迭代器的迭代动作进行下去。在上面的代码中,一旦条件不满足,就会跳过if块,这样就会跳过一次推进迭代进行的动作,导致一些不可预料的错误。
    改正后的代码片断如下:
    while(iter.hasPrevious())
    {
       tmpObject = (SomeObject)iter.previous();
       if (some condition)
         {
              tmpObject.someOperation();   
         }
    }
    从表面上看,只是一句代码位置的不同,但得到的结果却是不一样的。