Part2: Java Language Foundation
**
Example01: Receiving input characters from the console
**
Operation results:
Implementation code:
import java.util.Scanner; public class Example01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your name:"); //Prompt user for input String str = scanner.nextLine(); //Get a line of text entered by the user //Print what you enter System.out.println("What you enter is:"+str); } }
Points: import java.util.Scanner; //guide
In this example, we need to introduce the Scanner class, which is a Java scanner class that can read the specified type of data and strings from the input stream.
**
**
Example02: Automatic Type Conversion and Mandatory Type Conversion
**
There are two conversion methods between Java basic data types: automatic type conversion and forced type conversion. Automatic type conversion from low-type data to high-type data, and forced type conversion from high-type data to low-type data.
Operation results:
Code implementation:
public class Example02 { public static void main(String[] args) { byte b = 127; //The range of byte type data is -128-127. System.out.println("byte="+b); int i = 12; System.out.println("accumulation int Be equal to:"+(b+i)); short s = 23561; System.out.println("accumulation short Be equal to:"+(b+i+s)); long l = 400000l; System.out.println("accumulation long Be equal to:"+(b+i+s+l)); char c = 'w'; System.out.println("accumulation char Be equal to:"+(b+i+s+l+c)); float f = 3.1415f; System.out.println("accumulation float Be equal to:"+(b+i+s+l+c+f)); double d = 54.523; System.out.println("accumulation double Be equal to:"+(b+i+s+l+c+f+d)); //Mandatory Conversion from High Type to Low Type System.out.println("hold long Type mandatory conversion to int: "+(int)l); System.out.println("hold char Type mandatory conversion to byte: "+(byte)c); System.out.println("hold double Type mandatory conversion to int: "+(int)d); } }
Points: Converted type variables= (converted type) converted variables;
**
Example03: Encryption can be as simple as this (bit operation)
Tip: The XOR operator "^" of bit operation can change the value of each character in the string by XOR operation with a specified value, so that an encrypted string can be obtained. When the encrypted string is input to the program, XOR operation restores the encrypted string to the original string value. **
Operation results:
Figure 1 uses exclusive or encrypted strings
Figure 2 uses XOR decryption strings
Code implementation:
import java.util.Scanner; public class Example03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a string to be encrypted or decrypted:"); String str = scanner.nextLine(); //Get user input char[] chars = str.toCharArray(); //Get an array of characters for (int i = 0;i<chars.length;i++){ //foreach chars[i] = (char)(chars[i] ^ 2000); //XOR operations on each array element } System.out.println("The results of encryption or decryption are as follows:"); System.err.println(new String(chars)); //Output key } }
Points: The standard error output stream used by the program is not used to output information, but to highlight it in red on the console. The key technology of this example is XOR operation. If a character X or a numeric value is XOR operation with another numeric value m to get y, then XOR operation with y and M can be restored to x, which is the process of encryption and decryption.
*
Example04: Judging odd and even numbers with ternary operators
**
Operation results:
Implementation code:
import java.util.Scanner; public class Example04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter an integer:"); int number = scanner.nextInt(); String str = (number % 2 == 0)?"This is an even number.":"This is an odd number."; System.out.println(str); } }
Points: ternary operators
Conditional operation? Operational result 1: Operational result 2
If the result of conditional operation is true, the return value is the result of operation 1, otherwise it is the result of operation 2.
*
Example05: Implementing 2x 16 without multiplying operators
**
Tip: Left shift operation (very efficient)
Operation results:
Implementation code:
import java.util.Scanner; public class Example05 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the number you want to calculate:"); int number = scanner.nextInt(); System.out.println("The number you enter is:"+number); System.out.println("The result of multiplying the number by two is:"+(number<<1)); System.out.println("The result of multiplying this number by 4 is:"+(number<<2)); System.out.println("The result of multiplying this number by 8 is as follows:"+(number<<3)); System.out.println("The result of multiplying this number by 16 is as follows:"+(number<<4)); } }
Point: If an integer moves n bits to the left, it is equivalent to multiplying the integer by the n power of 2.
***