Some basic concepts of Java

Source: Internet
Author: User
Tags abstract format character set final implement integer variables variable
The concept of a recent look at a book, to understand the basic concepts of Java, in fact, have seen before, but swallowed, it is not guilty. However, to know the new, learning should be the process of understanding the deepening.
The main book is "Java 2 Reference encyclopedia" and "Java 2 Core technology: Principles"

Java Basic Concepts
A Character type
Mainly relive the number of bytes occupied by various character types.
Short int 16-bit 2-byte
int integer 32-bit 4-byte
Long integer 64-bit 8-byte
float and double precision float
Float single precision 32-bit 4-byte
Double double-precision 64-bit 8-byte

Char This is very special, Java is not using ASCII characters, nor the extended 8-bit character set, but the Unicode character set, Unicode characters occupy 16 bits, 2 bytes, can represent all the characters discovered by human, and because so Java can be more convenient for internationalization, I wonder if there is any problem with this understanding.
In fact, the value of char is an integer (0-65536), so we can assign char to a character that is integer or enclosed in single quotes.
By the way, look at the UTF (Unicode text format) format, which is the 16-bit Unicode character
Unicode characters with top nine digits 0: one byte
Unicode characters with top five digits 0: two bytes
Other: three bytes
The corresponding prefix encoding:
0xxxxxxx ASCII Code
10xxxxxx second or third byte
The first byte in a 110xxxxx two-byte sequence
The first byte in a 1110xxxx three-byte sequence

Two Class
Classes and objects: A class is a logical structure in which objects have physical entities. This means that the class does not occupy the memory space and the object occupies a certain amount of memory. This problem can be explained from the acquisition of class objects.
Box abox=new box ();
In fact, the process is two steps.
Box Abox;
Abox=new Box ();
The first step is to create an object that returns Abox as a reference to an object.
The second step is to call the constructor, allocate the memory space for the object, and return the reference to the memory address, which is assigned to Abox.
How does a constructor work?
The constructor does not return a value because its implied return type is the class itself.
The default constructor automatically initializes all instance variables to 0.
An instance variable is a data or variable defined in a class, and a member of a class is a method and a variable defined in a class.

The reference is a very interesting thing.
Box a=new box ();
Box b=new box ();
At this time A and B correspond to their respective memory addresses, if
A=b;
At this point a,b all point to the memory address of the same memory address, B. Which means that now you change the value of the variable of the member variable in a, and then change the value of the member variable in B, and the corresponding variable of a is changed. A reference is just an address of a memory.

A word in a class is called an abstract class, and the abstract class has many meanings and details to note:
1. Classes that contain abstract methods must be declared as abstract classes
2. The method of an abstract class must be overridden in a subclass, or the subclass must also be declared as an abstract class
3. An abstract class cannot have an object and cannot be instantiated by an abstract class
4. Abstract classes can have abstract methods and can be implemented in a specific way
5. Abstract classes can create object references
The fifth article is very useful to achieve polymorphism. The factory method in the design pattern that Li Qiangou's has explained is that the superclass variables of abstract classes refer to different subclass objects.
Because interfaces do not implement their own methods, if a class does not implement the methods in the interface, it must also be defined as an abstract class.

Three Interface
Interface only defines the method name, but does not implement the method.
Interfaces can declare variables, but these variables will be implied, final, static, and cannot be changed by the class that implements the interface. Therefore, you can define shared constants in an interface and import them into multiple classes.
What do you mean by using an interface reference to invoke the implementation?
Inerface Callback ()
{
public void call (int p)
{

}
}

Class Client implements Callback
{
public void call (int p)
{

}

}

public static void (String arg[])
{
<b>callback c=new Client ();</b>
C.call (1);
}

Note that the bold text section above callback C=new client () Creates a client object, but declares it as an interface type, which means that C just knows the method in the interface callback, as for the client class that implements this interface. C Don't know. This is something like superclass referencing subclasses, where it is useful to implement polymorphism, and many classes that implement this interface can be invoked with references to the same interface.

Four. Access control

Five. Meaning and application of some reserved words
Static: A member of a generic decorated class that makes the class member independent of any object of the class, which can be invoked using the class name.
Use Static to note:
The 1.static method can invoke only other static methods
2. Access to static data only
3. This or super can not be invoked in any way. (still don't understand why)

This: You can use this to invoke the current object in any method, this is always a reference to the current object.
This can be used for instance variable shadowing
The so-called instance variable is hidden, which means that when a local variable has the same name as an instance variable, the instance variable is hidden. Like the box constructor.
Box (double awidth,double aheight, double adepth)
{
Width=awidth;
Height=aheight;
depth=adepth;
}
If the argument is width,height,depth, the local variables will have the same name as the class's member variable, and the instance variable will be hidden. But this can avoid this situation.
Box (double width,double height, double depth)
{
This.width=width;
This.height=height;
this.depth=depth;
}
This refers directly to the object, which solves the problem of the namespace, that is, the local variable and the instance variable of the class with the same name. (This need to think again)

Super: There are two main ways to use
1. Super (Parameter_list) calls the superclass's constructor, but this must be the first statement of the subclass constructor. The interesting thing here is how the procedure is when the subclass constructor calls the superclass constructor, and actually calls the constructor in the derivation order of the class.
2. Super.member the superclass's method or instance variable is invoked at this time as a reference to a Super class object

Final: Can modify variables, methods, and classes.
Final variable: The contents of a variable cannot be modified
Final method: Method cannot be overloaded
Final class: class cannot be inherited





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.