




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
英文原文everything is an objectalthough it is based on c+, java is more of a “pure” object-oriented language.both c+ and java are hybrid languages, but in java the designers felt that the hybridization was not as important as it was in c+. a hybrid language allows multiple programming styles; the reason c+ is hybrid is to support backward compatibility with the c language. because c+ is a superset of the c language, it includes many of that languages undesirable features, which can make some aspects of c+ overly complicated. the java language assumes that you want to do only object-oriented programming. this means that before you can begin you must shift your mindset into an object-oriented world (unless its already there). the benefit of this initial effort is the ability to program in a language that is simpler to learn and to use than many other oop languages. alown well see the basic components of a java program and well learn that everything in java is an object, even a java program. you manipulate objects with references each programming language has its own means of manipulating data. sometimes the programmer must be constantly aware of what type of manipulation is going on. are you manipulating the object directly, or are you dealing with some kind of indirect representation (a pointer in c or c+) that must be treated with a special syntax? all this is simplified in java. you treat everything as an object, using a single consistent syntax. although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object.10 you might imagine this scene as a television (the object) with your remote control (the reference). as long as youre holding this reference, you have a connection to the television, but when someone says “change the channel” or “lower the volume,” what youre manipulating is the reference, which in turn modifies the object. if you want to move around the room and still control the television, you take the remote/reference with you, not the television. also, the remote control can stand on its own, with no television. that is, just because you have a reference doesnt mean theres necessarily an object connected to it. so if you want to hold a word or sentence, you create a string reference: string s;but here youve created only the reference, not an object. if you decided to send a message to s at this point, youll get an error (at run time) because s isnt actually attached to anything (theres no television). a safer practice, then, is always to initialize a reference when you create it: string s = asdf;however, this uses a special java feature: strings can be initialized with quoted text. normally, you must use a more general type of initialization for objects. you must create all the objectswhen you create a reference, you want to connect it with a new object. you do so, in general, with the new keyword. the keyword new says, “make me a new one of these objects.” so in the preceding example, you can say: string s = new string(asdf);not only does this mean “make me a new string,” but it also gives information about how to make the string by supplying an initial character string. of course, string is not the only type that exists. java comes with a plethora of ready-made types. whats more important is that you can create your own types. in fact, thats the fundamental activity in java programming, and its what youll be learning about in the rest of this book. its useful to visualize some aspects of how things are laid out while the program is runningin particular how memory is arranged. there are six different places to store data: registers. this is the fastest storage because it exists in a place different from that of other storage: inside the processor. however, the number of registers is severely limited, so registers are allocated by the compiler according to its needs. you dont have direct control, nor do you see any evidence in your programs that registers even exist. the stack. this lives in the general random-access memory (ram) area, but has direct support from the processor via its stack pointer. the stack pointer is moved down to create new memory and moved up to release that memory. this is an extremely fast and efficient way to allocate storage, second only to registers. the java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. this constraint places limits on the flexibility of your programs, so while some java storage exists on the stackin particular, object referencesjava objects themselves are not placed on the stack. the heap. this is a general-purpose pool of memory (also in the ram area) where all java objects live. the nice thing about the heap is that, unlike the stack, the compiler doesnt need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. thus, theres a great deal of flexibility in using storage on the heap. whenever you need to create an object, you simply write the code to create it by using new, and the storage is allocated on the heap when that code is executed. of course theres a price you pay for this flexibility. it takes more time to allocate heap storage than it does to allocate stack storage (if you even could create objects on the stack in java, as you can in c+). static storage. “static” is used here in the sense of “in a fixed location” (although its also in ram). static storage contains data that is available for the entire time a program is running. you can use the static keyword to specify that a particular element of an object is static, but java objects themselves are never placed in static storage. constant storage. constant values are often placed directly in the program code, which is safe since they can never change. sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (rom), in embedded systems. non-ram storage. if data lives completely outside a program, it can exist while the program is not running, outside the control of the program. the two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated. the trick with these types of storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a regular ram-based object when necessary. java provides support for lightweight persistence, and future versions of java might provide more complete solutions for persistence. high-precision numbersjava includes two classes for performing high-precision arithmetic: big integer and big decimal. although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive analogue. both classes have methods that provide analogues for the operations that you perform on primitive types. that is, you can do anything with a big integer or big decimal that you can with an int or float, its just that you must use method calls instead of operators. also, since theres more involved, the operations will be slower. youre exchanging speed for accuracy. big integer supports arbitrary-precision integers. this means that you can accurately represent integral values of any size without losing any information during operations. big decimal is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations, for example. consult the jdk documentation for details about the constructors and methods you can call for these two classes. arrays in javavirtually all programming languages support arrays. using arrays in c and c+ is perilous because those arrays are only blocks of memory. if a program accesses the array outside of its memory block or uses the memory before initialization (common programming errors), there will be unpredictable results. one of the primary goals of java is safety, so many of the problems that plague programmers in c and c+ are not repeated in java. a java array is guaranteed to be initialized and cannot be accessed outside of its range. the range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity is worth the expense. when you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null. when java sees null, it recognizes that the reference in question isnt pointing to an object. you must assign an object to each reference before you use it, and if you try to use a reference thats still null。thus, typical array errors are prevented in java. you can also create an array of primitives. again, the compiler guarantees initialization because it zeroes the memory for that array. you never need to destroy an objectin most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. how long does the variable last? if you are supposed to destroy it, when should you? confusion over variable lifetimes can lead to a lot of bugs, and this section shows how java greatly simplifies the issue by doing all the cleanup work for you. scopingmost procedural languages have the concept of scope. this determines both the visibility and lifetime of the names defined within that scope. in c, c+, and java, scope is determined by the placement of curly braces. 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. 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.scope of objectsjava 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. the references 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, because the only reference to it is out of scope. in later chapters youll 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. the hardest problems seem to occur in c+ because you dont get any help from the language in making sure that the objects are available when theyre needed. and more important, in c+ you must make sure that you destroy the objects when youre 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. from: wang rui book. c+ database system development detailed explanation m.beijing: electronics industry publishing house. 2002.中文译文一切皆是对象 “尽管以c+为基础,但java是一种更纯粹的面向对象程序设计语言”。无论c+还是java都属于杂合语言。但在java中,设计者觉得这种杂合并不像在c+里那么重要。杂合语言允许采用多种编程风格;之所以说c+是一种杂合语言,是因为它支持与c语言的向后兼容能力。由于c+是c的一个超集,所以包含的许多特性都是后者不具备的,这些特性使c+在某些地方显得过于复杂。java语言首先便假定了我们只希望进行面向对象的程序设计。也就是说,正式用它设计之前,必须先将自己的思想转入一个面向对象的世界(除非早已习惯了这个世界的思维方式)。只有做好这个准备工作,与其他oop语言相比,才能体会到java的易学易用。下面,我们将探讨java程序的基本组件,并体会为什么说java乃至java程序内的一切都是对象。(1)用句柄操纵对象。每种编程语言都有自己的数据处理方式。有些时候,程序员必须时刻留意准备处理的是什么类型。您曾利用一些特殊语法直接操作过对象,或处理过一些间接表示的对象吗(c或c+里的指针)?所有这些在java里都得到了简化,任何东西都可看作对象。因此,我们可采用一种统一的语法,任何地方均可照搬不误。但要注意,尽管将一切都“看作”对象,但操纵的标识符实际是指向一个对象的“句柄”(handle)。在其他java参考书里,还可看到有的人将其称作一个“引用”,甚至一个“指针”。可将这一情形想象成用遥控板(句柄)操纵电视机(对象)。只要握住这个遥控板,就相当于掌握了与电视机连接的通道。但一旦需要“换频道”或者“关小声音”,我们实际操纵的是遥控板(句柄),再由遥控板自己操纵电视机(对象)。如果要在房间里四处走走,并想保持对电视机的控制,那么手上拿着的是遥控板,而非电视机。此外,即使没有电视机,遥控板亦可独立存在。也就是说,只是由于拥有一个句柄,并不表示必须有一个对象同它连接。所以如果想容纳一个词或句子,可创建一个string句柄:string s;但这里创建的只是句柄,并不是对象。若此时向s发送一条消息,就会获得一个错误(运行期)。这是由于s实际并未与任何东西连接(即“没有电视机”)。因此,一种更安全的做法是:创建一个句柄时,记住无论如何都进行初始化:string s = asdf;然而,这里采用的是一种特殊类型:字串可用加引号的文字初始化。通常,必须为对象使用一种更通用的初始化类型。(2)所有对象都必须创建。创建句柄时,我们希望它同一个新对象连接。通常用new关键字达到这一目的。new的意思是:“把我变成这些对象的一种新类型”。所以在上面的例子中,可以说:string s = new string(asdf);它不仅指出“将我变成一个新字串”,也通过提供一个初始字串,指出了“如何生成这个新字串”。当然,字串(string)并非唯一的类型。java配套提供了数量众多的现成类型。对我们来讲,最重要的就是记住能自行创建类型。事实上,这应是java程序设计的一项基本操作,是继续本书后余部分学习的基础。程序运行时,我们最好对数据保存到什么地方做到心中有数。特别要注意的是内存的分配。有六个地方都可以保存数据:寄存器。这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部。然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配。我们对此没有直接的控制权,也不可能在自己的程序里找到寄存器存在的任何踪迹。堆栈。驻留于常规ram(随机访问存储器)区域,但可通过它的“堆栈指针”获得处理的直接支持。堆栈指针若向下移,会创建新的内存;若向上移,则会释放那些内存。这是一种特别快、特别有效的数据保存方式,仅次于寄存器。创建程序时,java编译器必须准确地知道堆栈内保存的所有数据的“长度”以及“存在时间”。这是由于它必须生成相应的代码,以便向上和向下移动指针。这一限制无疑影响了程序的灵活性,所以尽管有些java数据要保存在堆栈里特别是对象句柄,但java对象并不放到其中。堆。一种常规用途的内存池(也在ram区域),其中保存了java对象。和堆栈不同,“内存堆”或“堆”(heap)最吸引人的地方在于编译器不必知道要从堆里分配多少存储空间,也不必知道存储的数据要在堆里停留多长的时间。因此,用堆保存数据时会得到更大的灵活性。要求创建一个对象时,只需用new命令编制相关的代码即可。执行这些代码时,会在堆里自动进行数据的保存。当然,为达到这种灵活性,必然会付出一定的代价:在堆里分配存储空间时会花掉更长的时间!静态存储。这儿的“静态”(static)是指“位于固定位置”(尽管也在ram里)。程序运行期间,静态存储的数据将随时等候调用。可用static关键字指出一个对象的特定元素是静态的。但java对象本身永远都不会置入静态存储空间。常数存储。常数值通常直接置于程序代码内部。这样做是安全的,因为它们永远都不会改变。有的常数需要严格地保护,所以可考虑将它们置入只读存储器(rom)。非ram存储。若数据完全独立于一个程序之外,则程序不运行时仍可存在,并在程序的控制范围之外。其中两个最主要的例子便是“流式对象”和“固定对象”。对于流式对象,对象会变成字节流,通常会发给另一台机器。而对于固定对象,对象保存在磁盘中。即使程序中止运行,它们仍可保持自己的状态不变。对于这些类型的数据存储,一个特别有用的技巧就是它们能存在于其他媒体中。一旦需要,甚至能将它们恢复成普通的、基于ram的对象。java 1.1提供了对lightweight persistence的支持。未来的版本甚至可能提供更完整的方案。(3)高精度数字java 1.1增加了两个类,用于进行高精度的计算:big integer和big decimal。尽管它们大致可以划分为“封装器”类型,但两者都没有对应的“主类型”。这两个类都有自己特殊的“方法”,对应于我们针对主类型执行的操作。也就是说,能对int或float做的事情,对big integer和big decimal一样可以做。只是必须使用方法调用,不能使用运算符。此外,由于牵涉更多,所以运算速度会慢一些。我们牺牲了速度,但换来了精度。
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 清理案件现场协议书
- 民间个人贸易协议书
- 机械租货安全协议书
- 村委砍伐合同协议书
- 正规领养孩童协议书
- 支架家属和解协议书
- 施工签订居间协议书
- 民事按摩和解协议书
- 榕基软件股权协议书
- DL-T 2151.2-2024 岸基供电系统 第2部分:工频电源
- 我的家乡宁波
- 路灯设施维修施工组织设计
- 执业医师注册健康体检表
- 普通高等学校毕业生 毕业研究生就业协议书
- 地铁通信工程漏缆卡具安装作业指导书
- 地下室顶板施工电梯加固方案(共4页)
- 【工程文档】电梯安装样板放线记录
- EAS制造功能培训_MPS和MRP_完整版(截止70)
- 烤烟常见虫草害的防治措施
- 新课改背景下的初中数学课堂有效教学研究
- 叉车自查表 厂内机动车辆(叉车)安全自查表
评论
0/150
提交评论