Pole * Java Crash Course - (1)

Preface

As we all know, programmers need to quickly learn new knowledge, so there is a "21 days proficient in C ++" and "MySQL- deleted from the library to the foot," this book, Java as a more "advanced" language should not be backward, so I He wrote a "pole · Java crash Course," so that people who want to learn to quickly learn Java (hand diagonally).
This article probably divided into three parts:

  • Programming Fundamentals
    • Object-oriented thinking
    • type of data
    • Operators
    • Process Control
  • Java language features
  • Java Advanced Features

Please understand this article verbatim text, we only understand and learn Java programming, not as a conceptual reference

This article is a book review read "Java programming ideas" after

Object-oriented thinking

What is object-oriented

Java is a follow on from the beginning of the design object-oriented languages, and therefore has a lot of object-oriented features, then the question is, what is meant by the object-oriented?
Object-oriented, does not mean super-girlfriend's face, but that program when you everything operated are " objects ", if that Java programmers is moving brickwork, Java objects that operation is to move the hands of the brickwork. brick, a brick is an object, turn a pile of a pile of objects, many of the same object can be summed up with a concept, this concept is " class ."
In the first brick we can think is something that is "brick concept (class) ": brick, fired by the soil is made, can be used to walls, floor mats and corner of the table, there is a certain weight and volume, puzzle can be conveyed. Careful analysis of the definition of the phrase can be found can be divided into four small sentence:

  • Bricks are fired by the soil. In other words, a class are inherited evolved from more than one class or another, or " inherit (extend) and another from a combination of one or more class " in Java, all classes inherit from Object and expand class, write their own program, a class can inherit and extend from the official class, and class written by someone else to write their own classes.
  • Brick can be used to walls, floor mats and corner of the table. I.e. the same class can be processed according to different needs, modify the different "shape", add different properties, plays a different role in a different place.
  • Tiles from a given weight and size. Means that each class has its own properties, but for the same attributes, their values ​​are not necessarily the same as the weight and volume of each brick can be different.
  • Bricks can be moved puzzle. Each class has its function is to say, the class attribute is a static description of the function (or method) is described in a dynamic class.

The many "brick" the same object combinations, heaps together, we can build this building houses the program, to achieve the overall function.

Several features in Java classes

The Java class has several features

  1. Single inheritance, meaning that one class can inherit directly from another class, just as brick can only be fired by the soil from that soil → brick, but in addition to soil, brick firing process also need to add other the raw ingredients such as fire, that in addition to direct inheritance, classes can also be added to a class, this method is called inherit through an interface (interface).
  2. Access control. For a class, you can only see a part of your class wants to see. Like a brick, you can only see the surface of the brick, the inside can not see it, unless he knocked, but in Java, which is not allowed. Part of a class and want to see you do not want to let you see part controlled by different keywords:
    • For the public disclosure of all objects
    • private and only visible own content
    • protected only themselves and their children and grandchildren can see the content
    • default only their relatives (same package) you can see the contents
      we do not care what they look like inside, we just need to care about what we need, exposed things and objects can work on the line.
  3. "Like" is "is." For a class, if something looks like, it can be seen as a thing. Like son, like father, his father's son also competent and capable, like a fire turn is a brick, that he has all the attributes of brick, it can also be piled. And a method has piled up the object appears the energy barrier, and that he can be like a brick base up.
  4. static properties. For a class for some properties and methods can be static, it is static, this is not a description of the object properties and methods of a class, but rather a description of the class. For example, statistics on the number of operations can be performed on all the bricks for a brick let him count the number of the same is not appropriate, but for a bunch of turn, make this pile of bricks tell you how many blocks are feasible.
  5. Object life cycle management. Java language users do not care about how Java objects are born, how demise, just new when you need it, you can create an object, where after you are finished using the garbage thrown, that's something to be studied Java Advanced Features .
  6. The Java class package having a top (package) structure, having a package-level, it means such java.lang.Math Math class in the package under lang java, the hierarchy can have several layers, layer by layer depth, so the name may be very long. In use the contents of a certain class of one package, requires the use of import for introducing, if necessary at all to import a package, use wildcards. Such as the java.lang. .

Point Java programming need to be concerned

Just think of the good of this class and its objects should be how to design, how can the normal operation, the burden of other languages, it can get rid of

type of data

Everything is an object, but only a few foundation

When a user needs a Java object that position in memory when we do not control would not be able pipe. But this object is how come, but it is possible to think. Is like a house built by piling bricks, tiles are made of clay firing, and that the soil is what constitutes it, so further pursued, prosecuted and finally to become baryons and leptons, but we do not have to pursue so thin, like Java, we know that all objects have Object "fathers", but we do not need everything to call up the ancestors, it has several children are our common, or "basic types":

Types of size Wrapper The initial value
boolean true/false Boolean false
char 16 bit Character '\ U0000'
byte 8 bit Byte (byte)0
short 16 bit Short (short)0
int 32 bit Integer 0
long 64 bit Long 0L
float 32 bit Float 0.0f
double 64 bit Double 0.0d
void Void void

These basic types of size n bits are related to its minimum value of -2 n-. 1 , a maximum of 2 n. 1- -1, a float, and double follow standard IEEE754.
In addition to basic types, as well as string String, Integer common precision floating-point precision based BigInteger and BigDecimal like.

Objects should be managed

An object, give him a title, you can call him to do all sorts of things based on the name, just as the family can call you to let you work the same nickname. But when out of the home, there may be a lot of people have the same with you, this will bring trouble, so is not allowed, while outside call your nickname, this will bring embarrassment, that is, the name beyond the scope , in Java, a title (or reference) from the scope of this statement named to where to spend the end of the brackets. Of course, if you do not care that there is no problem, because the IDE will remind you of a problem here.
When an object with its call later, we can arrange for him to do many things, no matter who cried, cried as long as you can use. All calls within the scope range can be implemented by this name, and you can also give up this object in another name, this multiple names refer to the same object, when the treatment is not the time, this will bring some issues, such as a person you call John's to give you a view of the painted face, people call you after you call up John Doe will see a painted face.
For a bunch of objects of the same type, you can use a container loaded them live, help us to manage and use, Java provides a variety of types of containers, there is an array of the most basic, is a string of contiguous space to install the same object there are other such set and map such containers can be loaded, it can be discussed in detail later.

Operators

Operators like semantic understanding literally

Operator have priority, there are features of a short circuit. That priority will not continue operation when the first subtraction, multiplication and division, logical operations is a short circuit may be generated when a direct result.
There are several operators:
+. Plus, the digital accumulation, the connection string, a smaller type of digital promoted to type int.
-. Minus, digital subtraction. negative.
*. Multiplication, multiply the number.
/. In addition to numbers, the numbers division, the integer part is given the results, truncating decimal directly, not rounded.
%. Remainder, the digital division remainder number.
++ increment - decrement, when the object appears in front of the first operation to the output value, when the object appears behind the first output value recalculation.
>, <,> =, <=. Greater than, less than, greater than or equal, or less. As the name suggests.
== for equality, for Java, it is easy troubling, because except when basic types of objects are equal, == only to determine whether an alias is pointing to the same object, not the object of interest the properties are equal. To determine if the package good String and other official types, it is necessary to use equals()the method, but for their own definition of the class, if not redefine the equals()way and will == as a reference to the object to judge, rather than the object attribute determination.
&&, ||,!. Or logical operators and, for a boolean value to judge, when both sides && true is true, when one of both sides is true true ||,! Is the object boolean logic value is reversed.
&, |, ^, ~. Bitwise operators, types of digital bitwise AND, OR, XOR and non-operation, and, or, exclusive OR operation on two objects, is a non-operation object.
<< >>, >>>. Shift operator, operand is shifted bitwise, A ※※ B is to shift the direction of arrow A to B bits. If it is negative, >>
=. Number assigned to an object or aliases, or by adding an object to another alias.
+ =, - =, * = , / =, & =, | =, ^ =, >> =, >>> =, << =. Comparative Similarly, a ※ = b is a = a ※ b. When >>> = byte and short, then converted to int truncation processing, it may produce incorrect results.
Exponential notation: AeB represents Ax10 B , as the representative float 1.39e-43f of 1.39x10 -43 .
?:. This is a ternary operator, such as A B:? C says that if the logical result A is true, B is executed, otherwise execution C.
Direct constant, is the direct number, such as 200D, 0x200. Rule is that if the latter represents a double with D, F represents a float, L represents a long, the foregoing represents octal with 0, 0x indicates hexadecimal number.

Operational problems prone operator using

Type Conversion

(A) B B can be forced into the type A, if it is converted from a smaller type to a larger type, will not cause problems, but if it is the type of conversion from a larger to a smaller type, It will bring a loss of information. Similarly conversion custom class.

Truncation and rounding

For floating-point numbers, the forced conversion of plastic, discards the fractional part, if you want to get the result of rounding, java.lang.Math the need to use the round()method.

Process Control

Java is an object-oriented language, but it is still sequential execution, the program execution order, does some selection branches and cyclic operation.

select

Java can be used if-else branch and switch selection.

if-else

Shaped like

if(A){
    B
}else{
    C
}

If it is determined that the condition A is true, B is executed, otherwise, execute C.

switch

Shaped like

switch(A){
    case 1: B;
    case 2: C;break;
    case 3: D;break;
    case 4: E;break;
···
}

I.e., if the determination condition A is 1, B is executed and then interrupting the execution of code block C, if the judgment condition 2 is then executed to interrupt the block C, if the determination condition 3 is then executed to interrupt the block D, and the like .

cycle

There are three forms loop: while, do-while and for.

while

Shaped like

while(A){
    B
}
对判断条件A进行判断,当判断条件为A时,多次执行B代码。

do-while

Shaped like

do{
    B
}while(A)

To perform a B code and the determination of the determination condition A, it is determined when the condition A is true, loop B to execute the code braces head.

for

Shaped like

for(int i=0,j=0;i<100;i++,j++){
    B
}

First, the first of ;the front part of the implementation of the common loop control to initialize the object, a plurality of objects can ,be distinguished, then ;loop between the control condition is determined, if the condition is true under the code block is executed, finally ;the The third section performs such circulating operation object increment. Example is the first of the i and j are initialized to 0, and determines whether i is less than 100, if true execute B codes, the final implementation of i is incremented and j increment, and determines whether i is less than 100, if true continue to cycle the code is the last code segment B will be executed 100 times.
When programming, the cycle is usually carried out in an array on iterables like. Iteration can order from front to back is that you can go again. In this regard, Java provides some convenient syntax facilitate development. Shaped like

容器 A;
for(int i:A){
    B
}

That the container A, iterate over again, I will refer to each object A in each cycle, and then execute the code block B. If the object 100 has a container A, B that is executed 100 times.

return

return

The end of the statement where the function returns the return value.
Return Value determined by the function is defined, and if the mismatch error IDE will not be compatible.

Java out of the ordinary

Java process control is no goto statement, but through the break and continue statements to achieve a jump from the deep circulation function. By tag before the loop label:, then break label, and continue label, out of the loop to be out of the specified location or the specified location to the current cycle.

label1:
while(true){
    label2:
    while(true){
        continue label1;//结束本次循环,跳转到外循环继续循环
        break label1://结束外层循环
    }
}

Lament

Java basic grammar, there are similarities with other languages, there are differences, but the programming has always been a practice of skills, paper come Zhongjue, hand line and to know the code, the real deal or just write code knowledge can be converted into power. And, sad to say write the article.

Guess you like

Origin www.cnblogs.com/CoveredWithDust/p/11899457.html