JAVA- basis

A, Java foundation
  1.JDK and JRE What is the difference?

  A: JRE is the acronym for the Java Runtime Environment, as the name suggests is a java runtime environment, including the java virtual machine, java base class library. It is the use of java language programs needed to run the software environment, is available to, and all of the Java class library class files used by a user wants to run java program, both in the lib directory, and are packaged into jar. Jdk Java Development Kit is an abbreviation, as the name suggests is a java development kit, programmers are required to use java language java Development Kit is available to programmers. JDK includes the JRE, also contains the source code to compile java compiler javac, also it contains a lot of java debugging and analysis tools: jconsole, jvisualvm tools such as software, java program also includes the required documentation and demo examples written procedures . If you need to run java program, simply install the JRE on it. If you need to write java program, you need to install JDK.
  2. What is the difference between == and the equals?

  A: (1): For == basic effect and reference types are different, as follows: Basic type: comparing the values are identical; reference type: a comparison of reference is the same; (2) equals Interpretation on essentially equals ==, but String and Integer, etc. rewrite the equals method, turning it into a comparison value. == basic types, values for comparison, a reference to the type of comparison is that a reference; equals the default case is a reference comparison, but many class overrides the equals method, such as String, Integer, etc. it becomes value comparison, it generally equals the value of the comparison is equality.
  HashCode 3. two objects () are the same, equals () certainly is true, right?

  A: equals () equal to two objects, hashcode () is generally equal to, preferably when rewriting equals () method, override hashcode () method; equals () does not equal two objects, but not prove their hashcode () are not equal. In other words, two objects equals () method is not equal, hashCode () possibly equal. Conversely: hashcode () range, will certainly be able to launch equals () is not so; hashcode () are equal, equals () may be equal or may vary. In the object class, hashCode () method is a native method returns a reference (address value) of an object, the object class of the equals () method reference (address value) of the two objects are compared, if equals () are equal, that the two objects address values are equal, of course, hashcode () will equal a.
  4.final What is the role of java?

   A: with the final effect of the different type of modification (1), or a variable final modification of the properties of the class, whether the attribute is a basic type or a reference type, final role is stored inside the "value" can not be changed variables . This value, the basic types, which put the variable is the real value, such as 1, "abc" and the like. The reference type variable to put the address, so use the final modified reference type variable refers to the address inside it can not be changed, not to say that the contents of the address pointed objects or arrays can not be changed, this must pay attention to. But hope does not refer to objects in the constant changes in the new time, you can use the static keyword. For example: there is a class attribute is the final Person p = new Person ( "name"); then you can not be p
Re-assignment, but the value of p can be changed inside the property, p.setName ( 'newName'); final modified properties may not be assigned when declaring a variable, and once the assignment can not be modified. It can be assigned to the final properties in three places: declaration, initialization block, the configuration process. In short, must be assigned. (2) The method of action of the modified final class: can be inherited, but can not be rewritten after the inheritance. Non-final methods efficiency higher than that defined as final. (3), final modification of the role of class: Class can not be inherited. We do not want others to make changes to the class. If a class is set to final form, all methods in a class is set to final form, but the final class member variable may be defined as non-final or final. (4), in non-final pile member variables, final member variable constant pool is stored in the method area. java java constant pool art in constant pool speaking, generally refers to the runtime constant pool, which is part of the zone method, only one instance of a running jvm constant pool, shared among threads running constant pool. java into memory in the heap and stack memory models, wherein the shared heap memory is a data area between threads, the threads of the stack private memory area. The method also includes stack area and non-area method section, stack includes native method stacks, such as virtual machine stack
  of 5.java Math.round (-1.5) equal to how much?

   A: round () method a number rounded to the nearest integer. Return Value: integer closest to x. Note: For 0.5, the process will be rounded. For example, 3.5 is rounded to 4, and rounded to -3 -3.5.
  6.String belongs to the basic data type?

   A: String instead of the basic data types, Java 1. There are eight basic character: byte (character), char (byte) floating point 2: a float (single-precision), double (double precision) 3. Basic integrity: short, int, long 4. boolean: boolean basic data on the type of reference positions are stored java types: (1) when the basic data types are created on the stack to be divided in a memory, the values stored on the stack directly . All simple data types concept of "reference" does not exist, the value of the data itself is stored on the stack space inside, and the Java language inside eight kinds of data types that are on the memory stack primitive data types are stored directly in memory seed storage models; (2) reference data type when it is created, first to their reference (handle) allocate a block of memory on the stack, and specific information of an object stored on the heap, then the reference stack top pointer to a heap the address of the object. Reference types inherit from the Object class (also a reference type) are in accordance with the Java memory model inside the storage object for data storage, the use of Java memory heap and stack memory to perform this type of data storage, put it simply, "cited" is stored in an ordered stack memory, the value stored in the memory object itself heap; modified basic variable data types - Once assigned immutable reference type - immutable object reference
  7.java strings are operated What kind? What is the difference between them? A: String, StringBuffer, StringBuilder String: final modification, String class methods are return new String. That is, any changes to the String object is not
Affect the original object, to generate a new object will modify the operation of the string. StringBuffer: a method of operating a string are added synchronized, thread-safe. StringBuilder: not thread-safe, need to be modified to manipulate strings in vivo, may be new StringBuilder object, call append, replace, delete and other methods StringBuilder object to modify the string.
  8.String str = "i" and the String str = new String ( "i") the same? A: There is a constant pool java, it can be used to store the string constants. 1 difference between the two string variables created in memory appear to be the creation of a string object, but in memory indeed each have their own ideas. String str1 = "abc"; at compile time, JVM will go to find whether there is a constant pool "abc", if not, to open up a space in the constant pool to store the "abc"; if present, would not have opened up a new space . Then open up a name in the stack memory space for str1, and to store the address value in the constant pool "abc" in. String str2 = new String ( "abc"); to find whether there is "abc" at compile time JVM go constant pool, too, if not present, open up a space to store "abc" in the constant pool. In the run time, by the constructor new String class in a heap memory space, and then the String pool "abc" copy stored in the heap space, opened in the name of the stack is str2 space, storage heap address new out of this String object. In other words, the former initialization time may create an object, an object may not be created; the latter because new keywords, created at least one object in memory, there may be two objects. Characteristics of the String class 2 String class is final modified, can not be inherited. String class is based on the underlying char array. 1 two 3) performance efficiency of the String Class is designed to be immutable (the immutable) class, so that all objects are immutable. E.g:
String str = "hello"; str = str + "world"; (str value can not be modified not only will lead to a new object.) Therefore, when the above-str points to a String object (content of "hello "), then str," + "operator, the original point of the object str has not changed, but in turn points to another str object (" hello world "), the original object is still in memory. It can also be seen frequently on the String object to be modified, it will cause great memory overhead. At this point it should be replaced with StringBuffer or StringBuilder String. The new String () is more unsuitable, because every time you create an object constructor will call a new object on the heap, poor performance and more memory waste. 2) security objects are read-only, so multi-threaded concurrent access there will not be any problems. Because immutable, used to store data is also extremely safe. Introducing question: What is immutable (immutable object), immutable objects What are the benefits, under what circumstances should be used, or more precisely, why Java's String class is set to be immutable type? A: immutable objects, by definition is not changed after you create an object, a typical example is in Java String class. String s = "ABC"; s.toLowerCase (); above s.toLowerCase () does not change the value "ABC", but created a new class of String "abc", then point to the new instance variables s .
With respect to the variable objects, immutable object has many advantages: 1) an immutable object can improve the efficiency and safety of String Pool. If you know an object is immutable, you need to copy the contents of this object, it would not replicate itself but only copy its address, copy the address (usually the size of a pointer) requires very little memory efficiency is very high . For reference both the "ABC" of other variables will not be affected. 2). Immutable objects for multi-thread safe, because in the case of multiple threads at the same time, the value of a variable object is likely to be changed by another process, this will cause unpredictable results, and use immutable objects on this can be avoided. Of course there are other reasons, but the Java String set to be the biggest reason is immutable efficiency and safety.
  Common method 10.String class have those? A: () length of the string length; charAt () interception of a character; GetChars () taken by the plurality of other characters in the received string, where the first parameter is the initial index to intercept the string (int sourceStart ), the second parameter is a 5 index to be taken after the end of the string (int sourceEnd) is actually taken into the subscripts int sourceEnd-1, the third parameter is the received character string (char target []), the last parameter is the received character string starts receiving position; byte b [] = a.getBytes ( ); string into a byte array; char [] b = a.toCharArray ( ); toCharArray () array of characters into a string; if equals () and equalsIgnoreCase () compares two strings are equal, the former case-sensitive, which are not distinguished; startsWith () and endsWith () determines if a string is a specific character at the beginning or end; the toUpperCase () and the toLowerCase () to convert a string uppercase or lowercase; the concat () connecting the two strings; TRIM remove start and end spaces (); the substring () taken string; indexOf () and lastIndexOf () the former is where to find the first occurrence of a character or string, which is the place to find a character or string last occurrence; compareTo () and compareToIgnoreCase () Compares two strings lexicographically the size, the former case-sensitive, which are not distinguished; replace () replace.
  11. abstract class must be abstract way? Answer: It is not necessary there, but there are abstract methods of the class must be abstract class. Extension: (1), after the inheritance abstract class is required to implement all the abstract methods; (2), the abstract class may not instantiate the object; (3), the abstract method can have a method body.
  12. The ordinary classes and abstract classes What are the differences? A: 1, abstract class without abstract methods can also be ordinary methods;
2, abstract methods can not be declared as static, abstract simply declare the method need not achieve, there is no body, there is a common method body; about abstract method can not be declared as static explanation: because static methods belong byte code does not need to be instantiated to run, if the abstract method declared as static, that method is not an abstract method 3, subclasses of the abstract class is the abstract parent class must be implemented unless the abstract class is a subclass; 4, there may be an abstract class constructor, all subclasses default constructors will access the parent class constructor parameter hollow; parent class constructor can not be inherited, can explicitly or implicitly is transfer. 5, the abstract class contains methods must be abstract class; 6, abstract class that can not be instantiated;
  13. abstract class can use the final modification of it? A: No, an abstract class is used for inheritance, final modifications on behalf of non-modifiable, non-inherited.
  14. interfaces and abstract classes What is the difference? A: 1, the method interface defaults to "public abstract", member variables interface defaults to "public static final", access to static member variables of an abstract class can be arbitrary. 2, there may be an abstract class constructor, interface can not have a constructor. 3, abstract class can have an ordinary member variables, the interface is not an ordinary member variables. 4, the interface methods are not methods are abstract i.e. body, but may have non-abstract class abstract conventional method; 5, a subclass can inherit multiple interfaces, but a subclass can inherit an abstract class, an interface can not achieve interface, but the interface can be inherited, and may inherit multiple interfaces, separated by commas.
  15.java the IO stream is divided into several?

  16.BIO, NIO, AIO What is the difference?

  What are the common methods 17.Files?

  to add on: 

      About Integer:

      

Integer i01 = 59; int i02 = 59; Integer i03 = Integer.valueOf(59); Integer i04 = new Integer(59);

  (1): Comparison i01 and i02, Integer i01 = 59 here relates to an automatic packing process, the integer constant 59, to produce a packaged stack reference point and the presence of the memory occupied by an integer constant, then i01 is a reference to an Integer. The int i02 = 59 Because int is a primitive type, so there is no reference to the issue, directly by the compiler will store it in the stack, In other words, i02 itself is 59. So System.out.println (i01 == i02) The result? Herein relates to the
process of unpacking, while the presence of the equal sign as the basic type so compiler will unpack the other side to an int Integer object type, then comparing the numerical size is equal on both sides, it is true.

  (2): If it is Integer i01 = 59; Integer i02 = 59; then the result System.out.println (i01 == i02) is? You might say it is relatively equal numerical size ah, there may be equal on both sides say an object reference, so the comparison is a reference, but also opened up because of different memory space, so it refer to different returns false. But the correct answer is: true look at this issue :: If Integer i01 = 300; Integer i02 = 300; then the result System.out.println (i01 == i02) Is? You may say you are not above the true thing to say, but also how to ask this question, but the answer is false.
In the presence of an internal type Integer class, the main job is to do a data byte integer (-128-127) to the Integer class-pack and the corresponding reference stored in the cache array, such a process zone at Integer open up space for these static variables, static cache arrays while also stored here for the thread to enjoy, which is also known as static cache.    

      (3): Integer i01 = 59 returns a reference pointing to cache data. Then (59) Integer.valueOf performance function is to convert int type Integer, it simply is multi-pack, he was a newly created object? Before using the cache still like it? The use of caching, that would only increase the speed of the program, but also save memory.     

      (4): Integer.valueOf (59) returns a reference to cached objects, and Integer i04 = new Integer (59) is a newly opened space in the heap, the value of both the reference must be different.

  Example for the single mode:

  1, the constructor privatization;

        2, reference privatization instantiated variables;

        3, obtain an instance method total. Divided into two types: lazy mode and starving mode lazy mode: singleton instance refers to the global call to use only the first time will be built. Starving mode: singleton instance refers to the overall time to complete the build class loading. DEMO lazy mode: wherein the private constructor is to prevent instantiated, but that way does not prevent reflections.

  (1) Static factory:

// . Lanhan single embodiment, when the first call type instantiation own     
public  class the Singleton {       
     Private the Singleton () {}       
     Private  static the Singleton SINGLE = null ;       
     // static factory method      
     public  static the Singleton the getInstance () {
             IF (SINGLE == null ) {                  
                  single = new Singleton();
                }             
            return single;
       }
} 
                

(2) Method lock body, since the upper ways unsafe behavior in the case of multi-threaded, so that a lock 

 

public static synchronized Singleton getInstance() {
       if (single == null) {
              single = new Singleton();                
           }
          return single;
 }

(3), above there is efficiency, which is double-checked locking: 

  

public static Singleton getInstance() {   
        if (singleton == null) {
                 synchronized (Singleton.class) {
                    if (singleton == null) {
                       singleton = new Singleton();
                   }
                 }
             }
             return singleton; 
  } 

  Double-checked locking, in a new Singleton (); step, there may be reordering problem, so when multiple threads, will be erroneous results, so

  private static Singleton single = null; volatile keyword needs to be added, reordered effectively prevent occurrence.

  instance = new Singleton (); // line 10

  // can be divided into the following three steps

 

// can be divided into the following three steps 
 . 1 = the allocate Memory (); // allocate memory corresponding to c, the malloc 
 2 ctorInstanc (Memory) // initialize the object 
 . 3 s = Memory // set point s address just assigned

// the above-described three steps may be reordered as 1-3-2,

         

= The allocate Memory. 1 (); // allocate memory corresponding to c in the malloc 
. 3 s = Memory // set the address just assigned points s 
2 ctorInstanc (Memory) // Initialize the object

In this case, runs out in a third step, i.e., the object has not completed initialization. If there is another thread making judgments (if (singleton == null)) when this step will be to determine whether, direct return, but this time has not been initialized singleton object. 4): Class static inner - outer class initial load, static variables are initialized, block static code, static methods, but does not load classes and internal static inner classes.

  4): Class static inner - outer class initial load, static variables are initialized, block static code, static methods, but does not load classes and internal static inner classes. 

public class Singleton {
         private static class LazyHolder {
            private static final Singleton INSTANCE = new Singleton();         } 
        private Singleton (){}
         public static final Singleton getInstance() {
            return LazyHolder.INSTANCE;
        } 
    }
   // starving single embodiment mode, there is no performance advantage in this way: 
  // . Starving formula singleton initialization in the class has its own instance of the   
  public  class Singleton1 {
        Private Singleton1 () {}
        Private  static  Final Singleton1 SINGLE = new new Singleton1 ();       
        // static factory method 
        public  static Singleton1 the getInstance () {
            return SINGLE;   
    }
   } 
Use enumerated type single-mode embodiment, the more compact ways, gratis serialization mechanism, can be prevented absolutely instantiated multiple times, even when the face of complex sequences or reflected supplied: 
public enum EnumSingleton { INSTANCE; public EnumSingleton getInstance(){ return INSTANCE; }}

 

Guess you like

Origin www.cnblogs.com/mayang2465/p/11770574.html