package
Two key points:
1. It is usually the first non annotative statement of the class.
2. The domain name can be written backwards, plus the module name, which is convenient for internal management.
matters needing attention
- Add packages when writing projects. Do not use the default package.
- com.go and com go. Car, these two packages are two completely independent individuals, and logically the latter seems to be a part of the former.
Precautions for import
1.java will import Java by default Lang package, so we can use these classes directly.
2. If you import two classes with the same name, you can only use package name + class name to display and call related classes:
java.util.Date date=new java.util.Date();
For example, the following code implements opening other classes in another class:
package cpm.bjsxt.test2; public class User { public void login() { System.out.println("java"); } }
package cpm.bjsxt.test; import cpm.bjsxt.test2.User; public class Test { public static void main(String[] args) { User u=new User(); u.login(); } }
this keyword
Object creation is divided into the following four steps:
1. Allocate the object space and initialize the object member variable to 0 or empty
2. Perform pre initialization of attribute values
3. Execution construction method
4. Return the address of the object to the relevant variable
The essence of this: the address of the created object.
inherit
1. A subclass can inherit the parent class and get all the properties and methods of the parent class (except the construction method of the parent class).
2. If a class is defined without calling extends, its parent class is Java lang.Object.
3.java has only one direct parent class.
For example, the following code enables the Student class to obtain all the attributes of Person through extensions.
package cpm.bjsxt.test; public class Person /*extends Object*/{ int id; String name; public void rest(){ System.out.println("break"); } } class Student extends Person{ int score; public void study(){ System.out.println("hh"+this.name); } Student(int id,String name,int score){ this.id =id; this.name =name; this.score=score; } }
instanceof operator
instanceof operator is a binary operator, with objects on the left and classes on the right; When the object is an object created by the right class or subclass, return true; Otherwise, false is returned.
For example, Student and Person written in the code given above, because Person is the parent class of Student, Student instanceof # Person is true.
toString method
1. tostring () is a method in the object class, and all classes inherit the object class, so all classes can call the tostring method.
2. tostring is to automatically convert non string type to string type
3. When the tostring method is not rewritten, call tostring in object
String equality judgment
Generally, equals() is used to judge whether the string values are equal
Note: equals() needs to consider the case when judging the string, that is, the case is regarded as unequal.
package com.bjsxttest2; import java.util.Scanner; public class leix { public static void main(String[] args) { String str1=new String("abc"); String str2="abc"; System.out.println(str1==str2);//This = = determines whether the objects are equal, so the output result is false System.out.println(str1.equals(str2) ); } }
Definition and initialization of one-dimensional array
When allocating memory for an array using the new keyword:
1. The initial value of each element in the integer {array is 0;
2. The initial value of float double is 0.0;
3. The initial value of character type {is \ u0000;
4. The initial value of Boolean {is false;
int[] array=new int[]{1,2,3,4};// Static array standard allocation
int[] ac={1,2,3,4};// The static array is allocated by ellipsis. Note that ellipsis cannot be divided into two steps
char[] ab;// Static array two-step allocation ab=new char [] {'big', 'small', 'fast', 'happy'};
int[] a=new int[4];// Dynamic initialization of one-dimensional array (standard allocation)
int[] b;// Dynamic array distribution allocation b=new int[4];
Use the keyboard to enter an array of uncertain length:
import java.util.Scanner; public class shuzu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a=sc.nextInt();//Enter array length int[] b = new int[a]; for(int i=0;i<a;i++){ b[i] = sc.nextInt();//Input array } for(int i=0;i<a;i++){ b[i] = sc.nextInt();//Output array } }
Traversal of array
Range: 0~length-1.
Attribute of array length:
1) The return value of length is of type int;
2) The length of an array cannot be defined as a negative number;
3) The value of length is a constant.
for(int i=0;i<a.length;i++) a[i]=i;
Bubble sorting
Sort 3, 1, 6, 2, 9, 0, 7, 4 and 5 from small to large. The code is as follows:
package com.bjsxttest2; import java.util.Arrays; public class leix { public static void main(String[] args) { int[] a = {3, 1, 6, 2, 9, 0, 7, 4, 5}; int temp; for (int i = 0; i < a.length ; i++) for (int j = 0; j < a.length-1-i; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } for (int i = 0; i < a.length; i++) { System.out.printf("%d ", a[i]); } } }