Java language basics
Output Error information and debugging information
Package com;
Public class Msj {
Public static void main (String [] args ){
The System. out. println ("main () method is running. ");
// Output error message
System. err. println ("manually output an error message during running :");
System. err. println ("\ t this software does not buy insurance, please pay attention to security ");
System. out. println ("PrintErrorAndDebug. main ()");
The System. out. println ("main () method ends. ");
}
}
Result:
Receiving input characters from the console
Package com;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Input Scanner = new scanner (System. in); // create an input Flow Scanner
System. out. println ("Enter your ID card number:"); // prompt the user to enter
String line = response. nextLine (); // obtain a line of text entered by the user
// Print the description of the input text
System. out. println ("your ID card number was" + line. length () + "digit ");
}
}
Result:
Redirect output stream Implementation Program logs
Package com;
Import java. io. FileNotFoundException;
Import java. io. PrintStream;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Try {
PrintStream out = System. out; // Save the original output stream
PrintStream ps = new PrintStream ("./log.txt"); // create a file output stream
System. setOut (ps); // sets the new output stream.
Int age = 18; // defines the integer variable
System. out. println ("age variable defined successfully, initial value: 18 ");
String sex = "female"; // defines String variables.
System. out. println ("gender variable defined successfully, initial value: female ");
// Integrate two variables
String info = "this is a" + sex + "child. It should be" + age + "years old. ";
System. out. println ("integrate the two variables into the info string variable and the result is:" + info );
System. setOut (out); // restore the original output stream
System. out. println ("the program is running. Check the log file. ");
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
}
}
Result:
Automatic type conversion and forced type conversion
Package com;
Public class Msj {
Public static void main (String [] args ){
Bytes B = 127;
Char c = 'W ';
Short s = 23561;
Int I = 3333;
Long l = 400000L;
Float f = 3.14159F;
Double d = 54.523;
// Low-type and high-type automatic conversion
System. out. println ("accumulative byte equals to:" + B );
System. out. println ("accumulative char equals to:" + (B + c ));
System. out. println ("accumulative short equals to:" + (B + c + s ));
System. out. println ("accumulative int equals to:" + (B + c + s + I ));
System. out. println ("accumulative long equals to:" + (B + c + s + I + l ));
System. out. println ("accumulative float equals to:" + (B + c + s + I + l + f ));
System. out. println ("accumulative double equals:" + (B + c + s + I + l + f + d ));
// Forced conversion from high type to low type
System. out. println ("convert the forced long type to int:" + (int) l );
// Data will be lost during high-to low-type conversion
System. out. println ("Convert int to short:" + (short) l );
// Convert the real number to the integer to discard the fractional part.
System. out. println ("convert the double type to int:" + (int) d );
// Convert an integer to a character type to obtain the corresponding encoded characters
System. out. println ("convert the short forced type to char:" + (char) s );
}
}
Result:
Encryption can be as simple as this (bitwise Operation)
Package com;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Repeated scan = new partition (System. in );
System. out. println ("Enter the string to be encrypted ");
String password = scan. nextLine (); // get user input
Char [] array = password. toCharArray (); // get the character array
For (int I = 0; I <array. length; I ++) {// traverses character Arrays
Array [I] = (char) (array [I] ^ 20000); // returns an exclusive or operation on each array element.
}
System. out. println ("the encryption result is as follows :");
System. out. println (new String (array); // output encryption information
Password = new String (array );
Array = password. toCharArray (); // get the character array
For (int I = 0; I <array. length; I ++) {// traverses character Arrays
Array [I] = (char) (array [I] ^ 20000); // returns an exclusive or operation on each array element.
}
System. out. println ("the decryption result is as follows :");
System. out. println (new String (array); // output decryption Information
}
}
Result
Use a ternary operator to determine the odd and even numbers
Package com;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Scanned scan = new Scanner (System. in); // create an input Flow Scanner
System. out. println ("enter an integer :");
Long number = scan. nextLong (); // gets the integer entered by the user.
String check = (number % 2 = 0 )? "This number is an even number": "This number is an odd number ";
System. out. println (check );
}
}
Result:
Multiplication without multiplication Operators
Package com;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Scanner scan = new Scanner (System. in); // create a Scanner
System. out. println ("enter an integer ");
Long number = scan. nextLong (); // obtain the input integer
System. out. println ("the number you entered is:" + number );
System. out. println ("the result of multiplying the number by 2 is:" + (number <1 ));
System. out. println ("the result of multiplying the number by 4 is:" + (number <2 ));
System. out. println ("the result of multiplying the number by 8 is:" + (number <3 ));
System. out. println ("the result of multiplying the number by 16 is:" + (number <4 ));
}
}
Result:
Implement the interchange of two variables (without the aid of 3rd variables)
Package com;
Import java. util. collections;
Public class Msj {
Public static void main (String [] args ){
Scanner scan = new Scanner (System. in); // create a Scanner
System. out. println ("Enter the value of variable ");
Long A = scan. nextLong (); // receives the first variable value
System. out. println ("Enter the value of variable B ");
Long B = scan. nextLong (); // receives the second variable value.
System. out. println ("A =" + A + "\ tB =" + B );
System. out. println ("execution variable interchange ...");
A = A ^ B; // execute variable swapping
B = B ^;
A = A ^ B;
System. out. println ("A =" + A + "\ tB =" + B );
}
}