A real-world chapter in java: Java Basics

Start at foot!

Reference Links

1. First Java program, learn java

//Define a learn1 class that must have the same name as the j source file.
public class learn1 {      
    //Static's static class-owned method accesses without requiring an instance object, void means the method does not return a value
    public static void main(String[] args){    //The main method is the entry to the program

Since I wrote it using a compiler, I generate a.class file back in the corresponding directory after writing, which means the code has been compiled and the program runs as follows:

2. Variables

Variables in java need to be declared first

int a = 1;
public class VarTest{
    public static void main (String[] args){
        int a = 5;

3. Constants

The value of a constant cannot be modified. The final keyword in Java can be used to declare properties (constants), methods, and classes. When final modifies an attribute, it means that it must be initialized once memory space has been allocated, meaning "this cannot be changed" or "final". A constant can be declared by preceding the variable with the keyword final. In the Java coding specification, constant names are required to be capitalized.

final double PI = 3.141592652;
final double PI;
PI = 3.141592652;
public class FinalVar{
    public static void main(String[] args){
        final String THE_STRING = "shiyanlou";

IV. Data Types

Eight data types in java

Four basic data types, byte, short, int, long, represent integers. Note the long type. Variables modified with long need to be followed by L or l, such as long num = 1L; Generally, uppercase L is used to avoid confusion between lowercase L and numeric 1.

Floating point number
The float and double types represent floating-point numbers, which can represent fractional parts. It is important to note that values of float type need to be followed by F or f, otherwise they will be treated as doubles. Values of type double can be added with or without d.

char type
The char type is used to represent a single character. Characters need to be enclosed in single quotes with char a ='a'. Chars can be converted to integers, or if char a can be written as char a = 97. You can also use hexadecimal notation for char a ='\u0061'.

boolean type
boolean types (boolean types) are used to represent true or false values. boolean values in Java cannot be converted to or from integer types or other types


Strings are constants and their values cannot be changed after they are created. String buffers support variable strings.

String s0 = "abc";

String s1 = new String("abd");

Comparison of two different ways

1. Calculate string length

int len0 = s0.length();
int len1 = s1.length();

2. Comparison between equals() and ==

string comparison
equals() method, which determines whether the contents of two string objects are the same. Returns true if the same, false otherwise.

If you want to ignore the case relationship, you can use the equalsIgnoreCase() method

==Method: This method compares whether an object has the same address stored in memory.

String s1 = "abc";
String s2 = new String("abc");
boolean b = (s1 == s2);

The value of variable b is false, because the address corresponding to the S1 object is the address of "abc", and s2 uses the new keyword to request new memory, so the memory address is not the same as the address of "abc" in s1, so the value obtained is false.

3. String connection

There are two ways to connect strings
1. Use+
2. Use concat() method

String s0 = "HelloWorld";
String s1 = "!!!";
String s2 = s0 + s1;
String s3 = s0.concat(s1);
public class StringBuilderTest {
    public static void main(String[] args){
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder(5);
        StringBuilder s3 = new StringBuilder("shiyanlou");
        System.out.println("s1:" + s1.toString() + "\tcap:" + s1.capacity());
        System.out.println("s2:" + s2.toString() + "\tcap:" + s2.capacity());
        System.out.println("s3:" + s3.toString() + "\tcap:" + s3.capacity());


String cannot be modified. The modification to String actually creates a new String object. If you need to modify the contents of a string, you can use StringBuilder. It is equivalent to a container for storing characters.
Initialization method of StringBuilder

# Construct a StringBuilder with no characters and an initial capacity of 16
StringBuilder a = new StringBuilder();

# Construct a StringBuilder with cap and no characters
StringBuilder b = new StringBuilder(int cap);
# Construct a StringBuilder with content initialized to str
StringBuilder  c = new StringBuilder(String str);

5. Operators

Arithmetic operator

Bitwise Operators

Java defines bitwise operators that apply to integer types (int), long, short, char, and byte. Bit operations are converted to binary and then bitwise.

In the example in the table, variable a has a value of 60 (binary: 00111100) and variable b has a value of 13 (binary: 00001101):

public class BitOperation {
    public static void main(String args[]) {
        int a = 60;
        int b = 13;
        System.out.println("a & b = " + (a & b));
        System.out.println("a | b = " + (a | b));
        System.out.println("a ^ b = " + (a ^ b));
        System.out.println("~a = " + (~a));
        System.out.println("a << 2 = " + (a << 2));
        System.out.println("a >> 2 = " + (a >> 2));
        System.out.println("a >>> 2 = " + (a >>> 2));

Logical operators

A logical operator is a statement that logically determines an operand or an equation by an operator.

In the example in the table, assume that Boolean variable a is true and variable b is false:

public class LogicOperation {
    public static void main(String args[]) {
        boolean a = true;
        boolean b = false;
        System.out.println("a && b = " + (a && b));
        System.out.println("a || b = " + (a || b));
        System.out.println("!a = " + (!a));
        System.out.println("a ^ b = " + (a ^ b));

Relational Operators

Relational operators produce a boolean result that calculates the relationship between the values of the operands. If the relationship is true, the result is true; otherwise, the result is false.

In the example in the table, suppose variable a is 3 and variable b is 5:

public class RelationalOperation {
    public static void main(String args[]) {
        int a = 3;
        int b = 5;
        System.out.println("a == b = " + (a == b));
        System.out.println("a != b = " + (a != b));
        System.out.println("a > b = " + (a > b));
        System.out.println("a < b = " + (a < b));
        System.out.println("a >= b = " + (a >= b));
        System.out.println("a <= b = " + (a <= b));
        System.out.println("a > b ? a : b = " + (a > b ? a : b));

Symbolic Operational Priority

6. Small exercises

Calculate the sum of two numbers

//The Scanner class can get console input, but it needs to be imported first
import java.util.Scanner;
//Get Input Value
public class Sum{
    public static void main(String[] args){
        int x, y, sum;
        Scanner myObj = new Scanner(System.in);
        x = myObj.nextInt();
        y = myObj.nextInt();

        sum = x + y;

7. java keywords and statements

Java keywords have special meaning for Java compilers, they are used to represent a data type, or the structure of a program, etc. Keywords cannot be used as variable names, method names, class names, package names.

Java keywords are listed in the following table. There are currently 50 Java keywords, of which "const" and "goto" have no specific meaning in the Java language.

8. Methods in java

public void functionName(Object arg) {
  System.out.println("Hello World.");

In the grammar description above:

1 Access modifier: Represents the range of permissions a method is allowed to access, which can be public, protected, private, or default, where public indicates that the method can be called by any other code.

2 Return value type: The type of method return value, if the method does not return any value, the return value type is specified as void (representing no type); If the method has a return value, you need to specify the type of return value and use the return statement to return the value in the body of the method.

3 Method Name: is the name of the method, legal identifiers must be used.

4 Parameter List: A list of parameters passed to a method that can have multiple parameters separated by commas. Each parameter consists of a parameter type and a parameter name separated by spaces. When a method is called, a value is passed to the parameter. This value is called an argument or variable. A list of parameters refers to the type, order, and number of parameters of a method. Parameters are optional and the method may not contain any parameters.

5 Method body: The method body contains specific statements that define the function of the method.
Methods can be divided into four categories based on whether they have parameters or return values:

  • Parameterless no return value method
  • No-parameter return value method
  • No return value method with parameters
  • Return value with parameters method
public class MethodDemo{
    public static void main(String[] args){
    //Add the static keyword here because static methods can only call static methods
    public static void method(){
        System.out.println("Method Called");

Tags: Java Back-end

Posted on Thu, 02 Dec 2021 15:00:28 -0500 by ExpertAlmost