Java summary foundation

Keywords: Apache

1. Basic types

1.1 storage space

Byte type
byte 1 byte
plastic
short 2 byte
int 4 byte
long 8 byte
float
float 4 byte
double 8 byte
Character
char 2 byte
Boolean type
boolean 1 byte

1.2char

1.2.1 char a = '\u0041' can represent a Chinese character, based on the original Unicode specification

1.2.2 char a = 99 

a can be compared directly:

char a = 99;
if (a < 'z' && a > 'a') {
    System.out.println(a);
}

2. operator

2.1 binomial operators

Boolean expression? Expression 1: expression 2

Practice:

When x > 0: SGN (x) = 1;

When x=0: sgn(x)=0;

When x < 0: SGN (x) = - 1;

Input x, output the value of sgn(x).

public static void function04(){
	System.out.println("Please input x Value:");
	Scanner scan = new Scanner(System.in);
	int x = scan.nextInt();
	System.out.println("sgn(x)=" + (0==x?0:(x>0?1:-1)));
}

2.2 operator priority

'{}'  >  '++'  >  '(Cast)'  >  '/' >  '+'  >  '<<'  >  '>='  >  '=='  >  '&'  >  '^'  >  '|'  >  '&&'  >  '||'  >  '?:'  >  '='

2.3 "equals()" and "= ="

equals

Note: the equals method does not work on variables of the base data type.

If the equals method is not overridden, the address of the object pointed to by the variable of reference type is compared;

When classes such as String and Date rewrite the equals method, they compare the contents of the object they point to.

==

If the variable acts on the basic data type, the stored "value" is directly compared with whether it is equal;

If the variable acts on a reference type, the comparison is to the address of the object it points to.

3.String

/**
 * 1:String length of output string "HelloWorld"
 * 2:Output the position of "o" in "HelloWorld"
 * 3:Output "HelloWorld" from subscript 5 where "o" first appears
 * 4:Intercept "Hello" in "HelloWorld" and output
 * 5:Intercept "World" in "HelloWorld" and output
 * 6:Remove the white space on both sides of the string "Hello" and output
 * 7:Output the 6th character "W" in "HelloWorld"
 * 8:Whether the output "HelloWorld" starts with "h" and ends with "ld".
 * 9:Convert HelloWorld to uppercase and lowercase, respectively, and output.
 */
public class Test01 {
	public static void main(String[] args) {
		String str = "HelloWorld";
		test1(str);
	}
	public static void test1(String str){
		System.out.println(str.length());
	}
	public static void test2(String str){
		System.out.println(str.indexOf('o'));
	}
	public static void test3(String str){
		System.out.println(str.indexOf('o', 5));
	}
	public static void test4(String str){
		System.out.println(str.substring(0,5));//substring() takes right instead of left
	}
	public static void test5(String str){
		System.out.println(str.substring(5));
	}
	public static void test6(String str){
		System.out.println(str.trim());
	}
	public static void test7(String str){
		System.out.println(str.charAt(5));
	}
	public static void test8(String str){
		System.out.println(str.startsWith("h")+"\n"+str.endsWith("ld"));
	}
	public static void test9(String str){
		System.out.println(str.toLowerCase()+"\n"+str.toUpperCase());
	}

}

4. array

4.1 creation method

int[] arr = new int[10]; / / initialization

int[] arr = {1,2,3,4,5}; / / initialize and assign

int[] arr1 = new int[] {1,2,3,4,5};

4.2 array operation code

//Convert Array to Set set Set
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]

//Array flipping
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

//Remove an element from an array
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

//Convert an int value to a byte array
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
    System.out.format("0x%x ", t);
}

//Check whether the array contains a value
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true

//Connect two arrays
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

//Output the elements in the array as strings
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);

2018-7-18

Posted by benyboi on Sun, 09 Feb 2020 12:28:26 -0800