The first Java program, or the familiar Hello World
public class Firstsample {// class public static void Main (string[] args) //method {System.out.println ("Hello world!"); //Output statement "Hello world! ”
The output is uninteresting, that is, the console output Hello world!
Notable areas of the program are:
1.java programs are case-sensitive, that is, case-insensitive.
2. Classes are building blocks for building all Java applications and applets. All content in a Java application must be placed in a class. The file name of the source code must be the same as the class name of the public class.
3. According to the Java language Specification, the main method must be declared public. The method in main must be static.
Java is a strongly typed language.
There are 8 basic types in Java, 4 integer, 2 float, 1 character types used to represent Unicode encoded character cells, char, and one to represent the true worth of the Boolean type. There are no unsigned types in Java.
|
Type |
Storage requirements |
|
Int |
4 bytes |
|
Short |
2 bytes |
|
Long |
8 bytes |
|
Byte |
1 bytes |
There are two types of floating-point in Java.
|
Type |
Storage requirements |
|
Float |
4 bytes |
|
Double |
8 bytes |
The char type in Java encodes a unit of code with UTF-16.
The Boolean type has two values: false and True, which are used to determine the logical condition. An integer value and a Boolean value cannot be converted to and from each other.
In Java, the keyword final is used to indicate constants.
Final double money = 50;
The keyword final indicates that the variable can only be assigned once, and once it has been assigned, it cannot be changed again.
If a constant is used in more than one method in a class, these constants are typically made into class constants. Set a class constant using the static final of the keyword.
public static final Double money = 50;
If a variable is declared public, the method of other classes can also use this constant.
In Java, an integer is removed by 0 and an exception is generated, and the floating-point number is 0 in addition to an infinity or Nan result.
The bitwise operators in Java are: & and, | Or, ^ xor, ~ Non-,<<, >> left and right shift.
int a = (N & (1 << 3)) >> 3; Check n fourth bit is not 1.
The >>> operator fills the high position with the sign bit with the 0 fill high,>> operator. No <<< operations.
A Java string is a sequence of Unicode characters.
The substring method of the string class can extract a substring from a larger string.
String a = "Hello"; String B = a.substring (0,4),//b is Hell B's length is 4-0=4
The Java language allows the use of the + sign to concatenate two strings. When a string is spliced with a non-string value, the latter is converted to a string.
int age = 13; String a = "abc" + Age;
Use the Equals method to detect whether two strings are equal.
A.equals (b); equals returns True, not equal to return false.
if you want to detect whether two strings are equal, not case-sensitive, you can use theEqualsignorecasemethod.
"Hello". Equalsignorecase ("Hello");
Detection method for empty string and value null string
(str = null && str.length ()! = 0)
The first thing to check is that STR is not NULL. If a method is called on a null value, an error occurs.
If you need to build a string with many small strings, you should try the StringBuilder class
Stringbulider builder = new StringBuilder (); bulider.append (ch);//Add character builder.append (str);//Add string all = Builder.tostring ();//Assign the full string to all
Input and output instances in Java
Import java.util.*;p Ublic class Inputtest {public static void main (string[] args) {Scanner in = new Scanner (system.in);//Construction Scanner Object System.out.print ("What's your name?") "); String name = In.nextline ();//Read in the string System.out.print ("How old are you?" int age = In.nextint ();//read in an integer System.out.println ("Hello," + name + "You are now" + Ages + "old");} }
A block that conforms to a statement is a simple Java statement with a pair of curly braces enclosed in a few conditions. The block determines the scope of the variable. A block can be nested within another block. A variable with the same name cannot be declared in a nested two block.
In the switch statement
The case label can be:
1. Type char, Byte, short, or int
2. Enumeration
3. Starting with JavaSE7, the case label can also be a string literal.
An array is a data structure that is used to store the same type worth collection.
Declaration of an array
Int[] A = new Int[10];int a[] = new INT[10];
When you create a numeric array, all the elements are initialized to0.booleanThe elements of the array are initialized tofalse. The elements of an array of objects are initialized to a special valueNULL, which means that these elements do not hold any objects.
If you want to get the number of elements in the array, you can use Array.Length
for (int i = 0;i < a.length;i++) System.out.println (A[i]);
another way to iterate over an array
for (int element:a) System.out.println (elelment);
To sort a numeric array, you can use theArraysin the classSortMethod
Arrays.sort (a);
Math.random method will return a 0 to the 1 between the random floating-point numbers, you can get the 0 to the n-1 a random number between the.
int r = (int) (Math.random () *n);
several small application examples
1. Lottery questions
Import java.util.*;p Ublic class test001 {public static void main (string[] args) {Scanner in = new Scanner (system.in); System.out.println ("How many numbers can you smoke?") "); int a = In.nextint (); System.out.println ("What's the biggest number?") "); int n = in.nextint (); int[] num = new Int[n];for (int i = 0; I < num.length;i++) {Num[i] = i + 1;} Int[] result = new Int[a];for (int i = 0; I <result.length;i++) {int rad = (int) (Math.random () * n); Result[i] = Num[rad] ; Num[rad] = num[n-1];n--;} Arrays.sort (result); System.out.println ("Lottery Result:"); for (int ele:result) {System.out.println (ele);}}}
2. Interest on banks ' savings
public class Test002 {public static void main (string[] args) {final double Begin = 10;final int R = 6;final int Y = 10;doub le[] Rate = new Double[r];for (int i = 0; i < rate.length; i++) {Rate[i] = (Begin + i)/100.0;} double[][] Bala = new Double[y][r];for (int i = 0; i < bala[0].length; i++) {Bala[0][i] = 10000;} for (int i = 1;i < Bala.length, i++) {for (int j = 0; J < Bala[i].length; J + +) {double Oldbala = bala[i-1][j];d ouble u p = Oldbala * Rate[j];bala[i][j] = Oldbala + Up;}} for (int i = 0; i < rate.length;i++) {System.out.printf ("%9.0f%%", [Rate[i]);} System.out.println (); for (double[] R:bala) {for (double b:r) {System.out.printf ("%10.2f", b);} System.out.println ();}}}
3. Print Yang Hui triangles using irregular arrays
public class Test003 {public static void main (string[] args) {final int m = 10;int[][] A = new Int[m][];for (int i = 0; i< ; m; i++) {A[i] = new int[i+1];} for (int i = 0, I < a.length;i++) {for (int j = 0; J <a[i].length; J + +) {int temp = 1;for (int k = 1; k <= J; k++) {Te MP = temp * (i-k + 1)/k;} A[I][J] = temp;}} For (int[] r:a) {for (int ele:r) {System.out.printf ("%4d", ele);} System.out.println ();}}}
My Java Learning notes (i)