Java language Basics

Source: Internet
Author: User

Tag: The Java language is missing even. com consolidation length End-128 odd

Output error message and debug information

Package com;
public class MSJ {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (the "main () method starts running. ");
Output error message
SYSTEM.ERR.PRINTLN ("Manually output an error message during run time:");
System.err.println ("\ t this software does not buy insurance, please pay attention to security");
System.out.println ("Printerroranddebug.main ()");
SYSTEM.OUT.PRINTLN (the "main () method runs the end. ");
}
}

Results:

To receive input characters from the console

Package com;

Import Java.util.Scanner;

public class MSJ {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);//create an input stream scanner
System.out.println ("Please enter your ID number:");//Prompt user to enter
String line = Scanner.nextline ();//Gets the text of a row entered by the user
Print a description of the input text
System.out.println ("Original your ID number is" + line.length () + "digits of ah");
}
}

Results:

REDIRECT Output stream Realization program log

Package com;

Import java.io.FileNotFoundException;
Import Java.io.PrintStream;
Import Java.util.Scanner;

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 file output stream
System.setout (PS);//Set up with a new output stream
int age=18;//defining shaping variables
SYSTEM.OUT.PRINTLN ("Age variable is defined successfully, initial value is 18");
String sex= "female";//define string variable
SYSTEM.OUT.PRINTLN ("The gender variable is defined successfully, the initial value is female");
Consolidation of two variables
String info= "This is a" +sex+ "child, should have" +age+ "years old. ";
System.out.println ("consolidates two variables as info string variable, the result is:" +info);
System.setout (out);//restore Original output stream
SYSTEM.OUT.PRINTLN ("The program has finished running, please review the log file. ");
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
}

Results:

Automatic type conversions and forced type conversions

Package com;
public class MSJ {
public static void Main (string[] args) {
byte B = 127;
char c = ' W ';
Short S = 23561;
int i = 3333;
Long L = 400000L;
float F = 3.14159F;
Double d = 54.523;
Automatic conversion of low type to high type
SYSTEM.OUT.PRINTLN ("Cumulative byte equals:" + B);
SYSTEM.OUT.PRINTLN ("Cumulative char equals:" + (b + c));
SYSTEM.OUT.PRINTLN ("Cumulative short equals:" + (b + C + s));
SYSTEM.OUT.PRINTLN ("additive int equals:" + (b + C + S + i));
SYSTEM.OUT.PRINTLN ("Additive long equals:" + (b + C + S + i + L));
SYSTEM.OUT.PRINTLN ("Cumulative float equals:" + (b + C + S + i + L + f));
SYSTEM.OUT.PRINTLN ("Cumulative double equals:" + (b + C + S + i + L + F + D));
High-type to low-type casts
System.out.println ("Converts a long cast type to int:" + (int) l);
High type to low type conversion can lose data
SYSTEM.OUT.PRINTLN ("Convert int coercion type to short:" + (short) l);
Real to integer conversions will discard fractional parts
System.out.println ("Converts the double coercion type to int:" + (int) d);
integer-to-character-type conversions get the corresponding encoded characters
System.out.println ("Convert the short coercion type to char:" + (char) s);
}
}

Results:

Encryption can be as simple as this (bit operation)

Package com;

Import Java.util.Scanner;

public class MSJ {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the string to encrypt");
String password = scan.nextline ();//Get user input
char[] array = Password.tochararray ();//Get character array
for (int i = 0; i < Array.Length; i++) {//Traversal character array
Array[i] = (char) (Array[i] ^ 20000);//An XOR operation on each array element
}
SYSTEM.OUT.PRINTLN ("Encrypted results are as follows:");
System.out.println (new String (array));//Output encryption information
Password=new String (array);
Array = Password.tochararray ();//Get character array
for (int i = 0; i < Array.Length; i++) {//Traversal character array
Array[i] = (char) (Array[i] ^ 20000);//An XOR operation on each array element
}
System.out.println ("Decryption results are as follows:");
System.out.println (new String (array));//Output decryption information
}
}

Results

Judging odd and even numbers with ternary operators

Package com;

Import Java.util.Scanner;

public class MSJ {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);//create an input stream scanner
System.out.println ("Please enter an integer:");
Long number = Scan.nextlong ();//Gets the integer entered by the user
String check = (number% 2 = = 0)? "This number is: even": "This number is: odd";
SYSTEM.OUT.PRINTLN (check);
}
}

Results:

Multiplication is not implemented without multiplication operators

Package com;

Import Java.util.Scanner;

public class MSJ {
public static void Main (string[] args) {
Scanner scan=new Scanner (system.in);//Create Scanner
System.out.println ("Please enter an integer");
Long number = Scan.nextlong ();//Gets the input integer
System.out.println ("The number you entered is:" +number);
System.out.println ("The number is multiplied by 2 for the result:" + (number<<1));
System.out.println ("The number is multiplied by 4 for the result:" + (NUMBER<<2));
System.out.println ("The number is multiplied by 8 for the result:" + (number<<3));
System.out.println ("The number is multiplied by 16 for the result:" + (number<<4));
}
}

Results:

Implements the interchange of two variables (without the help of the 3rd variable)

Package com;

Import Java.util.Scanner;

public class MSJ {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);//Create Scanner
System.out.println ("Please enter the value of variable a");
Long A = Scan.nextlong ();//Receive first value of variable
System.out.println ("Please enter the value of variable B");
Long B = Scan.nextlong ();//Receive second value of variable
System.out.println ("a=" + A + "\tb=" + B);
SYSTEM.OUT.PRINTLN ("Perform variable interchange ...");
A = a ^ b;//performing variable swaps
b = b ^ A;
A = a ^ B;
System.out.println ("a=" + A + "\tb=" + B);
}
}

Java language Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.