14 wrapper class and String class
Keywords:
Java
Big Data
Wrapper class and String class
Packaging class
Object Class is the parent of all classes
8 Basic data types
jdk5 Before:
Object o = 12;//Not before jdk5
jdk5 The wrapper class then converts the base data type to the reference data type
Classification of packaging
Basic data type |
Packaging type |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
char |
Character |
float |
Float |
double |
Double |
boolean |
Boolean |
Integer wrapper class
Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
Integer integer = new Integer(10);
System.out.println(integer);
System.out.println(integer.toString());
Integer integer2 = Integer.valueOf(20);
System.out.println(integer2);
Integer integer3 = new Integer(250);
int a = integer3.intValue();
System.out.println(a);
String string = "123456";
Integer integer4 = new Integer(string);
System.out.println(integer4);
String string2 = "34521";
Integer b = Integer.valueOf(string2);
System.out.println(b);
int c = 234;
String string3 = c+"";
System.out.println(string3);
String string4 = "345678";
int d = Integer.parseInt(string4);
System.out.println(d);
String string5 = "0234543";
int e = Integer.parseInt(string5);
System.out.println(e);
String string6 = "23 45 67";
int f = Integer.parseInt(string6);
}
}
Automatic packing and unpacking
After jdk5, it provides the functions of automatic unpacking and automatic packing.
Auto boxing: assign a simple data type directly to the packing type
Auto unpacking: assign packing type directly to simple type
Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
//Automatic unpacking
Integer i = new Integer(20);
int a = i;
//Automatic packing
Integer integer = 30;
}
}
Constant pool
Interview questions:
//Constant pool: exists-128----127The values are the values shared by all objects when the value exceeds127 New objects will be created
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
Integer integer1 = 120;
Integer integer2 = 120;
System.out.println(integer1==integer2);
Integer integer3 = 150;
Integer integer4 = 150;
System.out.println(integer3==integer4);
}
}
Application of packaging
Class String
How objects are created
String pool: there is a string pool in java, and the literal value of the string is directly put into the string pool
The first is:
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);//true
Second species:
String string = new String("hello");
This line of code creates two objects
First object: hello put into string pool
The second object: new creates a string object value of hello in the heap.
Note: the string pool and heap are two different memory spaces
String splicing
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
string1 = string1+"world";
System.out.println(string1==string2);
String If the address of the object of class is changed after splicing, but the string in the string pool is unchanged
//Variable string:
StringBuffer : It's the inefficiency of thread safety.
StringBuilder: Thread unsafe, efficient, commonly used
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
string1 = string1+"world";
System.out.println(string1==string2);
StringBuilder builder = new StringBuilder("hello");
StringBuilder builder2 = new StringBuilder("hello");
System.out.println(builder==builder2);
System.out.println(builder.hashCode());
System.out.println(builder2.hashCode());
builder.append("world");
System.out.println(builder.hashCode());
}
}
Methods in String class
1.charAt(int index): Take out every character in the string
index:Index from0start
2.length() :Get the length of the string
3.contains(Substring): Used to determine whether a substring exists in a stringtrue Non-existentfalse
4.equals():Determine whether the contents of two strings are the same
5.endsWith(String suffix)and startsWith(String prefix) Tests whether the string ends with the specified suffix.
startsWith(String prefix, int toffset)Tests whether the substring of this string starting from the specified index starts with the specified prefix.
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aglasdg";
System.out.println(string.startsWith("g",1));
System.out.println(string.endsWith("g"));
}
}
==================================================================
6..getBytes(): Convert string to a byte array
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aglasdg";
byte[] bytes = string.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
}
==================================================================
7.lastIndexOf(int ch) and IndexOf(int ch) Returns the index of the first occurrence of the specified character in this string
8.isEmpty() : Determine whether the string is empty
9.replace("a","A"); Replacement character
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "adgsfhasdhashdglh";
string = string.replace("a","A");
System.out.println(string);
}
}
==================================================================
10.split(String regex) Division
//Note: some symbols need to be escaped
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "ad.gsf.ha.dhas.hdg.lh";
String[] split = string.split("\\.");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
}
==================================================================
11.substring(3, 5) Truncate the head but not the tail of the string
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
String substring = string.substring(3, 5);
System.out.println(substring);
}
}
==================================================================
12.toCharArray() Convert string to a character array
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
}
}
==================================================================
13.toUpperCase() toLowerCase() toggle case
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
String upperCase = string.toUpperCase();
System.out.println(upperCase);
String lowerCase = upperCase.toLowerCase();
System.out.println(lowerCase);
}
}
==================================================================
14.trim() Remove spaces before and after
//Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = " agh al sdfh ";
String trim = string.trim();
System.out.println(trim);
}
}
Big data operation
Case study:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
double a = 1.0;
double b= 0.9;
double c = 0;
System.out.println(a-b);
System.out.println(a/c);
}
}
//Problem results are not accurate
divide(BigDecimal divisor, int scale, int roundingMode)
divisor:Divisor
scale:Decimal places reserved
roundingMode: Choice mode ROUND_HALF_UP
//Case study:
package cn.wzx;
import java.math.BigDecimal;
public class Demo1 {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
System.out.println(a.subtract(b));
System.out.println(a.add(b));
System.out.println(a.multiply(b));
System.out.println(a.divide(b, 4, BigDecimal.ROUND_HALF_UP));
}
}
Posted by Boozi on Wed, 30 Oct 2019 14:32:59 -0700