Day03-1 character type, string type

Posted by sillysillysilly on Tue, 04 Jan 2022 16:56:40 +0100

Character type

  1. Character constant: a single character char enclosed in single quotes
  2. The bottom layer of the computer can only recognize binary data. If some character information is represented, the character will be converted to the corresponding decimal integer first, and then the decimal integer will be converted to binary integer recognition.
  3. Each character corresponds to an integer. This correspondence is stored in the coding table.
  4. Conversion relationship:
    Character information -------- > integer information coding
    Integer information -------- > character information decoding
  5. Each country has its own character information, so it also has its own country corresponding coding table.
    Chinese coding table: GBK GBK coding table not only defines ASCII coding table, but also defines the corresponding relationship between some Chinese characters and integers.
  6. In order to better unify the characters of various countries, the official has formulated a unified coding table: universal code table: UTF-8.
  7. If an English character is defined in the encoding table of GBK, it is stored in one byte.
    If a Chinese character is defined, it is stored in two bytes.
  8. In the UTF-8 encoding table, if an English character is defined, it is stored in one byte.
    If a Chinese character is defined, it is stored in three bytes.

String type

  1. Character string: 0, 1, multiple symbols enclosed in double quotation marks.

  2. Type of string variable: String

  3. Is a reference data type
    0x0011 here is equivalent to the address. Reference data obtains information through the address

  4. Summary:
    In the variables of basic data type, the data value itself is stored
    In variables that reference data types, the address value of the data location is stored

  5. String type string, and other data types can be used + + +Operation
    + + +The operation is not addition, but splicing

class Demo01 {
	public static void main(String[] args) {
		//If it is two strings +, splice directly
		System.out.println("Hello" + "abc");
		//If one is a string, the other is another data type
		//Other data types will be converted to strings before splicing
		//"Hello" + 123      "Hello" + "123" 
		System.out.println("Hello" + 123);
		System.out.println("Hello" + true);
		//Add integers before splicing
		System.out.println(123 + 100 + "Hello");
		//Splice straight back
		System.out.println("Hello" + 123 + 100);

Common methods in String type

Equals (parameters)Used to compare whether two strings are equal. If equal is true, unequal is false
Contains (parameter)Used to determine whether the calling string contains a parameter string
Startswith (parameter)Used to determine whether the calling string starts with a parameter
Endswith (parameter)Used to determine whether the calling string ends with a parameter
isEmpty()Judge whether the calling string is an empty string
substring(int i)Intercept the string, starting from i+1 characters and then intercepting to return a new string
substring(int i,int j)Intercept the string from i+1 to j-1
toUpperCase()Convert string to all uppercase
toLowerCase()Convert string to all lowercase
class Demo02 {
	public static void main(String[] args) {

		String str = "Hello";
		//Determine whether the strings are equal
		//Determine whether the parameter string is included
		//Determine whether it starts with a parameter
		System.out.println("Zhang San".startsWith("Zhang"));
		System.out.println("Zhang San".startsWith("Lee"));
		//Determine whether it ends with a parameter
		System.out.println("Zhang San".endsWith("three"));
		System.out.println("Zhang San".endsWith("Zhang San"));
		//Determine whether it is an empty string
		System.out.println("Zhang San".isEmpty());
		System.out.println(" ".isEmpty());
		//null value cannot call the method
		//Intercept string
		String str2 = "abcdefg";
		String s1 = str2.substring(2);
		String s2 = str2.substring(2,5);
		String str3 = "ABCdEfG";
		//Convert string to all lowercase
		//Convert string to all uppercase

Topics: Java Programming string