String class and StringBuffer class
They are all classes of the string, but they have a biggest difference, that is.
EG1:
... // omit some code string S1 = "you are hired!"; system.out.println (s1.replace ('h', 'f')); // use f to put the string h Replaced System.out.Println (S1); ... // omit some code
operation result:
You are Fired! You are Hired!
Result analysis:
From the results, it will be apparent that the value of S1 is not changed, and the first row result is only the replacement of the screen content.
EG2:
... // omit some code stringbuffer s2 = new stringbuffer ("Hello from Java!"); S2.Replace (6, 10, "to"); System.out.Println (S2); ... ... // Omit Some Code
operation result:
Hello to Java!
Result analysis:
Obviously, the value of S2 has changed. 2. Logic and conditional logic
First, declare that in order to distinguish between the logic, I will take some other names that the usual logic is called conditional logic.
They have their own operators, bit logic operators are: & (with operation), ^ (alternative or calculation), | (or calculation); conditional logic operators are: && (and), || (or).
Bit logical operations are typically for two numbers, and the conditional logic is performed for two conditional expressions. In fact, the bit logic operator can achieve conditional operations, but at this time An important difference: When using the operator, regardless of the conditional expression on both sides of the operator is not established, it is not established, and the conditional logic operator is different. If the number of operands through the left can be made It is necessary to determine, then it will not calculate the number of operands on the right side, this is called short circuit. Tone less! And look at the following example.
EG1:
... // Omit Some Code Double Value = 0; IF (Value! = 0 && 1 / Value <1000) {System.Out.println ("The Value IS Not Too SMALL.");} else { System.out.println ("The Value IS TOO SMALL.");} ... // Omit Some Code
operation result:
The value is too small.
Result analysis:
When the sequence, the divisor should have a mistake of 0, but I just said that because the conditional logic operator is a short-circuit operator, obviously, value! = 0 is not established, immediately determine the statement that should be executed after the ELSE, so it No longer judge 1 / Value <1000. If you don't understand, please see another example:
EG2:
... // omit some code double int1 = 0, INT2 = 1, INT3 = 1; if (INT1! = 0 & (INT2 = 2) == 1) {} system.out.println ("INT2 = " INT2); if (INT1! = 0 && (INT3 = 2) == 1) {} system.out.println (" int3 = " int3); ... // omit Some Code
operation result:
INT2 = 2.0 INT3 = 1.0
Result analysis:
I don't think I have to analyze, you should understand it.
3. Example variables and class variables
Data can be stored in classes in two ways - as instance variables and class variables. Instance variables are subject to object, if you have two objects (ie, two instances of a class), instances in each object The variable is independent of the instance variable in another object; on the other hand, both the class variable of the two objects points to the same data, and therefore, the surface saves the same value, in other words, class variables are shared by all objects in the class. Almost forgot, they differ in the form, class variables are more static.eg than instance variables during the declaration:
Class Data {public int INTDATA = 0; // Obviously, INTDATA is here example variable} public class exam {public static void main (string [] args) {data a, b; a = new data (); b = new Data (); a.intdata = 1; system.out.println ("b.indata =" b.intdata);}}
operation result:
B.intData = 0
Result analysis:
It can be seen that the value of a.intdata has changed, but there is no impact of B.intData. But if INTDATA is declared in the DATA class, it will become a class variable in front of it (ie: public static int INTDATA). = 0;), then the result will become:
B.intData = 1
This time the A. Inddata value changes the B.intData, in fact, both the class variables of the objects A and B points to the same data, all values, this is the role of class variables.
4. Example method, class method, constructor method
We usually say that the example method is the example method, just like the function in the C language, I don't have to say the specific method, here I mainly use it to zone classification method and constructor method. Method and instance method The biggest difference is that in the form, you can directly call the class method directly in the usage, you don't have to create an object (and the instance method must first create the object, and then call the object call).
EG:
Class address {static int Addem (int OP1, INTOP2) {RETURN OP1 OP2;}} public class xxf {public static void main (string [] args) {system.out.println ("Addem (2, 2) = " add.addem (2, 2));} // Directly use class name as object call class method}
Note: You can also create objects, then call the method, but then STATIIC is no meaning.
Moreover, it is a way to initialize the data used to initialize the object. It is easy to create, just add a method with the same name as this class, do not need to add any access notes or return to the front. Type, additionally, the constructor can also transmit parameters as the method.
EG:
Class Data {Private String Data1; // Pre-declaration
Data (string s) {data1 = s; / * initializes the variable by receiving data. (Note: Can't declare variables in the constructor, you must declare in advance.) * /}
Public string getdata () {return data1;}}
Public class xxf {public static void main (string [] args) {system.out.println ((New Data ("i love you")). getData ()); / * Create a new object by transferring parameter call constructor, Receive data from the object call method * /}}
5. Interfaces and classes
Category is a specifications for a class of specific objects, we can create objects to create objects, combining all components belonging to this class, while interfaces cannot be done. And the interface is essentially a collection of constants and abstract methods. Using an interface, you need to implement this interface in the class, then as part of the class definition, write each method declared in the interface, the method in the interface is always public, Abstract, the constant in the interface is always public Static and Final, Therefore, it is not necessary to explain the properties for them.
Because multiple inheritors are not supported in Java, similar functions can be implemented with interfaces, this is one of the important roles of the interface. EG:
Interface Anyone // Defines an interface {Final Double Pi = 3.1416; Void SetNumber; Int getNumber ();} interface annother // Defines another interface {void setString (String Str); string getString ()
Class XXF IMPLEMENT Anyone, Anyother // Defines a class and uses two interface {int Number; string str; public xxf () {} void setnumber {this.number = number;} void setString (String Str) {this.Str = Str;} void int getNumber ()} // can be an empty implementation. Void string getString () {}} // All methods declared in the interface must be implemented in the class. (Of course you don't have to But use the adapter or use abstract class)