1. We strongly recommend that you do not use char type
So why is it not recommended to use char type in Java? In fact, 1 Java Char characters are not exactly equal to one Unicode character. The UCS-2 encoding used by char is an obsolete UTF-16 encoding with a maximum of 65536 encodings, much less than today's Unicode has a 110,000-character requirement. Java had to spell 1 Unicode characters with 2 char for the later new Unicode characters. Causes the number of char in string to be equal to the number of Unicode characters.
However, as you all know, char is a fixed-width string type in Oracle (the so-called fixed-length string type), and a blank complement is automatically used if the length is not sufficient. Therefore, in some special queries, there will be some problems, and this problem is very covert, it is difficult to be discovered by developers. Once the problem is found, it means that the data structure needs to be changed, it can be imagined that this is how great a disaster ah.
2. Final Modified constants
With the keyword final indicates a constant, the keyword final indicates that the variable can only be assigned once, once assigned, it can no longer be changed, habitually, the constant name uses all uppercase.
If you often want a constant to be used in more than one method in a class, these constants are often referred to as class constants, using the keyword static final decoration.
3, check whether the string is null or not an empty string
if null && str.length ()! = 0) { System.out.println ("string not NULL"else { System.out.println ("string is empty");}
View Code4. The difference between "= =" and "Equals"
"= =":
1) used to compare value types (int, float, Boolean, etc.)
2) is used to compare reference types and returns True if two objects have the same reference address
"Equals":
1) used to compare reference types, with the same return result as "= =" By default
2) This method is written by the developer and defines the logic itself. For example, the common string class to overwrite this method, after overwrite, even if the reference address of two objects is different, the returned result can still be the same
Public Booleanequals (Object anobject) {if( This==anobject) { return true; } if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =value.length; if(n = =anotherString.value.length) {CharV1[] =value; CharV2[] =Anotherstring.value; inti = 0; while(n--! = 0) { if(V1[i]! =V2[i])return false; I++; } return true; } } return false; }
View Code5, Objects.equals (A, A, b) compares with A.equals
View the source code, you will find that the Objects.equals (A, B) method before comparison, will first check whether a is empty, to avoid the program to report null pointer exceptions
Public Static Boolean equals (Object A, object B) { returnnull && a.equals (b)); }
View Code6, Localdate and date
If you only want to manipulate the date and not the point of time, it is recommended to use the Localdate class instead of the date class, which has more methods of operation, and the latter's method has been gradually discarded.
7. You should use instanceof to check before converting the parent class to subclasses
Object obj = "Test"; if instanceof String) { = (String) obj; System.out.println (str);}
View Code
Try to avoid forcing type conversions, and you can avoid this by re-designing the parent class
8. public class and class
The public class is the meaning of the common classes, which is the access modifier. Java prescribes a class file in which the public is decorated with only one class, and the class name must be the same as the file name of the class, so there can be more than one class in a class file, but a class decorated by public has only one
Note: Protected, private are seldom used to decorate classes
9, public, protected, private modifier
The modification here is mainly to modify methods, fields, etc. in the class.
Before I explain the four keywords, I want to make a simple definition of the relationship between classes, to inherit their class, to think that they are their own children, and for their own directory (the same package path) under the class, think of their own friends.
1, public:public indicates that the data member, the member function is open to all users, all users can directly make the call
2, private:private means private, private means that except class himself, no one can directly use, private property sacred inviolability, even children, friends, can not be used.
3, protected:protected for children, friends, is public, free to use, without any restrictions, and for other external class,protected become private.
Scope |
Current class |
Same package |
Descendant class |
Other Package |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
X |
Private |
√ |
X |
X |
X |
Friendly |
√ |
√ |
X |
X |
Note: Default to friendly when not written
10, integer.valueof () and Integer.parseint ()
Integer.parseint () Converts a string type to an int type
Integer.valueof () Converts a string type to an integer object
11. Formatted output
String message = String.Format ("hello,%s.next year,you ' ll be%d""jyy " - ); System. out . println (message); System. out. printf ("hello,%s.next year,you ' ll is%d""jyy ");
View Code
Conversion, do not need special memory, use, and then to find
12. Switch statement
If there is no break statement at the end of the case branch statement, it is very dangerous to proceed to the next cases statement, and for this reason, the switch statement is never used in our program.
13, indefinite long array-arraylist
//InitializeArraylist<car> ArrayList =NewArraylist<>(); //AddArraylist.add (NewCar (1001, "Mercedes", 200)); Arraylist.add (NewCar (1002, "BMW", 300)); //add in middle positionArraylist.add (1,NewCar (1003, "Porsche", 500)); //ModifyArraylist.set (0,NewCar (1001, "Audi", 400)); //EnquiryCar C = Arraylist.get (0); System.out.println (C.tostring ()); //DeleteArraylist.remove (2); //Query Lengtharraylist.size (); //go to arrayCar[] Cars =Newcar[arraylist.size ()]; Arraylist.toarray (cars);
View Code
The operation of inserting and deleting elements in an array is inefficient. This is not a concern for small arrays. However, if the array stores more elements and often inserts and deletes elements in the middle, you should consider using a linked list to handle them.
Java Basics | Small knowledge point