Java Basic Knowledge Collation

Source: Internet
Author: User

1. Object-oriented features: abstraction, inheritance, encapsulation, polymorphism (image memory: Draw more points-think 10086)

1.1 Abstraction

Ignore what is irrelevant to the current goal in a topic and focus on the aspects that are relevant to the current goal. (That is, the real world of a certain kind of things, extracted from the program code, the abstract is generally called a class or interface). Abstraction consists of two aspects: Data abstraction, Process abstraction.

      • Data abstraction--the characteristic of a class of things in the world--is the properties of an object. Like a bird with wings, feathers, etc. ( properties of a class )
      • Process Abstraction--the act of representing a class of things in the world, is the behavior of an object. For example, a bird can fly, it will be called ( the method of the class )

1.2 Inheritance

A class can be derived from an existing class, which is called Class inheritance, the new class inherits the attributes of the original class, the new class is called the derived class ( subclass ) of the original class, and the original class is called the base class of the new Class (the parent class ). Subclasses can inherit methods and variables from the parent class, note here that the constructor of the parent class is not inherited! Subclasses can add their own methods and variables, as well as the methods and properties of the parent class, and subclasses can override the methods of the parent class;

1.3 Package

is to encapsulate the specific methods and attributes of a class, and to use private privatization to access methods or variables by means similar to Get/set, the most common is javabean;

1.4 polymorphic

Polymorphism means that objects of different classes are allowed to respond to the same message. Polymorphism includes parameterized polymorphism ( overloading ) and inclusion polymorphism ( overrides, inheritance, interface implementations ), override of methods, overloading and dynamic linkage to make polymorphism. One of the conceptual reasons for introducing polymorphism in Java is to compensate for the insufficiency of the single inheritance of class.

      • Dynamic linkage--for a method defined in a parent class, if the method is overridden in a subclass, then a reference to the parent class type invokes this method in the subclass, which is the dynamic link
      • Overloading and rewriting---> relatively straightforward way of understanding is that overloading occurs in the same class, the same class, the same name of the different parameters and the constructor/method of the return value type; Overrides occur in different classes, with the same method, the same class of return values, different methods of implementation;

2. Scope public,private,protected, and differences when not written

Is 3.String the most basic type of data? What are the common methods in string? Can a string be inherited? What is the difference between stirng and Stringbuilder,stringbuffer?

    • No, the underlying data type contains only: Integer (Byte,short,int,long), float (float,double), char (char), Boolean (char), homophonic: conquering the vegetable force (the inevitable result of becoming a great God)

Contains the bytes (one byte contains 8 bits, one is a bits), respectively:

Byte-1;short-2;int-4;long-8, (Very good memory: stupid dead, respectively is 1*2*2*2)

float-4;double-8;

Char-2~4 (This encoding method is concerned, because the language is too many sake);

boolean-generally accounts for 1 bytes, but in the Java virtual machine because of the use of the int type instead, so actually accounted for 4 bytes;

  • String Common methods: String more commonly used include two aspects: Construction method, method;
    • Construction Method:
      • String (string value)
      • String (char chars[], int startIndex, int numChars)
      • String (StringBuilder builder)
      • String (StringBuffer buffer)
    • Common methods
      • Length ()//Returns the lengths of the string
      • equalsignorecase (String anotherstring)//is similar to the Equals method but ignores case
      • charAt (int index)//Returns the character at the specified position in the string; Note the first character index in the string is 0, and the last is length ()-1
      • substring (int beginindex, int endIndex)//This method returns the character from the current string to the endIndex-1 position as a new string from the Beginindex position.
      • concat (String str)//connects the string str in the argument to the back of the current string, the effect is equivalent to "+"
      • indexOf (int ch/string str)///used to find characters or substrings in the current string, returns the first occurrence of a character or substring in the current string from the left, and returns 1 if it does not appear
      • toLowerCase ()//Returns a new string that converts all characters in the current string to lowercase, with the corresponding uppercase method: toUpperCase ()
      • Replace (Char OldChar, char Newchar)//Replaces all OldChar characters in the current string with the character Newchar, and returns a new string
      • trim ()//truncate the spaces at both ends of the string, but do not handle the intervening spaces
      • contains (string str)//Determines whether the parameter S is contained in a string and returns a Boolean type value
      • Split (String str)//string decomposition of Str as a delimiter, the decomposed character string returned in the string array
      • Methods for converting a string to a variety of basic types: [Parsexxx] Methods for encapsulating classes of various basic types
      • Convert various basic types into STIRNG methods: various [valueOf] methods in the String class
  • String is the final method and cannot be inherited, but methods that are not part of the STIRNG itself (such as inherited from a parent class) can be overridden, such as ToString (); This is usually useful.
  • In terms of execution speed: stringbuilder>stringbuffer>string
    • Strings are normally bright, StringBuilder and StringBuffer are string variables, and the latter are variables that support modifying the same object, StringBuilder support single-threaded (that is, thread insecure), StringBuffer supports multithreading (thread safety);

4. What is the difference between a basic type and its corresponding package class: (Understand the idea: what , why , what attention points )

    • The basic type is the basic property of the data, and the corresponding encapsulation class is a class that uses its object directly when it is used; The base data type can only be passed by value, while the wrapper class is passed by reference;
    • The advent of encapsulation classes is to make it easier to use methods that are not available in basic types, allowing us to program in an object-oriented manner
    • JDK5.0 start can be automatically sealed, that is, the basic data can be automatically encapsulated into the package class, the basic data type of the benefits of speed (not related to the construction and recycling of objects), the purpose of encapsulation class is to better handle the conversion between the data, many methods, easy to use;

What is the difference between 5.& and &&?

& is a bitwise operator that indicates that the bitwise AND operation,&& are logical operators, indicating that the logic and,&& have short-circuit characteristics

What is the difference between 6.heap and stack? [Belongs to JVM content]

The heap and stack (stack) in Java is a memory storage mechanism, namely heap memory and stack memory, the basic type variables defined in the program and reference variables of objects are stored in the stack memory, and when the scope of the variable is executed, will automatically release this variable corresponding to the memory space, the other to do his use; heap memory mainly stores new objects and arrays, the memory they occupy is not automatically freed, only when no reference variable points to it, it becomes garbage, and at a later time, the GC (garbage collector) is recycled;

Popular point says: Steap is stored in the Java pointer, you can point to the actual data stored in the heap;

7.math.round (11.5) equals how much? How much does Math.Round (-11.5) equal?

The method returns the integer closest to the parameter, Math.Round (11.5) ==12 Math.Round (-11.5) ==-11

8. Does the array have the length () method? Does String have the length () method?

This is a trap, the array is a data structure, not a class, and string is a class, so there is a method length (), the array of such data structure has only the length property;

9. How do I jump out of the current multiple nested loops in Java?

You can use break; note that the normal break can only jump out of a layer of loops, and use the label to jump out of multiple loops; use return to directly connect all of the following programs are not executed, this note!

10. Can the constructor be overload and can be inherited by override? What is the difference between override and overload?

    • The constructor constructor cannot be inherited, so overrid cannot be overridden, but can be overloaded overload
    • The difference between the same class and the different classes, as described in the preceding "polymorphic";

11. Two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence right? [Hashcode's storage mechanism]

No, there's the same hash code.

Conclusion (not quite clear, remember first):

    • If X.equals (y) returns "true", then the Hashcode () of x and Y must be equal
    • If X.equals (y) returns "false", then the Hashcode () of x and Y may be equal or may vary

12. The following two statements return a value of true:
A: "Beijing" = = "Beijing";
B: "Beijing". Equalsignorecase (New String ("Beijing"));

Conclusion: The two Beijing in a and b,a are not new, so point to the same heap address, but the new point in the back B points to a different heap address; = = compares the physical address; equals is worth the size;

13. In Web application development process, we often encounter the output of some encoding characters, such as iso8859-1, how to output a certain encoding of the string?

1  Publicstring Translate (String str) {2String TempStr = "";3 Try{4TempStr =NewString (Str.getbytes ("iso-8859-1"), "GBK");5TempStr =Tempstr.trim ();6}Catch(Exception e) {7 System.err.println (E.getmessage ());8 }9 returnTempStr;Ten}

What is the difference between 14.abstract class and interface? Can I define a static method in an interface?

Understand the abstract method first: It only declares, and there is no specific implementation, that is, the method body. For example: public void fun ();

Abstract class:

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

    • The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public;
    • Abstract classes cannot be used to create objects;
    • If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class;

There is no difference between abstract classes and ordinary classes;

Interface (interface):

An interface can have variables and methods in which variables are implicitly specified as public static final variables, and methods are implicitly specified as public abstract methods and can only be public abstract methods, and all methods cannot have specific implementations If a non-abstract class follows an interface, you must implement all the methods in that interface. But for an abstract class that follows an interface, you can not implement an abstract method () in that interface.

The difference between the two:

    • Abstract classes can have their own implementation methods, but the interface can only have public abstract method;
    • The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
    • A class can inherit only one abstract class, while a class may implement multiple interfaces;
    • At the design level:
      • Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior. Abstract classes are abstractions of the whole class as a whole, including properties and behaviors, but the interface is an abstraction of the local (behavior) of the class.
      • For abstract classes, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface has been changed, all the implementation of this interface class must be changed accordingly;

A static method cannot be defined in an interface: The method that a class implements in an interface is actually a rewrite of the interface method. To implement a static method, you must override the static method in the interface, or if you can define static abstract method (). Is the method of the abstract class A, then because it is a static method that can be used A.method (); And this method is abstract there is no method body, the use of how to execute it?

15.java Q What is not allowed to access non-static methods or variables in a static method?

A static method belongs to a class, whereas a normal method belongs to an object. A static method belonging to a class can be accessed when the object does not exist, and the normal method must first be new to access the object. When we access a static method called (using the class name. static method Name) There is no object creation at this time, so the normal method is not accessible. To avoid this error, Java does not allow access to non-static methods in static methods.

What is the difference between 16.Static inner class (Static nested Class) and inner class (inner class)?

How to use:

1 Static classOuter {2     classInner {}3     Static classStaticinner {}4 }5 6Outer Outer =NewOuter ();7Outer.Inner Inner = Outer.NewInner ();8Outer.staticinner Inner0 =NewOuter.staticinner ();

Static inner classes function: Just to reduce the depth of the package, convenient for the use of classes, static internal classes for the inclusion of classes, but do not rely on external classes, not using the external class of non-static properties and methods, just to facilitate the management of the class structure is defined. When creating a static inner class, no reference to the external class object is required;

The difference between non-static inner classes: You can freely use all the variables and methods of the outer class;

17. What is the difference between a static variable and an instance variable?

Static variables, also known as class variables, are common to all classes, are not dependent on an object, can be accessed directly through the class name, and the instance variable must be dependent on an instance, only through the object to access it

18. When writing the Clone () method, there is usually a line of code, what is it?

What it means to use clone: 52345113

Actual use of clone: http://www.cnblogs.com/o-andy-o/archive/2012/04/06/2434904.html

What is 19.GC? Why do you have a GC? Will the GC have a memory leak?

GC: The garbage collector, used for memory management, is precisely used to manage the heap memory, the object in Java no longer has the concept of "scope", only the object reference has "scope", if there is no index to the corresponding object's memory area, the GC will be at a fixed time memory collection, However, stack memory is recycled by Java based on whether the variables in the code exceed the scope of the variable. Extension: The difference between stack memory and heap memory in front of, and in the extension: research JVM mechanism;

A memory leak occurs in the GC and the corresponding memory cannot be reclaimed when there is no use but an index pointing at him;

20.String s=new string ("xyz"), how many string Object has been created?

Two objects, one is "XyX", the other is a reference object pointing to "XyX" s;

20. Can interfaces be inherited by an interface? is an abstract class achievable (implements) interface? Does an abstract class inherit entity classes (concrete Class)?

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit entity classes, but only if the entity classes must have explicit constructors

21. Say some of the commonly used classes, packages, interfaces, please give 5;

22.Anonymous Inner Class (anonymous inner Class) can extends (inherit) other classes? Is it possible to implements (implement) interface (interface)?

Can inherit other classes or implement other interfaces

23. In Java, a class is declared as a final type, what does that mean?

Indicates that the class cannot be inherited, is a top-level class

Java Basic Knowledge Collation

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.