Welcome to visit the blog Basic Exercise Output
public class MyApp {public
static void Main (string[] args) {
System.out.println ("I can learn the Java language well.") ");
}
}
Integer type
public class Test {public
static void Main (string[] args) {
byte mybyte = 124;
Short myshort = 32564;
int myint = 45784612;
Long mylong = 46789451L;
Long result = Mybyte+myshort+myint+mylong;
SYSTEM.OUT.PRINTLN (The result of the addition of four types is: "+result");
}
Floating-point type
public class Test {public
static void Main (string[] args) {
float f1 = 12.23f;
Double D1 = 4562.12d;
Double d2 = 45678.1564;
Double result = F1+D1+D2;
System.out.println ("Floating-point number addition achieves the result:" +result);
}
Character type
public class Test {public
static void Main (string[] args) {
int i = ' d ';
char C =;
The Unicode code for the SYSTEM.OUT.PRINTLN ("character D" is: "+i);
System.out.println ("the character represented by Unicode code 97 is:" +c);
}
Escape character
public class Test {public
static void Main (string[] args) {
char char1 = ' \ ';
Char char2 = ' \u2605 ';
SYSTEM.OUT.PRINTLN ("Output back slash:" + char1);
SYSTEM.OUT.PRINTLN ("Output pentagram:" + char2);
}
variables (declarations and definitions)
public class Test {public
static void Main (string[] args) {
int i=;
System.out.println ("I's initial value is:" +i);
i=;
System.out.println ("I now the value is:" +i);
}
Declaring constants
Final General capitalization
public class Test {public
static void Main (string[] args) {
final double PI = 3.1415926F;//FINAL Capital
final B Oolean BOOL = true;
}
}
Variables and constants (static variables)
public class Test {
static final double PI = 3.14;
static int age =;
public static void Main (string[] args) {
final int number;
Number = 1235;
Age =;
SYSTEM.OUT.PRINTLN ("Constant PI value is:" +pi);
System.out.println ("The value of number after assignment is:" +number);
System.out.println (the value of age of int variable is: + age);
}
Member variables (static variables) and local variables
public class Test {The
static int times = 3;
public static void Main (string[] args) {
int times = 4;
System.out.println ("The value of the Times is:" + times);
System.out.println ("The value of the Times is:" + test.times);
}
Assignment operator
public class Test {public
static void Main (string[] args) {
int a,b,c;
A =;
c = b = a+4;
System.out.println ("C's value is:" + C);
System.out.println ("B's value is:" + b);
}
}
Arithmetic operators
public static void Main (string[] args) {
float number1 = 45.56f;
int number2 = 152;
System.out.println ("45.56f and 152 's and for:" + (Number1+number2));
System.out.println ("45.56f and 152 difference is:" + (Number1-number2));
System.out.println ("45.56f and 152 product is:" + (Number1*number2));
System.out.println ("45.56f and 152 of the merchants are:" + (Number1/number2));
}
Relational operators
public class Test {public
static void Main (string[] args) {
int number1 = 4;
int number2 = 5;
System.out.println ("4>5 is established:" + (Number1>number2));
System.out.println ("4<5 is established:" + (Number1<number2));
System.out.println ("4==5 is established:" + (Number1==number2));
System.out.println ("4!=5 is established:" + (Number1!=number2));
System.out.println ("4>=5 is established:" + (Number1>=number2));
System.out.println ("4<=5 is established:" + (Number1<=number2));
}
logical operators
public class Test {public
static void Main (string[] args) {
int a = 2;
int b = 5;
Boolean RESULT1 = ((a>b) && (a!=b));
Boolean result2 = ((a>b) | | (a!=b));
The value of System.out.println ("(a>b) && (a!=b) is:" + result1);
System.out.println ("(a>b) | | The value of (A!=B) is: "+ result2);
}
}
Bitwise operator
public class Test {public
static void Main (string[] args) {
int i = +;
int j =;
char c = ' a ';
System.out.println ("& 97 value is:" + (I&J));
The value of System.out.println ("& ' A" is: "+ (I^c));
System.out.println ("The value of the >>1 is:" + (i>>1));
}
Ternary conditional operator
public class Test {public
static void Main (string[] args) {
int i = +;
int j =;
int z = i>j? 100:200;
System.out.println ("i>j?100:200 value is:" + z);
}
Instance-implement two variable swaps without other variables
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the value of variable a: \ n");
Long A = Scan.nextlong ();
System.out.println ("Please enter the value of variable Wolf B: \ n");
Long B = Scan.nextlong ();
System.out.println ("a=" +a+ "\tb=" +b);
SYSTEM.OUT.PRINTLN ("Execute variable Interchange ...");
A = a^b;
B = B^a;
A = a^b;
System.out.println ("a=" +a+ "\tb=" +b);
}
Example-determining the parity of numbers
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter an integer:");
Long number = Scan.nextlong ();
String Check = (number%2 = 0)? "This number is: even": "This number is: odd";
SYSTEM.OUT.PRINTLN (check);
}
Automatic type conversion
public class Test {public
static void Main (string[] args) {
float number1 = 45f;
int number2 = 152;
System.out.println (Number1+number2);
}
public class Test {public
static void Main (string[] args) {
byte mybyte = 127;
int myint =;
float myfloat = 452.12f;
char MyChar = ten;
Double mydouble = 45.46546;
Output the result of the Operation
System.out.println ("127 + 452.12 =" + (mybyte+myfloat));
System.out.println ("127*150 =" + (mybyte*myint));
System.out.println ("127/10 =" + (Mybyte/mychar));
System.out.println ("45.46546+10 =" + (Mydouble+mychar));
}
Force type conversions
public class Test {public
static void Main (string[] args) {
int a = (int) 45.23;
Long y = (long) 456.6F;
int b = (int) ' A ';
System.out.println ("a =" + a);
System.out.println ("y =" + y);
System.out.println ("b =" + B);
}
}
Instance-Type conversions
public class Test {public
static void Main (string[] args) {
int intnum = 4;
float floatnum = 9.5F;
Floatnum/= Intnum; Automatic type conversion
System.out.println ("9.5F/4 =" + floatnum);
Double numx = 4.88;
Double numy = 78.83;
int numz = (int) numx + (int) numy; Force type conversion
System.out.println ("4.88 + 78.83 =" + numz);
Char Charvar = ' T ';
int intvar = (int) Charvar;
System.out.println ("convert character t to int type variable is:" + Intvar);
int num1 = n;
Double num2 = (double) NUM1/3;
System.out.println ("34 of One-third is:" +num2);
}
Comments
Single line multiline document comment
public class Test {public
static void Main (string[] args) {
//single line comment/
* Multiline Comment 1
* Multiline Comment 2
/* *
* Document comment
/
}
}
Example-Determine whether a year is a leap years
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter a year:");
Long year = Scan.nextlong ();
if (year%4==0 && year%100!=0 | | year%400==0) {
System.out.println (year +) is a leap years. ");
} else {
System.out.println is not a leap year. ");
}
}
}
Example-Spherical volume
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter a spherical radius:");
Double r = scan.nextdouble ();
Final double PI = 3.1415926;
Double volume = 4.0/3.0*pi*r*r*r;
System.out.println ("The radius of the sphere is:" + R);
System.out.println ("PI is:" + pi);
System.out.println ("Spherical volume is:" + volume);
}
Compound statement
public class Test {public
static void Main (string[] args) {
int y =;
System.out.println ("Output y value: + y");
int z = 245;
Boolean B;
Compound statement
{
b = y>z;
System.out.println ("Y>z City": "+ B";
}}}