Trivial knowledge and Java-related

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45613931/article/details/102547354

Trivial knowledge and Java-related


I was a novice, to write blog for self-review, self-summary.
If wrong, please point out heavyweights.


(1) identified by letters, underline, dollar signs and numbers, length is unlimited;
identifier number is not the first character;
keywords are lowercase. Certainly not encountered capitalized keywords.

(2) If the float type, you must add f or F, otherwise the number is double.

(3) Accuracy Data Type: byte short char int long float double
if there are expressions double, double press operation performed;
if the expression is highest precision float, float calculates press;
if the expression is the most accurate long , long press operation performed;
if the accuracy is lower than the expression int, int press operated.

(4) logical operators, also called short-circuit operators: &&, ||

(5) Bitwise Operators: & (and), | (or) ~ (non), must be approximately two integer data.

(6) instanceof is a binary operator. Which is the object of the left, right side is the class.
When the left is the object of the right class or subclass is created when, returns true, otherwise false.

(7) switch (expression) The value of the expression must be byte, short, int, char

(8) member variables: are valid throughout the class, have default values when defining.
Local variables: valid only parameter variables and methods defined in vivo method, no default value.
Class variables: Add a static variable of
a class method: Add a static method

(9) If the name of the names of local variables and member variables of the same, the temporary failure of member variables in this method;
if you want to use hidden member variables in this method, you must use the keyword this;
a local variable only within the method effective, but only became effective after the statement from its location.
At the same time initial value when you declare member variables, but can not have separate assignment in the class body;
local variables must initial value;
class methods can only be called a class method of that class;
class methods can only operate on class variables;
after the class declaration with an object, the object must be created.

(10) a variable parameter public void f (int ... x)
parameters are the same type, x is also the last parameter;
... represents several parameters, do not give the specific name and number of parameters;
like array, there will be x [0 ], x [1] ... x [m-1];
as can enter F (l, 2,3), F (3,4,6,8);
public void F (Double A, int ... X) may be .

(11) A class can put an object as its member variables, if an object is created in such a class, then the object
will have other objects. Or that the object is a combination of several objects together.
I.e., if the object is a combination of the object b, b, then a method can be used.

Note: a class declaration of the two objects have the same reference if both variables will have exactly the same.

The difference (12) members of the class instances members:
① all shared objects class variables;
② class name can directly access class variables; (eg:. Lader the bottom 100 =;)
③ different instance variables different from each other.

Examples of methods and class distinction method (13 is):
① instance object invokes the method;
② the class name can be called class methods, class instance variables methods can not operate.

(14) this may occur in the example of the method and method of construction, but may not appear in the class method.

(15) type of packaging: Byte, Integer, Short, Long , Float, Double, Character
constructor incoming values, such as: Double (double num) Float ( float num) ...
return value therein, such as: doubleValue () floatValue ( ) ...

In Character in:
public static boolean isDigit (char CH) determine if it is a digital
public static boolean isLetter (char ch) determine if it is character

(16) an object array:
As: Student [] stu; // further objects are empty, so at the time of use, should create
stu = new Student [10]; // array contains objects, such as: stu [0 ] = new Student ();

(17) Java does not support multiple inheritance, a subclass can have only one parent. However, a class can have zero or more subclass.

(18) Object is the ancestor of all classes.

(19) member variable or method of a subclass of parent class in the same package, the inherited access permissions remain intact; subclasses of the parent class is not in the same package, the subclass inherits the parent class Protected, Public member variables or method, and access rights remain unchanged.

(20) construction method ① subclass always first call the parent class constructor, and then call their own subclass.
② If the subclass does not explicitly specify a constructor call the parent class constructor is invoked without argument.
③ If no parent class constructor with no arguments, you must explicitly tell a parameterized constructor calls the parent class constructor in the subclass.
④ call the parent class constructor can be specified by the super keyword, and must be in the first sentence.
⑤ When you create a subclass subclass object, not just member variables declared in the subclass is allocated memory, and
all the parent class member variables are also allocated memory space.

(21) when the child and parent class member variable of the same name, parent class is hidden away. If the same name is a method, a subclass definitely want to override this method in order to realize their functions. Then rewrite can hide the parent class method.

(22) rewrite is not allowed to reduce access method, but it can be improved.

(23) super keyword:
① operative with super hidden member variables and methods
② with super can call the constructor of the parent class, subclass does not inherit the parent class constructor. (Certainly can not,
the name is different)
(24-) Final Keywords:
①final can be modified classes, member variables and methods. Representatives constant.
② modified by the final class, member variables and methods can not be inherited, of course, can not be rewritten.

Transformation of the object (25) of an object:
Example: or:
A A; A A;
A new new B = (); new new B B B = ();
A = B;
A is the parent of B. This time the object of the parent class is a subclass of the object on the transformation of the object.
① the transformation of the object member variables and methods can not operate the new sub-class. That operation can only be owned by the parent class.
② If a subclass overrides an instance of a parent class, and when to spend the transformation object calls the instance method, the instance will call the subclass rewritten.

(26) the abstract class:
with modified abstract class is called an abstract class, the modified method is referred to by abstract abstract methods.
① abstract class can have abstract methods, or may not. For only allowed to declare an abstract method, it does not allow to achieve, and does not allow the use of the modified final abstract methods.
② can not use new to create an abstract class object for the class, can only produce its subclasses, create objects by subclasses. The subclass must all abstract methods of the parent class to be achieved. Of course, if not all implement the abstract methods, but this must be defined as an abstract class.
③ subclass of abstract class encapsulates must be standards of conduct, this standard is represented by abstract methods.
④ abstract object class can be declared to be a subclass object on the transformation of the object, call the method overridden by subclasses, that reflect the specific behavior of the given subclass of abstract class according to standards of conduct.
⑤ abstract method can not be non-abstract class.
⑥ abstract method is non-static, ie abstract methods can not be modified by static.
⑦ abstract class constructor can be defined.
⑧ even if the parent class is a concrete subclass of this subclass may be abstract.

(27) oriented programming abstraction: When designing a program, you can use the abstract class, and wherein the abstract method declarations, show its importance in the whole system, the details of the content of its subclasses implement.

Transformation of (28) as well as using object oriented abstraction skilled programming thought to embody the principle of opening and closing programming advocated. That is open for extension, but closed for modification.

(29) Interface:
①Java does not support multiple inheritance, interfaces can compensate for this, a class can implement multiple interfaces.
② with defined interface defines the interface, and the class interface definition is similar to the interface and interface declarations into the body.
Define the interface body contains constant definitions and method in which only the method declarations, does not allow to provide methods.
Such as: the Printable interface {
int MAX = 100;
void the Add ();
}
Note: The interface allows only appear constant and is automatically public static final, i.e. can not defined in detail in the above examples. Interface methods are automatically public abstract, static and can not be modified and the final, because the method interface to be rewritten, as in the abstract class.
③ Interface no constructor method may throw an exception.
④ with a class implements one or more interfaces can declare their own implementations.
Such as: A class the implements Printable, addable {}
⑤ If a class implements an interface, it also means that all methods of the interface class must be rewritten.
⑥ Interface can generate a new interface through inheritance.
⑦ Callback Interface: Interface reference variables can be assigned to the interface declaration of an interface implemented in a class of objects created, then the interface variable can call the class interface methods are overridden. This is similar to the transformation of the object.
Such as:

       interface showMessage{
			void show(String s);
		}
		class TV implements showMessage{
			public void show(String s){
				System.out.println(s);
			}
		}
		public class x{
			public static void main(String args[]){
				showMessage sm;
				sm=new TV();
				sm.show("sss")
			}
		}
注:接口与抽象不同,实现接口的类中重写的方法前必须加public。
	而继承抽象类的子类中的重写方法前可以不加public。
	当然一个类可以继承抽象类,也可以实现接口。
	如:class x extends y implements z{}
⑧使用接口进行程序设计(面向接口设计),主要就是用接口回调。
⑨接口参数:如果一个方法的参数是接口类型,我们就可以把任何实现该接口的类的实例的引用传递给接口参数。
如:
       interface SpeakHello{
			void speakhello();
		}
		class C implements SpeakHello{
			public void speakhello(){
				System.out.println("hello!")
			}
		}
		class kindHello{
			public void look(SpeakHello hello){
				hello.speakhello();
			}
		}
		public class X{
			public static void main(String args[]){
				kindHello kindhello=new kindHello();
				kindhello.look(new C());
			}
		}

(30) Internal class

1. Internal class
①Java support another class declared in a class, this class is called inner classes.
The class is called inner class contains fitted inside the class class (class external).
Class body ② internal class, can not declare static methods and variables.
③ external member variables and methods of the class are still valid within the class.
But if the variables and methods of the same name, you can use this distinction. (Can be the same name)
then the default method of the same name inside the class will visit their, to access the outer class,
use this method: external class method or variable name .this
④ inner class can independently inherit an interface, regardless of external whether classes have inherited,
internal class is not affected. Internal and external type that is relatively independent classes.
⑤ In addition to external classes, other classes can not access the inner class.
⑥ external class must first create an internal class object, then use the inner class object to access internal class
variables and methods
⑦ inner class can access data from external class directly.

2. static inner classes: modified with static inner classes. It is at the same level with the external class, or called a static member outside the class. It can not access non-static members outside of class.

3. Local inner class: class definition block, constructs, scope. It can be modified with the abstract. No modifier classes can only partially inside the final type of local variables defined in the access method.

4. anonymous inner class :( no class name)
① can only be used once, and then create an instance, the class definition immediately disappear.
② must inherit a class (abstract and non-abstract can) or implement an interface.
Of course, if the parent class is an abstract class or implements an interface, it is clear that an anonymous inner class
abstract method in which the need to implement.
③ anonymous inner class can not be an abstract class, because it is after the definition, is the need to create an instance.
④ It does not define a constructor method, but may have all the same parent class constructor.
⑤ code blocks may be defined, for example initialization, but can not define a static block.
⑥ can define new methods and properties (not modified with static), but not explicitly call,
because the amount is created using new "transition object"
⑦ It is a local inner class, so to meet the requirements of the local inner class.

(31) anonymous class: Java allows us to create a class body directly subclass a class
subclass object.
For example: board.showMess (new new OutputAlphabet () {
public void Output () {
(code portions)
}
}
In other words, directly at the time of calling this method, the class definition of a so-called class of course can also be achieved. the interface.

(32) Exception class: when the program is running abnormal, Java runtime environment creates an exception object with the appropriate subclass exception classes Exception and waiting to be processed. Of course, we can inherit the exception class Exception, to achieve our own set of error messages.

(33) asserts: for debugging code phase.
Such as: assert number> 0: "negative results are not";
(34 is) String Class:
①String class java.lang package, this package is introduced by default.
②java the String class is declared as final, that is, non-rewritable.
③ Users can not output String object reference, the output is an object entity.
Method ④ object construction string:
String = S new new String ( "We are Students.");
String S1 = "We are Students.";
// These two methods are not the same.
If: String s1, s2; // String constants
s1 = "hello";
s2 = "hello";
then s1 and s2 have the same references.

⑤String common class constructor:
(char array -> String)
1.String (char a []); // create a character array with a a String object
2.String (char a [], int startIndex, int count) ;
// create a String object extraction of a part of a character.
// parameters startIndex: starting position count: the number of characters taken.

(byte array -> String)
1.String (a byte []); // Create a byte array with a String object
2.String (a byte [], int startIndex, int COUNT);
// extract a portion of the byte create a String object.
// parameters startIndex: starting position count: the number of characters taken.
// an English one byte, a two-byte Chinese

⑥ common methods:
1.public int length ();
2.public Boolean the equals (String S);
3.public Boolean startsWith (String S);
public Boolean endsWith (String S);
before the character (rear) // determines whether conjugated to is the specified sequence s
4.public int the compareTo (String s);
// lexicographically comparison with the size of the character sequence s
public int compareToIgnoreCase (String s);
// case-insensitive comparison
5.public boolean contains (String s) ;
// this character sequence is determined whether the sequence of characters comprising a parameter s
6.public int the indexOf (String s);
// start retrieving from the index position 0, the first occurrence of the recording position of the sequence s
public int indexOf (String s, int StartPoint);
// start exploring from a specified starting point position, recording the first sequence s position appears once
7.public String substring (int StartPoint);
// starting at the specified starting point position, copy s character sequence to the current character
8 .public string trim ();
before and after the current string // remove whitespace

⑦ interconversion basic string data

For example: (String -> basic data types)
int X;
String S = "876";
X = the Integer.parseInt (S);
// Similarly, Byte ... Double class methods can invoke the appropriate class, can be transformed into

Basic data types such :( -> String)
String STR = String.valueOf (12313.9876);
// Similarly, all the basic data types can be converted into String

Part of the code:

    double sum=0,item=0;
	for(String s:args){ //一种输入方法
		try{
			item=Double.parseDouble(s);
			sum=sum+item;
		}catch(NumberFormatException e){
			.....
			//如果输入类型有错则输出自定义的错误信息
		}
	}

⑧ string representation of the object
Object class has a public String toString () method of
the character sequence of the object can be obtained by this method.
Returns the character sequence formats:
a quoted string to create the object names @ object class is represented
as:

public String toString(){
			String x=super.toString();
			return x+"...//某些信息";
		}

⑨ character strings, byte array
(String -> char array)
1.public getChars void (int start, int End, C char [], int offset)
// String object of the character sequence from start to end-1 the array c stored at offset
2.public char [] toCharArray ()
// String object into the array of char, char array returns to a already existing.
The difference between the two methods is that, without a return, there is a return.
such as:

      char []a,c;
      a=new char[4];
      String s="信息信息信息";
      s.getChars(1,5,a,0);
     或:c="信息信息信息".toCharArray();

(String -> byte array)
1.public byte [] the getBytes ();
2.public byte [] the getBytes (String charsetName);
conversion, using the specified byte encoding //
// note the distinction between the difference between the conversion char

For example: byte d [] = "hello" .getBytes ();

⑩ Example: string encryption algorithm

String 加密(String password,String SourceString){
		//password输入密码  SourceString文件内容
		char []p=password.toCharArray();
		int n=p.length;
		char []c=SourceString.toCharArray();
		int m=c.length;
		for(int k=0;k<=m;k++){
			int mima=c[k]+p[k%n];
			c[k]=(char)mima;
		}
		return new String(c);
	}
	String 解密(String password,String SourceString){
		//password输入密码  SourceString文件内容
		char []p=password.toCharArray();
		int n=p.length;
		char []c=SourceString.toCharArray();
		int m=c.length;
		for(int k=0;k<=m;k++){
			int mima=c[k]-p[k%n];
			c[k]=(char)mima;
		}
		return new String(c);
	}

Alternatively the decomposition ⑩① regular expression and a character string
1.public Boolean The matches (String regex);
// this is determined whether the character sequence String object and parameters specified regex regular expression matching

Regular expressions are metacharacter with special meaning, such as \ dcat the \ d,
this is a regular expression similar wording. \ d means any of a number of 0-9.

such as:

             String regax="[159ABC]";
			 String str="测试信息";
			 if(str.matches(regex)){
			 	//如果匹配成功则输出相关信息
			 }

So the meaning here is, "1ABC" "5ABC" "9ABC" three to match.

  1. String replaceAll public (String regex, String Replacement);
    // the regular expression regex, and are replaced by a replacement.

For example: String s = "12hello567bird" .replaceAll ( "[a-zA-Z] +", " Hello");
// that is as long as there letters (case) all replaced with "Hello"
// The output is "567 12 Hello Hello"

  1. String public [] Split (String regex);
    // use regular expressions as regex division marks, and the decomposition of words in the array

For example: String str = "1949 dated 10 years. 1 Day";
String REGEX = "\ D +"; // string as an array of non-division marks
String digitWord [] = str.split (regex );

Then digitWord [0], digitWord [1], digitWord [2] are "1949" "10" "1"

Note: If the prefix String and regex to match a sequence of characters, such as "Year October 1, 1949," because the split will comply with what division mark on the left side of the record. Here it will store four values, rather than in Example 3. One of them is""

(35) StringTokenizer class (in java.util package)
① common constructor
1.StringTokenizer (String s);
// s configuration to a parser, using the default division marks, such as space, line feed, tab, carriage
2 .StringTokenizer (s String, String the delim);
// s configuration to a parser, given division marks

如:StringTokenizer fenxi=new Stringtokenizer(“you//are//welcome","#”);

② Method
1.nextToken ();
// Get language-by-symbol string
2.hasMoreTokens ();
// string is determined whether there are sign language
3.countTokens ();
// Returns a string in the language symbol number

(36) Scanner type: string parsed from data
such as: the NBA String = "Game the I Love";
Scanner Scanner Scanner new new = (the NBA);

① The method of
the form useDelimiter (regex);
character sequence // regular expression matching as division mark
②scanner call next () returns the word successively in the NBA
the hasNext () after determining there is no word
③ If the numeric analytic word, available the nextInt () or nextDouble () instead of next () method

如:String regex="[^0123456789.]+";
    Scanner scanner=new Scanner(cost);
    scanner.useDelimiter(regex);

Note: From now completed and Scanner StringTokenizer class is the same function, that is given a separate mark to get what you want. But StringTokenizer are parsed into StringTokenizer object, so the program will be faster, but will take up more memory, it is proposed to be responsible for small data, responsible for long data Scanner.

(37) StringBuffer class
sequence of characters String object that can not be modified, deleted.
Memory entities StringBuffer class objects can be automatically resized, easy to
store a sequence of characters may change.
① common methods:
the StringBuffer the append (String s);
// s appended to the current object
the StringBuffer the append (n-int);
// int-type data into the String object currently catching added
the StringBuffer the append (Object O);
/ / other types of empathy

char the charAt public (int n);
// get a single character position on the parameter n specifies the
public void setCharAt (int n, char ch);
// character with the corresponding positions Alternatively ch

INSERT the StringBuffer (int index, String STR);
// insert the corresponding character at the specified position

Reverse StringBuffer public ();
// Flip characters

The Delete StringBuffer (int startIndex, int endIndex);
// delete the substring specified area
StringBuffer deleteCharAt (int index);
// delete the specified position of a character

Replace the StringBuffer (int startIndex, int endIndex, String str);
// the specified region by substring alternative str

such as:

		 StringBuffer str=new StringBuffer();
		 str.append("你好");
		 System.out.println(str.length);
		 System.out.println(str.capacity);
		 str.setCharAt(0,'we');
		 str.insert(2,'are');
		 int index=1;
		 str.replace(index,str.length,"你好");

(38) Math, BigInteger, Random class
Math in the java.lang package, in a BigInteger java.math package
Math class contains methods for scientific calculation (method too, to find Baidu)
a BigInteger provides arbitrary-precision integer arithmetic (Method too and more Baidu to find)

Random类例:
	Random random=new Random();
	random.nextInt(100); //返回一个从0到100间的随机整数,包含0不包含100
	//或者random.nextBoolean();
	//其他类型类似

(39) Console categories: text input, but not explicitly command line
in java.io package

比如:Console cons=System.console();
	 char[] pw=cons.readPassword();
	 String password=new String(pw);
	 if(password.equals("密码")){
	 	//输出密码正确相关信息
	 }

(40) Pattern Matcher class with: pattern matching
in the package java.util.regex

比如:String regex="good";
	 Pattern p=Pattern.compile(regex);
	 String input="Hello,good morning";
	 Matcher m=p.m(input);
	 
方法:public boolean find();是否有下一个和regex匹配的子序列
	 public boolean matches():是否完全匹配
	 public boolean lookingAt():是否有和regex匹配的子序列

Guess you like

Origin blog.csdn.net/qq_45613931/article/details/102547354