Advanced technologies for cainiao (I)-analysis of basic java knowledge

Source: Internet
Author: User

Opening Speech: This is the first article in the small series of articles for Java interviews. In this series of articles, all readers are considered as a beginner, introducing Java-related questions from a simple perspective. These problems are all collected by sandezivip and friends, or frequently and important in groups, forums, or networks. Of course, they must be mastered. If you think that you are more than a beginner, but do not quite understand the Several Questions introduced at the beginning of each article, please repeat yourself into the ranks of beginners.

The basic knowledge of Java covers basic object-oriented, operators, control execution processes, and initialization-related content.

Problem:

  1. What are the features of object orientation?
  2. What is the difference between integer and Int?
  3. Short num = 1; num = num + 1; what is the error? Short num = 1; num + = 1; is there an error?
  4. What is the difference between "&" and? And use the most efficient method to calculate 2x8 equals to a few
  5. Differences between static variables and instance variables
  6. What is the difference between equals () and =?
  7. The difference between overloaded and override. Can the overloaded method change the type of the returned value?
  8. What are the differences between fianl, finally, and finalize?
Analysis and answer: What are the features of object orientation?The concept of getting started with Java should be object-oriented. What are the characteristics of object-oriented and what are the benefits of object-oriented programming? When it comes to object-oriented, such problems will surely be met, but I personally think object-oriented is an idea after all, without long-term access to applications and experiences, it is still difficult to use short words to tell you such a problem about object-oriented. However, it is certain that memorizing the characteristics of object-oriented will be of great help to gradually deepen your understanding of object-oriented in the future. Therefore, I still give the official answer in the form of textbooks:

  • Abstract: Abstraction ignores the aspects irrelevant to the current target in a topic, so that you can pay more attention to the aspects related to the current target. Abstraction is not intended to understand all the problems, but to select a part of the problem. Abstract: Process abstraction and data abstraction.
  • Inheritance: inheritance is a hierarchical model that connects classes and allows reuse of encouraging classes. It provides a clear way to express commonalities. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the features 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 (parent class) of the new class ). A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make it more suitable for special needs.
  • Encapsulation: encapsulation is used to enclose processes and data. Data access can only be performed through the defined interface. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface.
  • Polymorphism: allows different types of objects to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. The polymorphism language has the advantages of flexibility, abstraction, behavior sharing, and code sharing, which effectively solves the same name problem of application functions.
After clarifying these concepts, we recommend that you read this article: Seven Essential object-oriented principles for programmers
What is the difference between integer and Int?

Java provides two different types: The reference type and the original type (or the built-in type ). Int Is the original data type of Java, and integer is the encapsulation class provided by Java for int. Java provides encapsulation classes for each original type. The behavior of the reference type and the original type is completely different, and they have different semantics. The reference type and the original type have different features and usage, they include: size and speed problems, which type of data structure is stored, the default value specified when the reference type and original type are used as instance data of a class. The default value of the instance variables referenced by the object is null, and the default value of the original type of instance variables is related to their types. In JSP development, the default integer value is null. Therefore, when an El expression is used to display data in a text box, the value is null, while the int value is 0 by default, so when the El expression is displayed in the text box, the result is 0, so int is not suitable for the form data type of the web layer (see the table below for details ).

Basic Type Big and small Minimum value Maximum Value Encapsulation type
Boolean - - - Boolean
Char 16 bits (2 bytes) Unicode 0 Unicode 2 ^ 16-1 Character
Byte 8 bits (1 byte) -128 127 Byte
Short 16 bits (2 bytes) -2 ^ 15 + 2 ^ 15-1 Short
Int 32 bits (4 bytes) -2 ^ 31 + 2 ^ 31-1 Integer
Long 64 bits (8 bytes) -2 ^ 63 + 2 ^ 63-1 Long
Float 32 bits (4 bytes) Ieee754 Standard Ieee754 Float
Double 64 bits (8 bytes) Ieee754 Ieee754 Double
  • All numeric types have positive and negative signs, so do not look for unsigned numeric types
  • The size of the bucket occupied by the boolean type is not explicitly specified. It is defined as true or false only when the nominal value can be obtained.
Short num = 1; num = num + 1; what is the error? Short num = 1; num + = 1; is there an error?
Short S1 = 1; S1 = S1 + 1; (S1 + 1 is an int type, which requires a forced conversion type to be compiled correctly) Short S1 = 1; s1 + = 1; (can be compiled correctly)
What is the difference between "&" and? And use the most efficient method to calculate 2x8 equals to a few

& And & can both be used as logical operators to represent logic and (and). When the expression structures on both sides of the operator are true, the calculation result is true. Otherwise, if either of them is false, the result is false.

& Is also called short circuit and, that is, if the first expression is false, the second expression is no longer calculated. For example, if (! STR = NULL &&! Str. equals ("") expression. If 'str' is null, the following expression will not be executed, so nullponinterexception will not occur. If & is changed &, a null pointer exception is thrown.

& Can also be used as bitwise operators. When the expressions on both sides of the & operator are not of the boolean type, & indicates bitwise AND operation.

2 <3 should be the most efficient calculation method of 2*8. Because, if a number is shifted to N places, it is equivalent to multiplying the power of N by 2, so a number multiplied by 8 only needs to be shifted to three places. Bit operations (<) are supported by the CPU, and the efficiency is the highest.

What is the difference between static variables and instance variables?

Variables modified with the static keyword are called static variables, because they exist only as the entire class rather than a specific object, so they are also called class variables. Otherwise, variables not modified by static are called instance variables.

Differences between class variables and instance variables:

  1. A static variable has only one storage space for each class, while a non-static variable (instance variable) has a storage space for each object.
  2. Static variables can be located through an object, or they can be directly referenced without their class names. For non-static variables, they can only be referenced through objects.
Let's look at the example below:
Class teststatic {static int I = 5;} create two testsatic objects: teststatic ts1 = new teststatic (); teststatic ts2 = new teststatic (); ts1. I and ts2.1 point to the same bucket, therefore, they have the same value 5. If you want to reference static variable I, you can locate it through an object, like ts1. I; or directly reference it through its class name, such as teststatic. i. This is invalid for non-static variables.

What is the difference between equals () and =?

= The operation compares whether the values of two variables are equal. For a referenced variable, it indicates whether the addresses of the two variables stored in the heap are the same, that is, whether the stack content is the same.

The equals operation indicates whether the two variables are referenced to the same object, that is, whether the content in the heap is the same.

= Compare the addresses of two objects, while equals compares the content of two objects.

For more information, see sandezivip's previous blog:Starting from equals and =

The difference between overloaded and override. Can the overloaded method change the type of the returned value?

Overloaded indicates overload, that is, multiple methods can be created in the class. They have the same method name but have different parameter types. Override indicates rewriting, that is, the subclass defines a method with the same name and parameter list as the base class. In this case, the method in the base class will be hidden.

For more information, see sandezivip's blog:Java method overloading, override, and final
Methods

What are the differences between fianl, finally, and finalize?

Final is used to declare attributes. Methods and classes indicate that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited.

Finally is a part of the structure of the exception handling statement, indicating that it is always executed.

Finalize is a method of the object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file.

PS:

All the articles in this series follow the principle of permanent update, and new things will be added in time, so I hope you will continue to discuss them actively and give us some advice.

Other problems are cumbersome and important.

Entry to the update list of this series of articles: Java interview

This article belongs to the http://blog.csdn.net/sandezivipsearch set. Please refer to the source for any form of transfer or reference!

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.