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();本文来自优尔\文(论"文?网,毕业论文 www.youerw.com 加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).
- 上一篇:PLC数控加工工业机械手英文文献和翻译
- 下一篇:机械电子轧机的空间振动英文文献和翻译
-
巴金《激流三部曲》高觉新的悲剧命运
C++最短路径算法研究和程序设计
上市公司股权结构对经营绩效的影响研究
中国传统元素在游戏角色...
高警觉工作人群的元情绪...
江苏省某高中学生体质现状的调查研究
浅析中国古代宗法制度
g-C3N4光催化剂的制备和光催化性能研究
现代简约美式风格在室内家装中的运用
NFC协议物理层的软件实现+文献综述