Java Programming Ideas (2)

Source: Internet
Author: User
Tags function definition

HandleWhen creating a handle, a more secure approach is to initialize a handle whenever it is created.

String s = "SDFSD";

A special type is used here, and in general, a more general initialization type must be used for the object.

when you create a handle, you usually use the new keyword to implement it if you want it to connect to a new object.

string s = new String ("asdf");

Data SavingWhen the data saver is running, it is best to keep the data where it is stored, with the following six places to save the data:
(1) Register. This is the fastest save area because it is located in a different place than all other ways of saving: inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, nor can we find any trace of the register in our own program.
(2) stack. resides in the general RAM (random access memory) area, but can be directly supported by its "stack pointer" for processing. If the stack pointer moves down, the new memory is created, and if you move up, the memory is freed. This is a particularly fast, particularly effective way to save data, second only to registers. When you create a program, the Java compiler must know exactly the "length" and "time of existence" of all data saved in the stack. This is because it must generate the appropriate code to move the pointer up and down. This restriction undoubtedly affects the flexibility of the program, so although some Java data is stored in the stack-especially the object handle-the Java object is not placed in it.
(3) heap. A general-purpose memory pool (also in the RAM area) in which Java objects are saved. Unlike stacks, the most appealing aspect of the heap is that the compiler does not have to know how much storage to allocate from the heap, or how long the stored data will stay in the heap. As a result, there is greater flexibility in storing data with heaps. When you require an object to be created, you only need to compile the relevant code with the new command. When the code is executed, the data is saved automatically in the heap. Of course, there is a certain price to be paid to achieve this flexibility: it takes longer to allocate storage space in the heap!
(4) Static storage. "Static" here means "in a fixed position" (albeit also in RAM). During the run of the program, the data that is stored statically is always waiting to be called. The static keyword can be used to indicate that a particular element of an object is static. However, the Java object itself will never be placed in static storage space.
(5) Constant storage. The constant value is usually placed directly inside the program code. It is safe to do so, because they will never change. Some constants need to be strictly protected, so consider placing them in read-only memory (ROM).
(6) Non-RAM storage. If the data is completely independent of a program, the program can still exist when it is not running and outside the control of the program. The two most important examples are "streaming objects" and "fixed objects". For streaming objects, the object becomes a byte stream, which is usually sent to another machine. For fixed objects, objects are saved on disk. Even if the program is aborted, they can still keep their state intact. A particularly useful technique for these types of data stores is that they can exist in other media. Once needed, they can even be restored to normal, RAM-based objects.
High-precision digitalBigInteger and BigDecimal. Although they can be broadly classified as "wrapper" types, they do not have a corresponding "master type".
These two classes have their own special "methods" that correspond to the actions we perform on the main type. That is, what can be done with int or float can be done for BigInteger and BigDecimal. Only method calls must be used and operators cannot be used. In addition, the computational speed is slower due to more involvement. We sacrificed the speed, but in exchange for precision.
BigInteger supports integers of arbitrary precision. That is, we can accurately represent an integer value of any size, while no information is lost during the operation.
The BigDecimal supports fixed-point numbers of arbitrary precision. For example, it can be used for accurate currency calculations.
Scope

most programming languages provide the concept of a scope. For a name defined in the scope, the scope also determines its "visibility" and "time of existence".

Object scope: Java has a special "garbage collector" that finds all objects created with new and identifies which ones are no longer referenced. It then automatically frees the memory occupied by those idle objects so that it can be used by new objects. This means that we don't have to worry about the memory recycling problem at all. Simply create objects, and once they are no longer needed, they will automatically leave. Doing so prevents a programming problem that is common in C + +: "Memory Overflow" caused by programmers forgetting to release memory.
However, this guarantee does not apply to "local" variables-those variables are not fields of a class. So, if you write the following code in a function definition:
int x;
Then X will get some random values (this is the same as C and C + +) and will not initialize to 0 automatically. It is our responsibility to assign an appropriate value before the official use of X. If you forget, you get a compile-time error that tells us that the variable may not have been initialized.
Static keywordTypically, when we create a class, we indicate the appearance and behavior of the object of that class. Unless you create an object of that class with new, you don't actually get anything. The data storage space is not formally generated until new is executed, and the appropriate method can be used. However, in two special cases, the above method is not used. One scenario is to use only one storage area to hold a specific data-no matter how many objects are created, or even if you do not create them at all. Another scenario is that we need a special method that is not associated with any object of this class. That is, even if you do not create an object, you need a method that can be called. To meet these two requirements, you can use the static (static) keyword. Once something is set to static, the data or method will not be associated with any object instances of that class. So although an object of that class has never been created, you can still invoke a static method, or access some static data. Before that, for non-static data and methods, we had to create an object and use that object to access the data or method. This is because non-static data and methods must be aware of the specific objects they operate on. Of course, before formal use, because the static method does not need to create any objects, they cannot simply invoke other members without referencing a named object, thereby directly accessing the non-static member or method (because the non-static Members and methods must be associated with a specific object). For a method, static is an important use to help us invoke that method without having to create an object.
Some shortcuts for printing (display)

The prt () method prints a string; PInt () prints a string first, then an int, and Pflt () prints a string before printing a float. Of course, they end up with System.out.println ().

method overload

so in order for the same method names to be used with different types of arguments, "method overloading" is critical a measure. At the same time, although the method overload is required by the builder, it can be applied to any other method and is very convenient to use. An overloaded method must take a unique list of argument types.

ClearJava can use the garbage collector to reclaim memory occupied by objects that are no longer being used. Now consider a very special and rare situation. Suppose our object is assigned a "special" memory area, not using new. The garbage collector knows only to release the memory allocated by new, so it does not know how to dispose of the object's "special" memory. For
To solve this problem, Java provides a method called Finalize (), which defines it for our class. Ideally, it should work like this: Once the garbage collector is ready to release the storage space occupied by the object, it calls Finalize () first, and the memory of the object is actually reclaimed only during the next garbage collection process. So if you use Finalize (), you can do some important cleanup or cleaning work during garbage collection.
in Java, the garbage collector automatically frees memory for all objects, so the equivalent cleanup method in Java is not always needed. If you don't need a builder-like behavior, Java's garbage collector can greatly simplify programming and increase security during memory management. Some garbage collectors can even erase other resources, shapes and file handles, and so on. However, the garbage collector does increase the run-time overhead. But it's hard to see how much of this overhead is going to happen, because the overall performance of the Java interpreter is still slow so far.
the creation process of an object(1) When an object of type dog is first created, or when the static method/static field of the dog class is first accessed, the Java interpreter must find Dog.class (search in the pre-set classpath).
(2) After Dog.class is found (it will create a class object, which will be learned later), all of its static initialization modules will run. Therefore, static initialization occurs only once-when the Class object is first loaded.
(3) When creating a new dog (), the build process of the dog object will first allocate enough storage space for a dog object in the heap.
(4) This storage space is cleared to zero, and all the basic types in Dog are set to their default values (0 for numbers, and for Boolean and char equivalent settings).
(5) All initialization occurs when the field definition is performed.
(6) executes the builder. This may actually require a considerable amount of action, especially when it comes to inheritance.

Java Programming Ideas (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.