Java Learning Notes: Basic Programming Concepts

Keywords: Java IntelliJ IDEA Windows Programming

9.23-9.26
Go to the website and download intellij IDEA https://www.jetbrains.com/idea/download/#section=windows
1, class
public class hello {...} where Hello is the name, if you modify the name on this basis, the compilation will be wrong (the file name is still hello.java, so the file name and name must be consistent)
2. System Output
println -- Adding a newline to the output
print -- Output without additional line breaks

System.out.print("hello");
System.out.print("world");
//The output results are as follows:
helloworld
System.out.println("hello");
System.out.println("world");
//The output results are as follows:
hello
world

2. Procedure Notes

\ Single line comment
 /*...*/: Multi-line comments
 / **...*/: Document Notes

3. Identifiers and keywords
(1) Identifier

/ ** Core principles of identifier definition in Java: composed of letters, numbers,, $
   You can't start with numbers, you can't use reserved words (or keywords) in Java.
 * Recommendations on the use of identifiers:
 * When writing code, try not to use numbers, such as i1, i2
 * Name as meaningful as possible, don't use a, b, etc., try to use the English words to write the title.
 * In Java, case-sensitive, mlkj, Mlkj and MLKJ are three different identifiers
 * The "$" symbol has a special meaning, so don't use it (as you can see in the contents of the internals)
 * Illegal identifiers for example:
 * class (keywords [keywords are all expressed in lowercase letters]   
 * 67.9 (Number Beginning and Including ".")     
 * YOOTH HFJDF (including spaces)
 */

Note: Chinese-defined identifiers can be used in Java

public class hello
{
   public static void main(String[] args)
    {
       int age = 20;
       System. out. println (age);
    }
}


The output results are as follows:
20

Although Chinese Definition Identifier can be used in Java, the Editor strongly recommends that Chinese Definition not be used in actual programming.
(2) Keyword
There are two unused keywords in Java: goto (for unconditional jumps in other languages), const (for constants in other languages)
There are also three special meanings of tags in Java: true, false, null
4. Data type
byte (8 bits)
short (16 bits), int (32 bits), long (64 bits)
float (32 bits), double (64 bits)
char (16 bits)
bloolean
5, integer

public static void main(String[] args)
{
  int x=208374;
  byte y = 124;
  short  z = 1234;
  long sum = 1234554;//long integers, the last "L" or "l" can not be lost;
 System.out.println(x+y+z+sum);
}

6. Floating Point Type

double a = 12.2;//double floating point number, the last "D" or "d" may not be necessary;
float  b = 13.3f;//Floating point type, the last "F" or "f" can not be lost;
Float B = 13.3f; / float type floating point number, the last "F" or "f" can not be lost;
   float  c = 13.3f;
   System.out.println(b+c);
The output is as follows:
26.6

In integer computing, decimal digits cannot be saved. To solve the problem of dividing decimal points, the data type can be converted to double or float type, as follows:

public static void main(String[] args)
{
 int a=10;
 int b=4;
 System.out.println(a/b);
    System.out.println((double)a/b);//The final type is double 
    System.out.println(a/(float)b);//The final type is float

}

//The output results are as follows:
2
2.5
2.5

7. Character type

package classe;
public class hello
{
   public static void main(String[] args)
   {
    char c='A';
    int num = c;//Get the encoding value of the character

    System.out.println(num);

    num = num + 32 ;
       System.out.println((char)num);//int to char
       char a='Plum';//Only one Chinese character is allowed to be saved.
       int sum = a;
       System.out.println(sum);

   }
}

8. Boolean type
It is a logical result. It mainly stores two kinds of data: true and false. This kind of data is mainly used in the logical use of some programs.

package classe;
public class hello
{
   public static void main(String[] args)
   {
    boolean a = true;
    if(a)
    {
        System.out.println("hello");
    }
   }
}
//The output results are as follows:
hello

9. String string

package classe;
public class hello
{
   public static void main(String[] args)
   {
    String str = "hello world";

      System.out.println(str);
      //Connection of strings:
       str = str + ",my name is anny";
       System.out.println(str);
   }
}

10. Escaped Characters

package classe;
public class hello
{
   public static void main(String[] args)
   {


      System.out.println("Hello: t My name is Ma Yun n I come from " Mysterious and ancient Oriental ");

   }
}
The output results are as follows:
Hello: My name is Ma Yun.
I come from the "mysterious and ancient Oriental"

11. Operator
11.1 Arithmetic Operators: +, -,*, /, (Add, subtract, multiply, divide, modular operation [Remainder]

package classe;
public class hello
{
   public static void main(String[] args)
   {

       int a = 10;
       int b = 2;
       int x = 4;
       int y = 3;
      System.out.println("addition a+b:"+(a+b))
       System.out.println("subtraction a-b:"+(a-b));
       System.out.println("multiplication a*b:"+(a*b));
       System.out.println("division a/b:"+(a/b));
       System.out.println("Modular operation x%y:"+(x%y));
       //Simplified Operator
       int sum = 20;
       sum+=a;//Equivalent to sum=sum+a, the rest of the operators are the same principle, here are not listed one by one.
   }
}

11.2 Self-adding and Self-reducing
+ + i: self-adding in operation first
i++: The first operation is self-increasing

System.out.println(++a); //The output is 11.
System.out.println(a++);//The output is 10.

11.3 Relational Operator

package classe;
public class hello
{
   public static void main(String[] args)
   {

      int x = 10;
      int y = 10;
      double z = 10.0;
      boolean flag = x==y;
          System.out.println(flag);
          boolean flag1 = x==z;
       System.out.println(flag1);
       char c = 'A';
       int sum =65;
       boolean flag2 = c==sum;
       System.out.println(flag2);
   }
}

//Output results:
true
true 
true

11.4 Trinomial Operator

package classe;
public class hello
{
  //Find the maximum value
   public static void main(String[] args)
   {

      int x = 10;
      int y = 20;
      int max;
      max = x > y?x:y;//X > y is established, max=x; x > y is not established, max=y;

       System.out.println(max);
   }
}

11.5 Logic Operator

package classe;
public class hello
{

   public static void main(String[] args)
   {

       int x = 10;
       int y = 10;
       boolean flag =! (x==y);//Flag takes the opposite of (x==y), x==y - > flag=true
                               //!(x==y) -> flag =false
       System.out.println(flag);
       

   }

}
package classe;
public class hello
{

   public static void main(String[] args)
   {

       int x =10;
       int y = 20;
       int z = 30;
          System.out.println(  z>x  &&  z>y  );
       System.out.println(  z<x &&  z<y );

       System.out.println(z>x ||  z>y);
       System.out.println(  z<x ||  z<y );

   }

}
//Output results:
true 
false
true 
false

11.6-bit operator
&,|,^,~,<<,>>,>>>
Bit-by-bit AND, Bit-by-bit OR, XOR (the same is 0, the different is 1), inversion, left displacement, right displacement, unsigned right displacement

package classe;
public class hello
{

   public static void main(String[] args)
   {

       int x = 16;
       System.out.println(x<<2);//Left displacement: equivalent to x multiplied by 2, the displacement is multiplied by 2.
       System.out.println( x>>2 );//Right displacement: equivalent to x divided by 2, the displacement is divided by 2.

   }

}

Shift can be well implemented by multiplying or dividing the n-th power of 2.

Posted by mallard on Thu, 26 Sep 2019 06:37:53 -0700