Java Fundamentals 1

Source: Internet
Author: User

Phase 0: Draw up a plan Phase 1: What to make? Phase 2: How to build? Phase 3: Start creation Phase 4: Revision phase 5: Planned return first, when the program runs, the data save location1, 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 as required by the compiler. We have no direct control over this, nor can we find a 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 his "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. Do it safely, because they will never change. There are constants that require strict protection, 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 save their state unchanged. 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.   Second, primary data type         main type       size         Wrapper 1, Boolean     1-bit   &NBS P      boolean2, char           16-bit        CHARACTER3, byte           8-bit          BYTE4, short          16-bit        SHORT5, int              32 bit        INTEGER6, lo ng           64-bit        long7, float           32-bit &N Bsp      FLOAT8, double        64 bit       Double If you want a non-primary object in the heap to represent that type of Chu, Use the corresponding wrapper, such as:    char c = ' x ';    Character c = new Character (' C ');    can also be used directly: Character C = new Character (' x ');  high-precision numbers: BigInteger and bigdecimal can do things like int and float, as can be done for BigInteger and BigDecimal. Only method calls must be used and operators cannot be used. Because of the more involved, the operation speed is slower. We sacrificed the speed, but in exchange forPrecision.      biginteger supports integers of arbitrary precision. That is, an integer value of any size can be accurately represented, and no information is lost during the operation.      bigdecimal supports fixed-point numbers with arbitrary precision. For example, it can be used for accurate currency calculations.   Iii. Use of other componentsThe Import keyword tells the Java compiler exactly what class we want.    The purpose of import is to instruct the compiler to import a "package"--or a "class library".    Import Java.util.Vector; Its role is to tell the compiler that we want to use the Java vector class. When util contains a large number of classes, we want to use a few of them, and do not want to declare them all explicitly, you can use the "*" wildcard character import java.util.*; iv. Static KeywordsOnce something is set to the static method, 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. For non-static data and methods, we must 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. In order to set the data member or method to static, you only need to define the predecessor and this keyword. For example, the following code can generate a static data member and initialize it: class statictest{ Static int i = n;      }In addition to using the Create Statictest object to reference a static variable, you can also refer directly to its class name here.    such as statictest.i++; Similar logic applies to static methods, either by referencing a static method as you would any other method, or by referring to a special syntax format, "Class name. Method ()". For example: class staticfun{ static void incr () {statictest.i++;}      }Method One: The typical method calls incr (); staticfun SF = new Staticfun (); sf.incr ();Method Two: staticfun.incr ();For a method, static is an important use to help us invoke that method without having to create an object. v. Pause the outputIn some programming environment, the program will be on the screen all over, and even no chance to see the results, you can put the following code at the end of Main (), with it to pause the output: try{ Thread.currentthead (). Sleep (5*1000); } catch (Interruptexception e) {};     }Its function is to pause the output for 5 seconds. Vi. First Java program //property.java   import java.util.*;Public class property{Public static void Main (string[] args) { System.out.println (New Data ()); Properties p = system.getproperties (); p.list (System.out); System.out.println ("---Memory Usage:"); Runtime RT = Runtime.getruntime (); System.out.println ("Total Memory =" +rt.totalmemory () + "Free Memory =" +rt.freememory ());   } }  At the beginning of each program file. Must place an import statement to import all the additional classes that are used in the code for that file. Note that we say they are "extra" because a special class library is automatically imported into each Java file: Java.lang. Launch your Web browser to view the user documentation provided by Sun. In the packages.html file, you can find all the class library names provided by the Java Companion. Please select the Java.lang. Under "Class Index", you can find a list of all the classes that belong to that library. Since Java.lang enters each Java code file by default, these classes can be used directly at any time. In this list, you can find the system and runtime, and we use them in the Property.java. The data class is not listed in Java.lang, so you must import another class library to use it. If you do not know which class library a particular class is in, or if you want to view all the classes, you can select "Class Hierarchy" (class hierarchy) in the Java user documentation. In a Web browser, although it takes a short time to build this structure, it is clear that each class provided with the Java companion can be found. You can then search for the keyword "Data" using your browser's Find feature. Once this has been done, we can find that our search targets are listed in the form of Java.util.Data. We finally know that it is located in the Util library, so you must import java.util.*, otherwise you cannot use data  to observe the first part of the packages.html document, select Java.lang, and then click System. You can see that the System class has several fields. If you choose out, you know that it is a static PrintStream object. Since it is "static", we do not need to create anything. The Out object must be 3, so just use it directly. What we can do with this out object is determined by its type: PrintStream. PrintStream is listed as a hyperlink in the description text, which is very handy. So if you click that link, you'll see all the methods that can be called for PrintStream. For the time being, we are interested in the freedom of println (). It means "Print the things I gave you to the console and end with a new line". So in any Java program, once you want to print some content to the console, you can System.out.println ("content"), and the class name and file are the same. If you create a standalone program as you do now, a class in the file must have the same name as the file (if you do not, the compiler willRespond in a timely manner). The class must contain a method called Main (), in the form of the following:    public static void Main (string[] args) {}    the keyword "public" means that the method can be called by the external world. The main () argument is an array that contains a string object. Args are not used in this program, but need to be listed in this place because they hold arguments that are called at the command line. The first line of the   program is very interesting: System.out.println (new data)); The only purpose of creating the Data object is to send his value to println (). Once this statement is executed, data is no longer needed. The resulting "garbage collector" will find this out and recycle it whenever possible.   The second line calls System.getproperties (). If you use a Web browser to view online user documentation, you know that GetProperties () is a static method of the System class. Because it is "static", you do not have to create any objects to invoke the method. The static method is ready to use regardless of whether or not an object of the class exists. When GetProperties () is called, it generates system properties as an object of the properties class (note that the property is meant for "attributes"). The subsequent handle is stored in a properties handle called P.   In the third row, you can see that the properties object has a method named list () that sends its entire contents to a PrintStream object that we pass as an argument.   Note To print out multiple string values, separate them with a plus sign (+). When used in a string object, the plus sign does not represent a true "add". When working with strings, we usually don't have to consider any special meaning of "+". However, Java's String class is constrained by a mechanism called operator overloading. That is, the plus sign is only used when accompanied by a string object to produce a different representation from anywhere else. For a string, it means "connect the two strings".   where TotalMemory () and Freememory () return a numeric value, not a string object. If a number is "added" to a string, the compiler automatically converts that value (int,float, etc.) into a string. After such treatment, they can of course use the plus sign to "add" together.

Java Fundamentals 1

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.