??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩综合精品,亚洲第一导航,欧美在线一二三http://www.aygfsteel.com/jingming/zh-cnFri, 20 Jun 2025 00:39:28 GMTFri, 20 Jun 2025 00:39:28 GMT60Java垃圾回收机制http://www.aygfsteel.com/jingming/archive/2006/08/01/61259.htmljingmingjingmingTue, 01 Aug 2006 15:31:00 GMThttp://www.aygfsteel.com/jingming/archive/2006/08/01/61259.htmlhttp://www.aygfsteel.com/jingming/comments/61259.htmlhttp://www.aygfsteel.com/jingming/archive/2006/08/01/61259.html#Feedback0http://www.aygfsteel.com/jingming/comments/commentRss/61259.htmlhttp://www.aygfsteel.com/jingming/services/trackbacks/61259.html

Java's garbage-collected heap

An introduction to the garbage-collected

heap of the Java virtual machine

By Bill Venners

Trackback: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html

SummaryA key feature of Java is its garbage-collected heap, which takes care of freeing dynamically allocated memory that is no longer referenced. Because the heap is garbage-collected, Java programmers don't have to explicitly free allocated memory. Here's a hands-on introduction to Java's garbage-collected heap. (4,000 words)

Page 1 of 4 : Java's garbage-collected heap

Welcome to another installment of "Under The Hood." This column gives Java developers a glimpse of what is going on underneath their running Java programs. This month's article takes a look at the garbage-collected heap of the Java virtual machine (JVM).

The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.

The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.

In addition to freeing unreferenced objects, a garbage collector may also combat heap fragmentation. Heap fragmentation occurs through the course of normal program execution. New objects are allocated, and unreferenced objects are freed such that free blocks of heap memory are left in between blocks occupied by live objects. Requests to allocate new objects may have to be filled by extending the size of the heap even though there is enough total unused space in the existing heap. This will happen if there is not enough contiguous free heap space available into which the new object will fit. On a virtual memory system, the extra paging required to service an ever growing heap can degrade the performance of the executing program.

This article does not describe an official Java garbage-collected heap, because none exists. The JVM specification says only that the heap of the Java virtual machine must be garbage collected. The specification does not define how the garbage collector must work. The designer of each JVM must decide how to implement the garbage-collected heap. This article describes various garbage collection techniques that have been developed and demonstrates a particular garbage collection technique in an applet.

Why garbage collection?

Garbage collection relieves programmers from the burden of freeing allocated memory. Knowing when to explicitly free allocated memory can be very tricky. Giving this job to the JVM has several advantages. First, it can make programmers more productive. When programming in non-garbage-collected languages the programmer can spend many late hours (or days or weeks) chasing down an elusive memory problem. When programming in Java the programmer can use that time more advantageously by getting ahead of schedule or simply going home to have a life.

A second advantage of garbage collection is that it helps ensure program integrity. Garbage collection is an important part of Java's security strategy. Java programmers are unable to accidentally (or purposely) crash the JVM by incorrectly freeing memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The JVM has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.

Fortunately, very good garbage collection algorithms have been developed, and adequate performance can be achieved for all but the most demanding of applications. Because Java's garbage collector runs in its own thread, it will, in most cases, run transparently alongside the execution of the program. Plus, if a programmer really wants to explicitly request a garbage collection at some point, System.gc() or Runtime.gc() can be invoked, which will fire off a garbage collection at that time.

The Java programmer must keep in mind that it is the garbage collector that runs finalizers on objects. Because it is not generally possible to predict exactly when unreferenced objects will be garbage collected, it is not possible to predict when object finalizers will be run. Java programmers, therefore, should avoid writing code for which program correctness depends upon the timely finalization of objects. For example, if a finalizer of an unreferenced object releases a resource that is needed again later by the program, the resource will not be made available until after the garbage collector has run the object finalizer. If the program needs the resource before the garbage collector has gotten around to finalizing the unreferenced object, the program is out of luck.

Page 2 of 4 : Garbage collection algorithms

Garbage collection algorithms

A great deal of work has been done in the area of garbage collection algorithms. Many different techniques have been developed that could be applied to a JVM. The garbage-collected heap is one area in which JVM designers can strive to make their JVM better than the competition's.

Any garbage collection algorithm must do two basic things. First, it must detect garbage objects. Second, it must reclaim the heap space used by the garbage objects and make it available to the program. Garbage detection is ordinarily accomplished by defining a set of roots and determining reachability from the roots. An object is reachable if there is some path of references from the roots by which the executing program can access the object. The roots are always accessible to the program. Any objects that are reachable from the roots are considered live. Objects that are not reachable are considered garbage, because they can no longer affect the future course of program execution.

In a JVM the root set is implementation dependent but would always include any object references in the local variables. In the JVM, all objects reside on the heap. The local variables reside on the Java stack, and each thread of execution has its own stack. Each local variable is either an object reference or a primitive type, such as int, char, or float. Therefore the roots of any JVM garbage-collected heap will include every object reference on every thread's stack. Another source of roots are any object references, such as strings, in the constant pool of loaded classes. The constant pool of a loaded class may refer to strings stored on the heap, such as the class name, superclass name, superinterface names, field names, field signatures, method names, and method signatures.

Any object referred to by a root is reachable and is therefore a live object. Additionally, any objects referred to by a live object are also reachable. The program is able to access any reachable objects, so these objects must remain on the heap. Any objects that are not reachable can be garbage collected because there is no way for the program to access them.

The JVM can be implemented such that the garbage collector knows the difference between a genuine object reference and a primitive type (for example, an int) that appears to be a valid object reference. However, some garbage collectors may choose not to distinguish between genuine object references and look-alikes. Such garbage collectors are called conservative because they may not always free every unreferenced object. Sometimes a garbage object will be wrongly considered to be live by a conservative collector, because an object reference look-alike refered to it. Conservative collectors trade off an increase in garbage collection speed for occasionally not freeing some actual garbage.

Two basic approaches to distinguishing live objects from garbage are reference counting and tracing. Reference counting garbage collectors distinguish live objects from garbage objects by keeping a count for each object on the heap. The count keeps track of the number of references to that object. Tracing garbage collectors, on the other hand, actually trace out the graph of references starting with the root nodes. Objects that are encountered during the trace are marked in some way. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.

Reference counting collectors Reference counting was an early garbage collection strategy; here a reference count is maintained for each object. When an object is first created its reference count is set to one. When any other object or root is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented. Any object with a reference count of zero can be garbage collected. When an object is garbage collected, any objects that it refers to has their reference counts decremented. In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

An advantage of this scheme is that it can run in small chunks of time closely interwoven with the execution of the program. This characteristic makes it particularly suitable for real-time environments where the program can't be interrupted for very long. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another, for example, a parent object that has a reference to its child object, which has a reference back to its parent. These objects will never have a reference count of zero even though they may be unreachable by the roots of the executing program. Another disadvantage is the overhead(开销) of incrementing and decrementing the reference count each time. Because of these disadvantages, reference counting currently is out of favor. It is more likely that the JVMs you encounter in the real world will use a tracing algorithm in their garbage-collected heaps.

Page 3 of 4 : Tracing collectors

Tracing collectors

Tracing garbage collectors trace out the graph of object references starting with the root nodes. Objects that are encountered during the trace are marked in some way. Marking is generally done by either setting flags in the objects themselves or by setting flags in a separate bitmap. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.

The basic tracing algorithm is called mark and sweep. This name refers to the two phases of the garbage collection process. In the mark phase, the garbage collector traverses the tree of references and marks each object it encounters. In the sweep phase unmarked objects are freed, and the resulting memory is made available to the executing program. In the JVM the sweep phase must include finalization of objects.

Some Java objects have finalizers, others do not. Objects with finalizers that are left unmarked after the sweep phase must be finalized before they are freed. Unmarked objects without finalizers may be freed immediately unless referred to by an unmarked finalizable object. All objects referred to by a finalizable object must remain on the heap until after the object has been finalized.

Compacting collectors

Garbage collectors of JVMs will likely have a strategy to combat heap fragmentation. Two strategies commonly used by mark and sweep collectors are compacting and copying. Both of these approaches move objects on the fly to reduce heap fragmentation. Compacting collectors slide live objects over free memory space toward one end of the heap. In the process the other end of the heap becomes one large contiguous free area. All references to the moved objects are updated to refer to the new location.

Updating references to moved objects is sometimes made simpler by adding a level of indirection to object references. Instead of referring directly to objects on the heap, object references refer to a table of object handles. The object handles refer to the actual objects on the heap. When an object is moved, only the object handle must be updated with the new location. All references to the object in the executing program will still refer to the updated handle, which did not move. While this approach simplifies the job of heap defragmentation, it adds a performance overhead to every object access.

Copying collectors

Copying garbage collectors move all live objects to a new area. As the objects are moved to the new area, they are placed side by side, thus eliminating any free spaces that may have separated them in the old area. The old area is then known to be all free space. The advantage of this approach is that objects can be copied as they are discovered by the traversal from the root nodes. There are no separate mark and sweep phases. Objects are copied to the new area on the fly, and forwarding pointers are left in their old locations. The forwarding pointers allow objects encountered later in the traversal that refer to already copied objects to know the new location of the copied objects.

A common copying collector is called stop and copy. In this scheme, the heap is divided into two regions. Only one of the two regions is used at any time. Objects are allocated from one of the regions until all the space in that region has been exhausted. At that point program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. When the stop and copy procedure is finished, program execution resumes. Memory will be allocated from the new heap region until it too runs out of space. At that point the program will once again be stopped. The heap will be traversed and live objects will be copied back to the original region. The cost associated with this approach is that twice as much memory is needed for a given amount of heap space because only half of the available memory is used at any time.

Heap Of Fish: a garbage-collected heap in action

The applet below demonstrates a mark and sweep garbage-collected heap that uses compaction. It uses indirect handles to objects instead of direct references to facilitate compaction. It is called Heap Of Fish because the only type of objects stored on the heap for this demonstration are fish objects that are defined as follows:

class YellowFish {

YellowFish myFriend;

}

class BlueFish {

BlueFish myFriend;

YellowFish myLunch;

}

class RedFish {

RedFish myFriend;

BlueFish myLunch;

YellowFish mySnack;

}

As you can see, there are three classes of fish -- red, blue, and yellow. The red fish is the largest as it has three instance variables. The yellow fish, with only one instance variable, is the smallest fish. The blue fish has two instance variables and is therefore medium-sized.

The instance variables of fish objects are references to other fish objects. BlueFish.myLunch, for example, is a reference to a YellowFish object. In this implementation of a garbage-collected heap, a reference to an object occupies four bytes. Therefore, the size of a RedFish object is 12 bytes, the size of a BlueFish object is eight bytes, and the size of a YellowFish object is four bytes.

A big difference between the Heap Of Fish code and the kind of code likely to be found in a real JVM stems from the fact that Java does not have pointers. The heaps of real world JVMs would use pointers where Heap Of Fish uses array indexes. In the sections that follow I describe some of the structure of the Java code that implements the heap in the applet. If you are curious about how the heap is implemented you can consult the source code for the ultimate level of detail. The heap data structures and behavior are implemented in the applet source as class GCHeap.

Swimming fish

Heap Of Fish has five modes, which are selectable via radio buttons at the bottom left of the applet. When the applet starts it is in swim mode. Swim mode is just a gratuitous animation. The animation is vaguely reminiscent of the familiar image of a big fish about to eat a medium-sized fish, which is about to eat a small fish.

The other four modes -- allocate fish, assign references, garbage collect, and compact heap -- allow you to interact with the heap. You can instantiate new fish objects in the allocate fish mode. The new fish objects go on the heap as all Java objects do. In the assign references mode you can build a network of local variables and fish that refer to other fish. In garbage collect mode, a mark and sweep operation will free any unreferenced fish. The compact heap mode allows you to slide heap objects so that they are side by side at one end of the heap, leaving all free memory as one large contiguous block at the other end of the heap.

Allocate fish

The allocate fish mode shows the two parts that make up the heap, the object pool and handle pool. The object pool is a contiguous block of memory from which space is taken for new objects. The object pool is structured as a series of memory blocks. Each memory block has a four-byte header which indicates the length of the memory block and whether it is free. The headers are shown in the applet as black horizontal lines in the object pool.

The object pool in Heap Of Fish is implemented as an array of ints. The first header is always at objectPool[0]. The object pool's series of memory blocks can be traversed by hopping from header to header. Each header gives the length of its memory block, which also reveals where the next header is going to be. The header of the next memory block will be the first int immediately following the current memory block. When a new object is allocated the object pool is traversed until a memory block is encountered with enough space to accommodate the new object. Allocated objects in the object pool are shown as colored bars. YellowFish objects are shown in yellow, BlueFish objects are shown in blue, and RedFish objects are shown in red. Free memory blocks, those that currently contain no fish, are shown in white.

The handle pool in Heap Of Fish is implemented as an array of objects of a class named ObjectHandle. An ObjectHandle contains information about an object, including the vital index into the object pool array. The object pool index functions as a reference to the actual allocated object's instance data in the object pool. The ObjectHandle also reveals information about the class of the fish object. In a real JVM, each allocated object would need to be associated with the information read in from the class file such as the method bytecodes, names of the class, its superclass, any interfaces it implements, its fields, and the type signatures of its methods and fields. In Heap Of Fish, the ObjectHandle associates each allocated object with information such as its class -- whether it is a RedFish, BlueFish, or YellowFish -- and some data used in displaying the fish in the applet user interface.

The handle pool exists to make it easier to defragment the object pool through compaction. References to objects, which can be stored in local variables of a stack or the instance variables of other objects, are not direct indexes into the object pool array. They are instead indexes into the handle pool array. When objects in the object pool are moved for compaction, only the corresponding ObjectHandle must be updated with the object's new object pool array index.

Each handle in the handle pool that refers to a fish object is shown as a horizontal bar painted the same color as the fish to which it refers. A line connects each handle to its fish object in the object pool. Those handles that are not currently in use are drawn in white.

Page 4 of 4 : Assign references

Assign references

The assign references mode allows you to build a network of references between local variables and allocated fish objects. A reference is merely a local or instance variable that contains a valid object reference. There are three local variables which serve as the roots of garbage collection, one for each class of fish. If you do not link any fish to local variables, then all fish will be considered unreachable and freed by the garbage collector.

The assign references mode has three sub-modes -- move fish, link fish, and unlink fish. The sub-mode is selectable via radio buttons at the bottom of the canvas upon which the fish appear. In move fish mode, you can click on a fish and drag it to a new position. You might want to do this so that your links are easier to see or just because you feel like rearranging fish in the sea.

In link fish mode, you can click on a fish or local variable and drag a link to another fish. The fish or local variable you initially drag from will be assigned a reference to the fish you ultimately drop upon. A line will be shown connecting the two items. A line connecting two fish will be drawn between the nose of the fish with the reference to the tail of the referenced fish.

Class YellowFish has only one instance variable, myFriend, which is a reference to a YellowFish object. Therefore, a yellow fish can only be linked to one other yellow fish. When you link two yellow fish the myFriend variable of the "dragged from" fish will be assigned the reference to the "dropped upon" fish. If this action were implemented in Java code, it might look like:

// Fish are allocated somewhere

YellowFish draggedFromFish = new YellowFish();

YellowFish droppedUponFish = new YellowFish();

 

// Sometime later the assignment takes place

draggedFromFish.myFriend = droppedUponFish;

 

Class BlueFish has two instance variables, BlueFish myFriend and YellowFish myLunch, therefore a blue fish can be linked to one blue fish and one yellow fish. Class RedFish has three instance variables, RedFish myFriend, BlueFish myLunch, and RedFish mySnack. Red fish can therefore link to one instantiation of each class of fish.

In unlink fish mode, you can disconnect fish by moving the cursor over the line connecting two fish. When the cursor is over the line, the line will turn black. If you click a black line the reference will be set to null and the line will disappear.

Garbage collect

The garbage collect mode allows you to drive the mark and sweep algorithm. The Step button at the bottom of the canvas takes you through the garbage collection process one step at a time. You can reset the garbage collector at any time by clicking the Reset button. However, once the garbage collector has swept, the freed fish are gone forever. No manner of frantic clicking of the Reset button will bring them back.

The garbage collection process is divided into a mark phase and a sweep phase. During the mark phase, the fish objects on the heap are traversed depth-first starting from the local variables. During the sweep phase, all unmarked fish objects are freed.

At the start of the mark phase, all local variables, fish, and links are shown in white. Each press of the Step button advances the depth-first traversal one more node. The current node of the traversal, either a local variable or a fish, is shown in magenta. As the garbage collector traverses down a branch, fish along the branch are changed from white to gray. Gray indicates the fish has been reached by the traversal, but there may yet be fish further down the branch that have not been reached. Once the terminal node of a branch is reached, the color of the terminal fish is changed to black and the traversal retreats back up the branch. Once all links below a fish have been marked black, that fish is marked black and the traversal returns back the way it came.

At the end of the mark phase, all reachable fish are colored black and any unreachable fish are colored white. The sweep phase then frees the memory occupied by the white fish.

Compact heap

The compact heap mode allows you to move one object at a time to one end of the object pool. Each press of the Slide button will move one object. You can see that only the object instance data in the object pool moves; the handle in the handle pool does not move.

The Heap Of Fish applet allows you to allocate new fish objects, link fish, garbage collect, and compact the heap. These activities can be done in any order as much as you please. By playing around with this applet you should be able to get a good idea how a mark and sweep garbage-collected heap works. There is some text at the bottom of the applet that should help you as you go along. Happy clicking.



jingming 2006-08-01 23:31 发表评论
]]>
JAVA相关概念http://www.aygfsteel.com/jingming/archive/2006/07/28/60560.htmljingmingjingmingFri, 28 Jul 2006 06:20:00 GMThttp://www.aygfsteel.com/jingming/archive/2006/07/28/60560.htmlhttp://www.aygfsteel.com/jingming/comments/60560.htmlhttp://www.aygfsteel.com/jingming/archive/2006/07/28/60560.html#Feedback0http://www.aygfsteel.com/jingming/comments/commentRss/60560.htmlhttp://www.aygfsteel.com/jingming/services/trackbacks/60560.html
  • APIQJava ApplicationProgrammingInterface APIQ应用程序接口)是事先写好的代码, l织到相兛_。例?Applet ?AWT 包包括徏立字体、菜单、按钮的c(CLASSQ,全部的Java API被包含在JavaTM 2 Standard Edition ?
  • AppletQ小应用E序Q是在WEB览器里q行的javaE序。Applets使用GUIQ可能有文本、图象、按钮、声音等?AWT ?SWING 通常被联合用?
  • AWTQAbstract WindowToolkit AWTQ抽象窗口工具集Q是一个包含用来徏立Applet和应用程序需要用到的lgQ比如按钮、菜单、滚动条{)的包QpackageQ?
  • JavaBeansTM JavaBean可重复利用、可互换的Y件组件?JavaBeans可能是简单的象按钮或者是讉K数据库的工具{?
  • JFCQJavaTM Foundation Classes (JFC) JFCQJAVA基础c)集合了GUIlg以及其他能简化开发和展开桌面和Internet/Intranet应用的服务?
  • JNIQJavaTMNative Interface JNIQJAVA本地接口Q是 JDK中JAVA的本地编E接口,JNI允许Java代码操作用其他的高Q推荐)~程语言Q比如C、C++Q编写的应用E序和库?
  • JSPQJavaServerTM Pages 通过在HTML面里嵌入scriptlets (Java~程语言代码) 构造JSPQ?JSP 面处理表单Q完成计, 或者做JAVA语言能做的其他Q何事情?
  • J2EETMQJavaTM 2PlatformEnterpriseEdition J2EEQJAVA2企业版)q_提供一个基于组件设计、开发、集合、展开企业应用的途径。J2EE q_提供了多层、分布式的应用模型,重新利用lg的能力,l一安全的模式以及灵z?的处理控制能力?
  • J2METMQJavaTM 2MicroEdition J2ME QJAVA2_版)API规格ZJ2SETM Q但是被修改成ؓ只能适合某种产品的单一要求。J2ME使JAVAE序应用于电话卡、寻呼机{其他消费品成为可能?
  • J2SEQJavaTM 2StandardEdition J2SEQJAVA2标准版)包括基本~译器、小工具、运行环境、用来开发、运行applets和javaE序 的APIs?
  • JVM QJavaTM VirtualMachine1 JVMQJAVA虚拟机)Q能执行java~译器生的指o的运行环境?能嵌入各U不同的产品Q比如web览器、操作系l)?
  • JDBCTMJavaDataBaseConnect 您可以用JDBCQJAVA数据库互联) API讉K支持JDBC的Q何数据库。J2SE包括JDBC API?
  • JDKTMQJavaDeveloper'sKit JDKQJAVA开发工具集Q由 APIc,Java~译器,JVM解释器。可用来~applets和应用程序当前版本ؓJ2SE?
  • JINITM Jini|络技术M的服务(企业pȝ到炊具到|络Q变q稳和简单。Jini 体系l构让每U服?g讑֤和Y件系l? 自动对话?/div>


  • jingming 2006-07-28 14:20 发表评论
    ]]>
    多项查询条gl合下的SQL语句生成http://www.aygfsteel.com/jingming/archive/2006/06/06/50626.htmljingmingjingmingMon, 05 Jun 2006 17:30:00 GMThttp://www.aygfsteel.com/jingming/archive/2006/06/06/50626.htmlhttp://www.aygfsteel.com/jingming/comments/50626.htmlhttp://www.aygfsteel.com/jingming/archive/2006/06/06/50626.html#Feedback0http://www.aygfsteel.com/jingming/comments/commentRss/50626.htmlhttp://www.aygfsteel.com/jingming/services/trackbacks/50626.html 在作数据库条件查询时有时会遇到几U条件的l合查询Q对于这U情况下的SQL语句的生成一般的Ҏ是不行的。在本文中用Q:三元q算W解册个问题。尽在软g工程里经常有批判q种q算W的使用Q认U运符的用降低了代码的清晰度Q但本h感觉q种Ҏq是值的一提?br />       
    作了一q的WEB应用Q在q里面无疑核心就是数据的出出q进。而在作数据条件查询时Q经怼遇到多项查询条g的组合,对于q种情况下SQL语句的生成经q这么长旉的实践加思考,最l给自己定下了一个规范性的~写Ҏ?/font>

    举例如下Q?/font>

    现有数据库表Q表名:studentQ表内字D如下:IDQGENDERQNAMEQNUMQCLASSID?/font>

    有时会遇到的查询条g会是GENDERQNAMEQNUMQCLASSID的Q意组合,x一Ҏ件用户可以填也可以不填,如果按每一Ҏ件字DNULL  OR  NOT   NULL 来组合的话,会有16U情c过ȝ似条件只是两的情况下一般会用一U比较BC的办法就是根据这几种l合分别生成对应的SQL语句Q但后来遇到一ơ比较郁L情况是查询条件到?个,上种Ҏ的可行性可惌知?/font>

    对于上例可以用如下构造方?JAVA)?/font>

    String sql = " SELECT * FORM student WHERE " +

                         " ID = " + (ID.equals("")?"ID":ID) +

                         " AND GENDER = " + (GENDER.equals("")?"GENDER":GENDER) +

                         " AND NAME = " + (NAME.equals("")?"NAME":NAME) +

                         " AND NUM = " + (NUM.equals("")?"NUM":NUM) +

                         " AND CLASSID = " + (CLASSID.equals("")?"CLASSID":CLASSID);

            在这里用C?:三元q算W,在刚学JAVA的时候老师对这个运只是简单一提没有想到这个运符会在q里l你省这么多ȝ?/font>



    jingming 2006-06-06 01:30 发表评论
    ]]>
    代码命名规则Q-Q部分编E常用单词羃?/title><link>http://www.aygfsteel.com/jingming/archive/2006/06/06/50625.html</link><dc:creator>jingming</dc:creator><author>jingming</author><pubDate>Mon, 05 Jun 2006 17:29:00 GMT</pubDate><guid>http://www.aygfsteel.com/jingming/archive/2006/06/06/50625.html</guid><wfw:comment>http://www.aygfsteel.com/jingming/comments/50625.html</wfw:comment><comments>http://www.aygfsteel.com/jingming/archive/2006/06/06/50625.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jingming/comments/commentRss/50625.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jingming/services/trackbacks/50625.html</trackback:ping><description><![CDATA[ <p class="MsoNormal"> <span style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"> <font size="3">    规则Q较短的单词可通过L“元音”Ş成羃写;较长的单词可取单词的头几个字母Ş成羃写;一些单词有大家公认的羃写?/font> </span> </p> <p class="MsoNormal"> <span style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"> <table cellspacing="1" cellpadding="1" width="200" align="center" summary="" border="1"> <tbody> <tr> <td>完整单词</td> <td> <p>~写</p> </td> </tr> <tr> <td>A</td> <td> </td> </tr> <tr> <td>average</td> <td>avg</td> </tr> <tr> <td>B</td> <td> </td> </tr> <tr> <td>back</td> <td>bk</td> </tr> <tr> <td>background</td> <td>bg</td> </tr> <tr> <td>break</td> <td>brk</td> </tr> <tr> <td>buffer</td> <td>buf</td> </tr> <tr> <td>C</td> <td> </td> </tr> <tr> <td>color</td> <td>cr,clr</td> </tr> <tr> <td>control</td> <td>ctrl</td> </tr> <tr> <td>D</td> <td> </td> </tr> <tr> <td>data</td> <td>dat</td> </tr> <tr> <td>delete</td> <td>del</td> </tr> <tr> <td>document</td> <td>doc</td> </tr> <tr> <td>E</td> <td> </td> </tr> <tr> <td>edit</td> <td>edt</td> </tr> <tr> <td>error</td> <td>err</td> </tr> <tr> <td>escape</td> <td>esc</td> </tr> <tr> <td>F</td> <td> </td> </tr> <tr> <td>flag</td> <td>flg</td> </tr> <tr> <td>form</td> <td>frm</td> </tr> <tr> <td>G</td> <td> </td> </tr> <tr> <td>grid</td> <td>grd</td> </tr> <tr> <td>I</td> <td> </td> </tr> <tr> <td>increment</td> <td>inc</td> </tr> <tr> <td>information</td> <td>info</td> </tr> <tr> <td>initial</td> <td>init</td> </tr> <tr> <td>insert</td> <td>ins</td> </tr> <tr> <td>image</td> <td>img</td> </tr> <tr> <td>L</td> <td> </td> </tr> <tr> <td>lable</td> <td>lab</td> </tr> <tr> <td>length</td> <td>len</td> </tr> <tr> <td>list</td> <td>lst</td> </tr> <tr> <td>library</td> <td>lib</td> </tr> <tr> <td>M</td> <td> </td> </tr> <tr> <td>manager</td> <td>mgr,mngr</td> </tr> <tr> <td>message</td> <td>msg</td> </tr> <tr> <td>O</td> <td> </td> </tr> <tr> <td>Oracle</td> <td>Ora</td> </tr> <tr> <td>P</td> <td> </td> </tr> <tr> <td>panorama</td> <td>pano</td> </tr> <tr> <td>password</td> <td>pwd</td> </tr> <tr> <td>picture</td> <td>pic</td> </tr> <tr> <td>point</td> <td>pt</td> </tr> <tr> <td>position</td> <td>pos</td> </tr> <tr> <td>print</td> <td>prn</td> </tr> <tr> <td>program</td> <td>prg</td> </tr> <tr> <td>S</td> <td> </td> </tr> <tr> <td>server</td> <td>srv</td> </tr> <tr> <td>source</td> <td>src</td> </tr> <tr> <td>statistic</td> <td>stat</td> </tr> <tr> <td>string</td> <td>str</td> </tr> <tr> <td>Sybase</td> <td>Syb</td> </tr> <tr> <td>T</td> <td> </td> </tr> <tr> <td>temp</td> <td>tmp</td> </tr> <tr> <td>text</td> <td>txt</td> </tr> <tr> <td>U</td> <td> </td> </tr> <tr> <td>user</td> <td>usr</td> </tr> <tr> <td>W</td> <td> </td> </tr> <tr> <td>window</td> <td>win,wnd</td> </tr> </tbody> </table> </span> </p> <img src ="http://www.aygfsteel.com/jingming/aggbug/50625.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jingming/" target="_blank">jingming</a> 2006-06-06 01:29 <a href="http://www.aygfsteel.com/jingming/archive/2006/06/06/50625.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Z异或(^)q算的数g换运?/title><link>http://www.aygfsteel.com/jingming/archive/2006/06/06/50624.html</link><dc:creator>jingming</dc:creator><author>jingming</author><pubDate>Mon, 05 Jun 2006 17:28:00 GMT</pubDate><guid>http://www.aygfsteel.com/jingming/archive/2006/06/06/50624.html</guid><wfw:comment>http://www.aygfsteel.com/jingming/comments/50624.html</wfw:comment><comments>http://www.aygfsteel.com/jingming/archive/2006/06/06/50624.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jingming/comments/commentRss/50624.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jingming/services/trackbacks/50624.html</trackback:ping><description><![CDATA[两变量的数g换运是~程中经帔R到的一U运?普遍的运方法是ZW三I间的交换运?q是以牺牲空间ؓ代h.但我们也可以不通过牺牲I间而是以牺牲时间甚至于也不牺牲旉的方法来q行交换q算.<br /><p><font size="3">讄在有两个变量A和B</font></p><p><font size="3">要求不通过W三I间完成变量A和B的数g?/font></p><p><font size="3">首先讨论一下基于第三空间的数g换方?/font></p><p><font size="3">int tmp;</font></p><p><font size="3">tmp = A;</font></p><p><font size="3">A = B;</font></p><p><font size="3">B = tmp;</font></p><p><font size="3">现在我们讨论不通过W三I间的数g换方?/font></p><p><font size="3">A = A ^ B;</font></p><p><font size="3">B = A ^ B;</font></p><p><font size="3">A = A ^ B;</font></p><p><font size="3"><img alt="交换图解" src="http://p.blog.csdn.net/images/p_blog_csdn_net/jingmingblog/swapnum.jpg" _fcksavedurl="http://p.blog.csdn.net/images/p_blog_csdn_net/jingmingblog/swapnum.jpg" /></font></p><p><font size="3">优化后算法如?</font></p><p><font size="3">A ^= B;</font></p><p><font size="3">B ^= A;</font></p><p><font size="3">A ^= B;</font></p><img src ="http://www.aygfsteel.com/jingming/aggbug/50624.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jingming/" target="_blank">jingming</a> 2006-06-06 01:28 <a href="http://www.aygfsteel.com/jingming/archive/2006/06/06/50624.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Z异或Q^Q运进行数g换的可行性分?/title><link>http://www.aygfsteel.com/jingming/archive/2006/06/06/50623.html</link><dc:creator>jingming</dc:creator><author>jingming</author><pubDate>Mon, 05 Jun 2006 17:27:00 GMT</pubDate><guid>http://www.aygfsteel.com/jingming/archive/2006/06/06/50623.html</guid><wfw:comment>http://www.aygfsteel.com/jingming/comments/50623.html</wfw:comment><comments>http://www.aygfsteel.com/jingming/archive/2006/06/06/50623.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jingming/comments/commentRss/50623.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jingming/services/trackbacks/50623.html</trackback:ping><description><![CDATA[ <p> <font size="3">异或Q^Q运本w固有的良好q算性质Q这一性质的存在可以保证信息不丢失Q而因其这U性质使得q种q算在很多环境下q泛的应用?br /><br />Z异或q算可以不用牺牲I间完成两个变量数g换的操作。而在~程实践中一般都会用到第三空间来保存变量Q变量在交换过E中不至于数值改变,在此l称之ؓ信息丢失?/font> </p> <p> <font size="3">Z么异或运可以不借用W三I间但不造成信息丢失呢?</font> </p> <p> <font size="3">q里涉及到异或运的一个良好性质Q在表述q个性质之前我们先看一个几何上的例子?/font> </p> <p> <font size="3">讄有二l空间中两向量AQ。ؓ表示方便令A为轴正方向上的向量Q为Y轴正方向上的向量。现在我们可以利用AQ两向量的向量q算很容易就得到W三个向量EQ即Q Q Q Q Q;q里的向量E昄是位于第一象限中的向量。至此存在三个向量,QQ,而如果AQE中Q一向量信息的丢失,则可以通过另两向量q行q原。现在你应该q个例子中所透露出来的性质?/font> </p> <p> <font size="3">q里的异或运同样也是跟上述例子一栗?/font> </p> <p> <font size="3">卻IEQA^Q;</font> </p> <p> <font size="3">Q=Q^Q;</font> </p> <p> <font size="3">Q=Q^Q;</font> </p> <p> <font size="3">正是异或q算q一良好的性质也成Z密码学的密码~码中一个最基本的运?/font> </p> <p> <font size="3">现有明文Qͼ密钥Q,我们可以通过QͼQ经q一pd的运过E(|换Q变换,UMQ异或运)生成密文Q?/font> </p> <p> <font size="3">对于|换Q变换,UMq种cd的运可以通过它们的逆运进行还原,而异或运亦可以通过上述性质q行q原Q这׃得通过密钥Q,密文Q生成明文P成ؓ可能?/font> </p> <p> <font size="3">卻IQ=Q^Q;</font> </p> <p> <font size="3">Q》PQE^Q;</font> </p> <p> <font size="3">注:在此提到的异或运在密码学中的应用只是ؓ了说明一下异或运性质的应用环境,而真正的密码学中~码q程q比上述要复杂?/font> </p> <img src ="http://www.aygfsteel.com/jingming/aggbug/50623.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jingming/" target="_blank">jingming</a> 2006-06-06 01:27 <a href="http://www.aygfsteel.com/jingming/archive/2006/06/06/50623.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAVA中的递归Ҏ应用http://www.aygfsteel.com/jingming/archive/2006/06/06/50622.htmljingmingjingmingMon, 05 Jun 2006 17:22:00 GMThttp://www.aygfsteel.com/jingming/archive/2006/06/06/50622.htmlhttp://www.aygfsteel.com/jingming/comments/50622.htmlhttp://www.aygfsteel.com/jingming/archive/2006/06/06/50622.html#Feedback0http://www.aygfsteel.com/jingming/comments/commentRss/50622.htmlhttp://www.aygfsteel.com/jingming/services/trackbacks/50622.html在面试时l常会遇C些貌D查数学能力的问题,其时q些问题实际是考查你是否有很清晰的E序设计思想Q即用程序设计的思想来处理现实问题的能力?br />
    之前见到q一个运输问题,问题如下Q?/p>

    现有1辆RQ?0桶aQR自带一油箱Qa容量恰ZҎ定wQ每一桶a可以让R?公里Q而R上每ơ只能带一个aӞ初始条g为a桶和车现在同一赯位置QR上a׃满aQ而a桉的a可以倒到车的油箱Q反之亦可以。问用JAVA实现q个q程Q求R可以用这些a最多跑多远?/p>

    q个题在数学里属于优化类的题目,但是q个题出现在面试的时候,那么个h认ؓq个题的目的不是考查你的数学能力Q加之用JAVA实现Q显然它考查的是E序设计能力。故把这个问题的模型q行抽象化ؓJAVA中的递归问题?/p>

    思\Q可以考虑每次让R用一桶a剩余a向前q送一定距,q样可以用递归Ҏ求解。用JAVA实现如下Q?/p>

     1 int  bucketNum  =   11 ; // 初始沚w题ؓ11Ӟ加a׃油)
     2 int  distance  =   0 ; // 车的q输距离
     3 public   int  transit( int  bucketNum )
     4 {
     5   if (bucketNum == 2 )
     6   {
     7   distance  +=   2 ; // 如仅剩两桶a可以直接跑完
     8    return  distance;
     9  }

    10   else
    11   {
    12   distance  +=   1 / ( 2 * bucketNum  -   3 ); // 车用一桶a把其余a向前q送的距离
    13   distance  +=  transit(bucketNum -- );
    14  }

    15 }


     



    jingming 2006-06-06 01:22 发表评论
    ]]>
    վ֩ģ壺 ͨɽ| | | ̫| | | | ˮ| | ̨| | | | Դ| | ̷| | | ָ| ػ| ƽң| | ׷| | | | | ʡ| | ͭ| ƽ| | Ѱ| | | ǿ| | | ʡ| | ij|