Java from entry to the master essays 1

1.java main method must be declared as public static void. String args [] is an array of type string, parameter main () method.

2. The basic data types
Java There are eight basic data types: numeric (integer type (byte, short, int, long ), floating point type (float, double)), character, Boolean.

3. Type Integer
byte: 8 ~ bit 127 -128
Short: 16 bit 32767 ~ ~ -32768
int: 2147483647 32 ~ bit -2147483648
Long: 64 bit

4. The float type
single-precision float: 32 bit declarations need to add the data F or f in the
double-precision double: 64 bit

The character type
char: 16 bits to store a single character

6. Boolean
to true
false

7. The identifier of
the identifier can be simply understood as a name that identifies the class name, valid character sequence variable names, method names, array name, file name.

8. declare variables

9. declare a constant
amount in the course of the program has been run will not change to become constant, often referred to as "final variable." Constant throughout the program can only be assigned once shared by all objects.
final constant data type name [= value]
constant named uppercase letters are often used.

10. Variable Scope
member variables
variables defined in the class body is referred to as member variables, member variables are valid in the whole class. Class member variables can be divided into two categories: static and instance variables.
var {class
int X = 45; // instance variables
static int y = 90; // static variables (class variables)
}

Local variables
in the variable class defined in the method body are called local variables. Local variables are valid in the current code block.
Val class {public
static int = items. 3; // member variable
public static void main (String args []) {
int = Times. 4; // local variable
  }
}


11. The shift operation (for byte int short long)
data shift operation in binary
<< shift left
>> shift right
>>> unsigned right
a left n-bit number, this number is multiplied by 2 ^ n; a number to the right is to divide this number by 2 ^ n.

12. The ternary operator
conditional expression? Value 1: 2 value of
the condition expression evaluates to true to take the entire word 1, otherwise, the value 2

13.for loop statement
for (expression 1; expression 2; Expression 3)
Expression 1: initialization expression, is responsible for the completion of variable initialization of
expression 2: cycling conditional expressions, Boolean, develop cycling conditions
Expression 3 : after cycling expression, responsible for finishing variable, cycling conditions change.

14. The search string
indexOf (String s) // search location or character string is first found
lastIndexOf (String s) // search for the last occurrence of a character or string position

15. Gets the index position of the character
str.charAt (int index)

16. Comparison string
== compare two strings if the address is the same as
to whether the contents of two strings of the same use equals () method and equalsIgnoreCase () method
to distinguish size compared using equals () when write, equalsIgnoreCase () method to ignore case comparison string length and content.

17.compareTo () method Lexicographically two strings are compared.
The comparison is based on the Unicode value of each character,
str.compareTo (String otherStr)
before if this String object lexicographically parameter string is located, the comparison returns a negative integer; located after returning a positive integer; equal returns 0

18. The case of the string conversion
str.toLowerCase () String to lowercase. If the string does not need to change the character of the original string is returned; otherwise it returns a new string, the original string all the characters to lowercase.
str.toUpperCase () the String converted to uppercase. If the string does not need to change the character of the original string is returned; otherwise it returns a new string, the original string all the characters are converted to uppercase.

19. The array of query
Arrays class binarySearch () method, by dichotomy to search an array to obtain the specified object. This method returns the index of the search elements.

20. Static variables, constants, and methods
modified by the static keyword variables, constants, and methods are called static variables, constants, and methods.
Static members belong to the class of all, different from individual objects, can be used in this class and other classes in the class name and " . " Operator calls a static member.

Provisions in vivo method of local variables can not be declared as static in Java.

21. Compare objects

equal () method String class, which is used to compare the contents of two objects referred to are the same references; '==' operator to compare the two object references address are the same.

 22. The method of rewriting

When overridden superclass method, a method of modifying permission to modify only the range from small to large-scale change. For example modifier dosomething parent class () method is protected, following the dosomething Cheng Houzi class () can only be modified for the public authority can not be private.

When an object instance of subclasses, java compiler will automatically call the parent class constructor with no arguments in the constructor subclass.

23. The method of overloading

Overloads in the same class is allowed to exist in more than one method of the same name, as long as these parameters and a number of different methods can.

24.Java transition issues

Parent class object reference to point to a child, and the child class reference point is not the parent class object.

Handle class object reference assigned directly to the parent class called upward transition (upcasting), upward transition without coercion.

The point subclass object of the parent class reference is assigned to a subclass reference called downward transition (downcasting), to be cast.

public  class Father {
     // doSentence; 
}
 public  class Son the extends Father {
   // doSentence; 
}
 public  static  void main (String args []) { 
    Father F1 = new new Son (); // upward transition, the subclass object Switch the parent object. 
    S1 = Son (Son) F1; // downcast into the parent object subclass object. 
F2 = new new Father Father // ();
// S2 = Son (Son) F2 error, subclass references can not point to the parent class }

 Example:

class Animal{
    public void eat(){
        System.out.println("Animal eat");
    }
}
class Bird extends Animal{
    public void eat(){
        System.out.println("Bird eat");
    }
}

public class test{
    public static void main(String args[]){
        Animal a1 = new Bird();
        a1.eat();
        System.out.println();
        Bird b1 = (Bird)a1;
        b1.eat();
    }
}            

Export

Bird eat

Bird eat

 

Guess you like

Origin www.cnblogs.com/wwttc/p/11758819.html