00. Multi-variable declaration and initialization
- Simultaneously declare multiple variables of the same type
String a = "Hello", c = "hello";
int x = 5, y = 5;
01. Variable type
01.0 Floating point types
- The default is double type, if you need to specify float type, you can
float f = 1.0F;
01.1 Type conversion
- If you convert a large type to a small type, you can use coercion, but you will lose precision.
01.2 Reference types
- Note: Classes, Strings, interfaces, and arrays are all reference types.
02. Operators
02.0 Notes on division operations
1/2 = 0
Analysis: Divisions are displayed by largest type. Here both numbers are of type Int, so the decimal will be discarded- Division is not rounded.
1.0/2 = 0.5
or1/2.0 = 0.5
02.1 Notes on assignment operators
- With the assignment operator, the data type does not change
- eg:
byte b1=10; b1 = b1+10;
This will make the mistake of converting a large type to a small type - eg:
b1 += 10;
02.2 Notes on short-circuit operators
- It will be judged according to the result of the first conditional expression whether to execute the second conditional expression
- Verification program: (if j becomes 21, it means that the subsequent judgment has been executed)
int i=10,j=20;
boolean result = (i>10) && (++j>30);
System.out.println(result);
System.out.println(j);
03 Abstract method && abstract class
- Abstract method: a method that is only declared and has no implementation
abstract 返回值类型 方法名(参数)
- Abstract classes: incomplete classes
abstract class 类名
- Abstract classes cannot directly construct objects, that is,
Person p = new Person()
this is not possible for abstract classes. Need to inherit first, in the construction:
class Chinese extends Person{
……}
Chinese p = new Person();
- As long as the class contains abstract methods, the class is an abstract class (to add abstract)
abstract class Person{……}
- A class is an abstract class, and its methods are not necessarily abstract methods.
- The final modified method cannot be overridden, so abstract and final are not modified at the same time
03.0 Example
abstract class Person{
public abstract void eat();
}
class Chinese extends Person{
public void eat(){
Sysout.out.println("使用筷子吃饭")
}
}
Chinese p = new Chinese();
p.eat();
04. Interface
- grammar:
interface 接口名称{规则属性,规则的行为}
- Interfaces are abstract
- Properties are static, behavior is abstract
- Interface and class are two levels of things
- Interfaces can inherit from other interfaces
- The object of the class needs to follow the interface, in java, it is called the implementation (implement). That is: classes need to implement (multiple) interfaces.
04.0 case
interface USBInterface{
}
// 下面的规则符合USBInterface,行为就是powerXXX();
// 接口中的行为powerXXX是抽象的
interface USBSupply extends USBInterface{
public void powerSupply();
}
interface USBReceive extends USBInterface{
public void powerReceive();
}
// 电脑类中有两个USB接口,接口的功能是提供电源
class Computer implements USBSupply{
public USBReceive usb1;
public USBReceive usb2;
public void powerSupply(){
System.out.println("电脑提供能源");
usb1.powerReceive();
usb2.powerReceive();
}
}
// 灯
class Light implements USBReceive{
public void powerReceive(){
System.out.println("电灯接受能源");
}
}
// main中实现
Computer c = new Computer();
Light light = new Light();
c.usb1 = light;
c.powerSupply();
05. Bean specification
- Class requirements must have a no-argument, public constructor
- Attributes must be privatized, and getXXX() and setXXX() methods are provided
06. Array of objects
- Declaration method: type [] variable is:
String[] names
- Array creation: new type [capacity] ie:
new String[3]
String[] names = new String[3];
// 或者声明时,直接赋值
String[] nums = {
"1","3","5"};
07. String class
- Originated from
java.lang.String
, automatically loaded by the system
String name = "这是字符串";
07.0 Comparison
- Exact equality (including case)
a.equals(b)
- equal (regardless of case)
a.equalsIgnoreCase(b)
- Comparison of size:
a.compareTo(b)
07.1 Interception (substring)
String s = "01234 6789";
s.substring(0,3); //截取0-2,不包含3
s.substring(6);//只传一个参数,表示截取到末尾。6-9
07.2 Split
String s = "01234 6789";
String[] s1 = s.split(" ");//按空格来分割字符串
System.out.println(s1.length);
for(String s2:s1){
System.out.println(s2);
}
07.3 Remove spaces (trim)
- This is to remove the leading and trailing spaces of the string
String s = " H e l l o ,mygoodfriend ";
System.out.println(s.trim());
System.out.println("!"+s.trim()+"!");
07.4 Replace (replace, replaceAll)
- Replace all: replace
String s = "Hello World, World Li";
System.out.println(s.replace("World","Java");
- Replace according to your own rules: replaceAll
If I want to replace both World and Li with Java, I will do the following:
String s = "Hello World, World Li";
System.out.println(s.replaceAll("World|Li","Java");
07.5 Case conversion (toLowerCase, toUpperCase)
String s = "Hello mygoodfriend";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
If you only want to capitalize the first u of username, how to do it:
String name = "username";
String s1 = name.substring(0,1);
String s2 = name.substring(1);
System.out.println(s1.toUpperCase() + s2);
07.6 Find (charAt, indexOf)
- charAt: Find the character at the specified position by index
String s = "Hello, my friends";
System.out.println(s.charAt(0));
- indexOf: Find the subscript of the first occurrence of the string
String s = "Hello, my friends";
System.out.println(s.indexOf("my"));
- lastIndexOf: Get the subscript of the last occurrence of the string
String s = "Hello, my friends";
System.out.println(s.lastIndexOf("friends"));
07.7 Judgment contains (contains())
- contains: Determines whether the specified string is contained, and returns a Boolean type
String s = "Hello, my friends";
System.out.println(s.contains("mygood"));
System.out.println(s.contains("friends"));
07.8 Judge empty (isEmpty())
String s = "Hello";
System.out.println(s.isEmpty());
String str = "";
System.out.println(str.isEmpty());
08. StringStringBuilder
- This StringBuilder is to optimize the utilization rate caused by continuous creation of new space during splicing operations.
- StringBuilder builds a string, which is a class that provides a large number of string operations.
StringBuilder s = new StringBuilder();
for(int i=0;i<100;++i){
s.append(i);
}
System.out.println(s.toString());
- The efficiency of the above code is higher than the efficiency of the following
String s = "";
for(int i=0;i<100;++i){
s = s+i;
}
System.out.println(s);
08.0 Add (append())
StringBuilder s = new StringBuilder();
s.append("abc");
08.1 Convert to String (toString())
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.toString());
08.1 Length (length())
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.length());
08.2 Reverse (reverse())
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.reverse());
08.3 Insert (insert())
StringBuilder s = new StringBuilder();
s.append("012");
System.out.println(s.insert(1,"dr")); // 在下标为1的地方插入字符串dr