菜单
  

    A variable defined within a scope is available only to the end of that scope.
    Any text after a ‘//’ to the end of a line is a comment.
    Indentation makes Java code easier to read. Since Java is a free-form language, the extra spaces, tabs, and carriage returns do not affect the resulting program.
    You cannot do the following, even though it is legal in C and C++:
    {
    int x = 12;
    {
    int x = 96; // Illegal
    }
    }
    The compiler will announce that the variable x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs.
    Scope of objects
    Java objects do not have the same lifetimes as primitives. When you create a Java object using new, it hangs around past the end of the scope. Thus if you use:
    {
    String s = new String("a string");
    } // End of scope
    the reference s vanishes at the end of the scope. However, the String object that s was pointing to is still occupying memory. In this bit of code, there is no way to access the object after the end of the scope, because the only reference to it is out of scope. In later chapters you’ll see how the reference to the object can be passed around and duplicated during the course of a program.
    It turns out that because objects created with new stay around for as long as you want them, a whole slew of C++ programming problems simply vanish in Java. In C++ you must not only make sure that the objects stay around for as long as you need them, you must also destroy the objects when you’re done with them.
    That brings up an interesting question. If Java leaves the objects lying around, what keeps them from filling up memory and halting your program? This is exactly the kind of problem that would occur in C++. This is where a bit of magic happens. Java has a garbage collector, which looks at all the objects that were created with new and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. This means that you never need to worry about reclaiming memory yourself. You simply create objects, and when you no longer need them, they will go away by themselves. This eliminates a certain class of programming problem: the so-called “memory leak,” in which a programmer forgets to release memory.
    Creating new data types: class
    If everything is an object, what determines how a particular class of object looks and behaves? Put another way, what establishes the type of an object? You might expect there to be a keyword called “type,” and that certainly would have made sense. Historically, however, most objectoriented languages have used the keyword class to mean “I’m about to tell you what a new type of object looks like.” The class keyword (which is so common that it will not usually be boldfaced throughout this book) is followed by the name of the new type. For example:
    class ATypeName { /* Class body goes here */ }
    This introduces a new type, although the class body consists only of a comment (the stars and slashes and what is inside, which will be discussed later in this chapter), so there is not too much that you can do with it. However, you can create an object of this type using new:
    ATypeName a = new ATypeName();本文来自751\文(论"文?网,毕业论文 www.751com.cn 加7位QQ324~9114找原文
    But you cannot tell it to do much of anything (that is, you cannot send it any interesting messages) until you define some methods for it.
    Fields and methods
    When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: fields (sometimes called data members), and methods (sometimes called member functions). A field is an object of any type that you can talk to via its reference, or a primitive type. If it is a reference to an object, you must initialize that reference to connect it to an actual object (using new, as seen earlier).
  1. 上一篇:PLC数控加工工业机械手英文文献和翻译
  2. 下一篇:机械电子轧机的空间振动英文文献和翻译
  1. 集成计算机英文文献和中文翻译

  2. 电站锅炉暖风器设计任务书

  3. java+mysql车辆管理系统的设计+源代码

  4. 中考体育项目与体育教学合理结合的研究

  5. 杂拟谷盗体内共生菌沃尔...

  6. 河岸冲刷和泥沙淤积的监测国内外研究现状

  7. 当代大学生慈善意识研究+文献综述

  8. 十二层带中心支撑钢结构...

  9. 乳业同业并购式全产业链...

  10. 大众媒体对公共政策制定的影响

  11. 酸性水汽提装置总汽提塔设计+CAD图纸

  

About

751论文网手机版...

主页:http://www.751com.cn

关闭返回