java day03 - array, JVM memory space, method



array is the most basic data structure - data arrangement. array is the first reference type. It is a container that can store multiple variables

  1. The type of array is fixed. Only data of the same type can be stored
  2. Once the array is determined, the length of the array is immutable
  3. If the index range is exceeded: ArrayIndexOutOfBoundsException array index out of range exception


int[] arr = {element}; – Determine element content directly And length
int[] arr = new int[] {element} – directly determine element content and length
int[] arr = new int[num] – determines the length, and the content defaults

// Array initialization: there are three ways
      // Method 1: initialize while declaring, and determine the array contents
      int[] arr1 = {1, 2, 3, 4};
      // Cannot assign again using mode 1
//        arr1 = {2, 3, 4, 5}// Compilation error

      // Method 2: initialize while declaring, and the array contents have been determined
      int[] arr2 = new int[]{1, 2, 3, 4, 5};
      // Use mode 2 to assign values again
      arr2 = new int[]{3, 4, 5, 6, 7, 8, 9};
      // Method 3: initialize while declaring. Only the length can be determined. All contents are default values
      // Default values: integer -0, floating point -0.0, char-\u0000, Boolean false
      int[] arr3 = new int[5];
      //It can also be assigned again
      arr3= new int[]{3, 4, 5, 6, 7, 8, 9};

Print array

The output is the address of arr
Returns an array in string format

JVM memory space


Method area: stores class related information
Stack: temporary variable, local variable
Heap: real data of reference type, object

Class loading

Read the class information in the bytecode (class file) into the JVM method area,
Done by class loader



It is a function / procedure that contains some code
Grammar / structure
Modifier: public static
Method return value type: void/int/int[]/boolean/String
Method name: main/ optional
(parameter list):int a, int b, parameters are also called temporary variables
{method body}: some code

Naming rules

Method name: lower case first letter, upper case first letter of the second word - hump nomenclature
The length is unlimited, but it must be readable

Return value

Methods with return values need to return results through return
Return indicates the end of the method. For methods without a return value, you can write only return
Basic data type, such as int->return 0
Reference data type, such as int[]->return null

common method


int nextInt(): get the console content, convert it to an int value, and return
double nextDouble(): get the console content, convert it to double and return
String next(): get the console string and return it (when a carriage return or a space is encountered, the reading ends)
String nextLine(): get a whole line of console string and return it (please put it in the front when the carriage return ends)


Void print (content) - > print without line breaks
Void println (empty / content) - > line feed after printing

		// You can print line breaks without writing parameters
        // No line breaks, parameters must be given

        // Common escape characters \n-> carriage return \r-> line feed \t-> tab
        // \->\Itself


double random(): returns a [0,1) random number
double pow(double d1, double d2): returns the d2 power of d1
Escape characters: \n: carriage return \r: line feed \t: tabs \: itself


char charAt(int index): returns the character corresponding to the index position of the string index

		Scanner console = new Scanner(;
        String str =;
        char[] arrChar = new char[5];
        // Cut the string into each character
        for(int i = 0; i < arrChar.length; i++){
            arrChar[i] = str.charAt(i);


String toString (array): converts an array into a string format and returns

Case - guessing numbers game

Letter guessing game:
1. generate 5 random letters - > character array
2. guess the letter - > input the string on the console and convert it into a character array
3. compare two character arrays to get the correct number of characters and positions
4. if you don't guess correctly, continue to repeat steps 2 and 3

public class Demo03GuessGame {
    public static void main(String[] args) {
        Scanner console = new Scanner(;
        // 1. generate 5 random letters
        char[] arrChar = randomChars();
        while (true) {
            // 2. guess letters - > input string
            String s =;
            char[] guess = parseChar(s);
            // 3. compare arrChar and guess
            int[] res = compare(arrChar, guess);
            System.out.print("Correct number of letters: " + res[0]);
            System.out.println(", Number of correct positions: " + res[1]);
            // 4. end condition
            if (res[1] == 5) {
     * Purpose: To compare two character arrays
     * Returns two results: the correct number of letters and the correct number of positions
     * The two results are returned in a variable, so the return value type is determined to be int[]
    public static int[] compare(char[] answer, char[] guess) {
        int[] result = new int[2];
        for (int i = 0; i < answer.length; i++) {
            for (int j = 0; j < guess.length; j++) {
                if (answer[i] == guess[j]) {
                    // The correct number of letters +1
                    result[0] ++;
                    // Judge whether the position is correct
                    if (i == j) {
                        // It indicates that the position is correct. The number of correct positions +1
                        result[1] ++;
        return result;

     * Objective: to get a character array with length of 5
     * Five random letters, no repetition
    public static char[] randomChars() {
        char[] chars = new char[5];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = (char) (Math.random() * 26 + 'A');
            // The position of i and each position in front of i determine whether there is repetition
            for (int j = 0; j < i; j++) {
                if (chars[i] == chars[j]) {
                    i--; // Let the program repeat the generated letters in the original position
        return chars;

     * Purpose: to convert the specified string into a character array and return
    public static char[] parseChar(String str) {
        char[] arrChar = new char[5];
        // Cut the string into each character
        for (int i = 0; i < arrChar.length; i++) {
            arrChar[i] = str.charAt(i);
        return arrChar;

Tags: JavaEE

Posted by eektech909 on Mon, 30 May 2022 13:10:53 +0530