String class, generics, permission modifiers

1. String

1.1 Two declaration methods of String

public class Demo1 {
public static void main(String[] args) {
\t\t
//The first way to declare:
String string = "I want to go home with the wind";
System.out.println(string);
//This second way of declaring
String str1 = new String("goudan");
System.out.println(str1);
}
}

1.2 Comparison of two strings

== : The comparison is the address of the memory, which is more strict

equlas: compare the addresses first, then compare the contents if the addresses are different. In development, compare two strings must use equals

public class Demo2 {
public static void main(String[] args) {
\t\t
String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
String str4 = new String("abc");
\t\t
//== compares the address of the memory
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//false
System.out.println(str3 == str4);//false
\t\t
// boolean equals(Object anOnject); compares whether two strings are equal
//equals first compares the memory address, if the memory address is the same, it must be the same. If the address is different then compare the content
//If the content is the same, it is also true
System.out.println(str1.equals(str2));//true
System.out.println(str1.equals(str3));//true
System.out.println(str3.equals(str4));//true
/**
* "abc"a "abc"b
* a == b memory is different false
* Continue to compare content
* a = {'a', 'b', 'c'} b = {'a', 'b', 'c'};
* for () {
* if (a[i] == b[i]) {
* a[0] == b[0]
* a[1] ==b[1]
* a[2] == b[2]
* }
*
* }
* public boolean equals(Object anObject) {
        if (this == anObject) {//Comparing whether the two memory addresses are the same
            return true;//If the memory address is the same, return true
        }
        if (anObject instanceof String) {//true
            String anotherString = (String)anObject; forced to string
            v1 = {'a', 'b', 'c'} v2 = {'a', 'b', 'c'};
            int n = value. length;
            if (n == anotherString. value. length) {
                char v1[] = value;
                char v2[] = anotherString. value;
                int i = 0;
                while (n-- != 0) {
                \t\t
                    if (v1[i] != v2[i]) {
                    v1[0] !=v2[0]
                    v1[1] !=v2[1]
                    v1[2] !=v2[2]
                        return false;
                        }
                    i + + ;
                }
                return true;
            }
        }
        return false;
    }
*
*/
\t\t
}
}

1.3 Memory Analysis of String Type I Data

String str = "hello";
String str2 = "hello";
String str3 = new String("hello");
worldhello

Methods under 1.4String class

Get the string length int length();

Get the character at a specific position char charAt(int index);

Get the subscript of a specific character int indexOf(String str);

Get the last position of a specific character int lastIndexOf(int ch);

public class Demo3 {
public static void main(String[] args) {
String str1 = "Chinese Dream";
System.out.println(str1.length());//6
\t\t
String str2 = "abcdef";
System.out.println(str2.charAt(3));//Find the character whose subscript is 0 in the str2 string
\t\t
\t\t
String str3 = "abcdecvf";
\t\t
System.out.println(str3.indexOf("cd"));//Get the subscript of the character
System.out.println(str3.indexOf(98));//1
System.out.println(str3.indexOf('c'));//2
System.out.println(str3.lastIndexOf('c'));//5
\t\t
\t\t
\t\t
\t\t
}
}

The return value is boolean data

boolean endWith(String str); Whether to end with the specified character or string

boolean isEmpty(); Determine whether it is empty, if the string is empty, it will be true

boolean contains(); Determine whether to contain a substring

boolean equals(Object anOnject); Determine whether two strings are equal

boolean equalsIgnoreCase(Object anObject); Ignore case to determine whether two strings are equal

public class Demo4 {
public static void main(String[] args) {
\t\t
String str1 = "Demo1.java";
System.out.println(str1.endsWith(".java"));//true
System.out.println(str1.endsWith("av"));//false
System.out.println(str1.endsWith("va"));//true
\t\t
\t\t
String str2 = "";
System.out.println(str2.isEmpty());//true
String str3 = " ";
System.out.println(str3.isEmpty());//flase is not empty, the space also has content
\t\t
\t\t
String str4 = "abcdef";
System.out.println(str4.contains("cd"));//true
System.out.println(str4.contains("ce"));//false
System.out.println(str4.contains("abcde"));//true
\t\t
System.out.println(str4.equals("abcdeF"));//false
\t\t
System.out.println(str4.equalsIgnoreCase("abcDeF"));//true
\t\t
\t\t
}
}

Convert character array to string

You can directly use the constructor of the String class

String(char[] value)

String(char[] value, int offset, int count)

static String valueOf(char[] chs);

convert string to character array

char[] toCharArray();

public class Demo5 {
public static void main(String[] args) {
\t\t
//convert character array to string
char[] chs = {'a', 'b', 'c', 'd', 'e'};
String str = new String(chs);
System.out.println(str);//abc
//String(char[] chs, int offset, int count);
//The second parameter int offset offset from which number to start
//The third parameter int count number quantity
String str1 = new String(chs, 4, 1);
System.out.println(str1);
\t\t
String str2 = String. valueOf(chs);
System.out.println(str2);
\t\t
\t\t
//convert string to character array
char[] arr1 = "The dog egg is very dog".toCharArray();
for (int i = 0; i < arr1. length; i ++ ) {
System.out.println(arr1[i]);
}
\t\t
}
}

The following are a few more important methods

String replace(char oldChar, char newChar); characters are replaced in the string

String [] split(String regex); Cut the current string with regex

String subString(int beginIndex); intercept a part of the string

String subString(int beginIndex, int endIndex); intercept a part of the string

String toUpperCase(); Convert lowercase letters to uppercase letters

String toLowerCase(); convert uppercase to lowercase

String trim(); Remove left and right spaces

import java.util.Arrays;

public class Demo6 {
public static void main(String[] args) {
\t\t
String str = "abcdef";
System.out.println(str.replace('c', 'in'));
System.out.println(str.replace("cd", "Niu Caiyun"));
\t\t
\t\t
String str1 = "Hee hee da ha ha ha da me da ha ha ha da";
//cut by da
String[] strs = str1.split("哒");
System.out.println(strs);
System.out.println(Arrays.toString(strs));//[hee hee, huh, huh, huh]
\t\t
for (int i = 0; i < strs. length; i ++ ) {
System.out.println(strs[i]);
}
\t\t
String str2 = "ab,cd,ef,g";
// to, cut
String[] strs1 = str2. split(",");
System.out.println(Arrays.toString(strs1));
\t\t
\t\t
//Intercept part of the string
String str3 = "abcdef";
System.out.println(str3.substring(2));//cdef
\t\t
\t\t
String str4 = "University puts mobile phones in rural areas first";
System.out.println(str4.substring(2, 4));// first put //head or tail
\t\t
\t\t
\t\t
String str5 = "abCDF";
System.out.println(str5.toUpperCase());//ABCDF
System.out.println(str5.toLowerCase());//abcdf
\t\t
\t\t
String str6 = " index xixi ";
System.out.println(str6);
System.out.println(str6.trim());
}
}

2. Generic [key and difficult points]

2.1 Why use generics

A wide range of types, the requirements for data consistency in development are relatively high

For example:

The data stored in the array are all of the same type I

int[] arr = new int[]

char[] arr = new char[]

import java.util.ArrayList;
import java.util.Scanner;

public class Demo1 {
public static void main(String[] args) {
\t\t
int[] arr = {1,2,3,4};
//String class does not need to import package, why? Because under the java.lang package
String[] strs = {"nx", "hsjj", "xnjasjn"};
\t\t
//Arrays can indeed guarantee data type consistency, but it is not necessary for development! ! ! why? There are fewer array methods, and the array capacity is set in advance
//It's not very convenient, what should I do? Learning collection will be taught tomorrow
//The function of the collection is similar to that of an array, both of which are containers used to store data
ArrayList list = new ArrayList();//list is a container for storing data
list.add("dog egg");//Add
list. add(34);
list.add('zhi');
list. add(true);
System.out.println(list);
//Is the above code good? not good! ! ! The requirements for data consistency in development are relatively high
//Get a data
String obj = (String) list. get(0);
System.out.println(obj);
//It is not good to force it, now there is a technology generics that can guarantee our data consistency for collections
//ArrayList<class>
//<String> tells the compiler that this collection can only store data of String type, and the consistency is guaranteed
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Zhang San");
list1.add("Wang Er");
list1.add("wangliu");
System.out.println(list1);
String str1 = list1. get(0);
System.out.println(str1);
//Generic type in <class> If you put int type data ===》Integer
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2. add(23);
//Extension: wrapper classes corresponding to the eight basic data types
/**
* byte ==> Byte
* short===>Short
* int===>Integer
* long===>Long
* boolean-===>Boolean
* char===>Character
* float===>Float
* double====>Double
*/
}
}

2.2 Use of custom generics in methods

Grammar format:

 public <meaningless placeholder> return type method name (parameters) {

}

Meaningless placeholder: Any character can be used but all capitalized. In development, it is generally T (Type) E (Element) ? (unknown)

Getting Started Case:

public class Demo2 {
public static void main(String[] args) {
test(89);// test(T t) T===>Integer
test("goudan");// test(T t) T====>String
// public static void test (int i) {
// System.out.println(i);
// }
// public static void test (double i) {
// System.out.println(i);
// }
// public static void test (String i) {
// System.out.println(i);
// }
}
//Write generic entry-level cases that are more extensive in nature
//T changes its type with the actual parameter of the method
public static <T> void test(T t) {//T represents all types
System.out.println(t);
}
}

no parameter no return value

With parameters but without return value

Return value without parameter

have parameters and return value

public class Demo3 {
public static void main(String[] args) {
test();
}
//A method with no parameters and no return value does not need to use generics
public static <T> void test () {
System.out.println("hewllo world");
}
\t
//A method with parameters but no return value can be used
public static <T> void test (T t, T t1) {
//t is multiple types, when operating, it must conform to the operation mode corresponding to the data type
System.out.println(t + "" + t1);
}
\t
//Without parameters and return values, there is no need to use generics
public static <T> T test1 () {
\t\t
return null;
}
\t
//The method fa with parameters and return value has limitations in writing
public static <T> T test2 (T t, T t1) {
return t;
}
// Only those with parameters have a little use
\t
}

2.3 Generic class [emphasis]

Grammar format:

class class name <meaningless placeholder> {
\t
}
class Person<T> {
// Generic classes can have ordinary methods
public void eat() {
System.out.println("dinner");
}
//method with generic
//public <T> void test() {Generally, do not write <T> in methods in generic classes because the parameters of the method are constrained according to the <T> behind the class
public void test (T t) {
System.out.println(t);
}
//Static method for self-entertainment
//The generic type is determined when the object is created, and the static method is earlier than the creation of the object, so your generic is useless to me
//Inform the programmer that the generics here have nothing to do with the generics of the class
public static <E> void test1 (E e) {
System.out.println(e);
}
}
public class Demo4 {
public static void main(String[] args) {
\t\t
Person.test1(78);
Person<String> strPerson = new Person<String>();
// Now T is String, which means that when calling the test method, the parameter can only pass String
strPerson.test("goudan");
//strPerson.test(89);
}
}

2.4 Generic abstract class

Grammar format:

abstract class class <meaningless placeholder> {

}
abstract class A<T> {
abstract void test(T t);
}
//1. When inheriting an abstract class, the subclass must also have the same generic placeholder as the parent class
class ATest<T> extends A<T> {
void test(T t) {
\t
}
}
public class Demo5 {
public static void main(String[] args) {
ATest<Integer> aTest = new ATest();
aTest.test(67);
}
}

2.5 Generic interface

Grammar format:

interface interface name <meaningless placeholder> {

}
interface B<T> {
void test(T t);
}
class BTest<T> implements B<T> {
public void test(T t) {
System.out.println("xixi");
}
}
public class Demo6 {
public static void main(String[] args) {
\t\t
}
}

3. Permission modifier

Permission Modifier Current class Other classes under the same package Subclasses under other packages Other classes in other packages
public can can can can
protected Yes Yes Yes No
Default Yes Yes No No
private Yes No No No

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108682 people are studying systematically