Java basics, arrays, string variables

one type

Definition: In Java, a class is a basic object-oriented programming concept. A class is a user-defined data type that contains properties and methods. Properties represent the state of the class, and methods represent the behavior of the class. The use of classes can encapsulate related data and behaviors together, making program organization and management more convenient.

//案例实现
public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void sayHello() {
        System.out.println("Hello, my name is " + this.name + " and I'm " + this.age + " years old.");
    }
}
  • This class represents a person, including two attributes of name and age, and a method to say hello. Among them, nameand ageare private attributes, which can only be accessed inside the class, sayHelloand methods are public methods, which can be called outside the class.

  • Here is the code of how to create a Person object :

    • Person p = new Person("Tom", 20);
      p.sayHello();
      
  • This code creates a Person object named Tom with an age of 20 and calls a sayHellomethod on that object. Executing this code will output the following:
    • Hello, my name is Tom and I'm 20 years old.
      

​​​​​​​​Two : Array

Definition: An array is a data structure that can store multiple elements of the same type. In Java, an array is a type of object, and array elements can be accessed through array variables.

The following is the definition and initialization of a simple array:

  • ​
    int[] nums = {1, 2, 3, 4, 5};
    
    ​
  • This array contains 5 integer elements, and the array elements can be accessed through subscripts, for example:
    • System.out.println(nums[0]); // 输出1
      
  • Arrays can also be looped over all elements, for example:
    • for (int i = 0; i < nums.length; i++) {
          System.out.println(nums[i]);
      }
      
  • This code will output all the elements in the array in turn.

Three: string variable

Definition: A string is a data type that represents text, and in Java, a string is an object. String variables can hold one or more characters, or an empty string.

  • The following is the definition and initialization of a simple string variable:
    • ​
      String str = "Hello, world!";
      
      ​
  • This string variable holds a string "Hello, world!". The length, substring, etc. of the string can be obtained through the method of the string variable, for example:
    • System.out.println(str.length()); // 输出13,表示字符串的长度
      System.out.println(str.substring(0, 5)); // 输出"Hello",表示字符串的前5个字符
      
  • String also supports operations such as string concatenation and string formatting, for example:
    • String s1 = "Hello";
      String s2 = "world";
      String s3 = s1 + ", " + s2 + "!";
      System.out.println(s3); // 输出"Hello, world!"
      String s4
      

The character sequence inside the String object can be stored in two ways: heap memory and string constant pool.

  • Heap memory storage method
    • When we create a String object through the new keyword, the character sequence of the object will be stored in Java's heap memory. The String object created in this way can be referenced by multiple reference variables and can also be recycled by the garbage collector.
      • String str1 = new String("hello");
        String str2 = new String("hello");
        
    • The above code creates two different String objects, and their storage locations in the heap memory are different, even though their character sequences are the same.
  • String constant pool storage method
    • The string constant pool is a special storage area for storing string constants. When we use a string constant to create a String object, the character sequence of the object will be stored in the string constant pool. String objects in the string constant pool can be shared by multiple reference variables because they have only one copy in memory.
      • String str3 = "hello";
        String str4 = "hello";
        
    • The above code creates two String objects, both of which are created using string constants, so their positions in the string constant pool are the same.
  • In general, String objects in Java can be stored in two ways: heap memory and string constant pool. When using the new keyword to create a String object, the character sequence of the object will be stored in the heap memory; when using a string constant to create a String object, the character sequence of the object will be stored in the string constant pool. The string objects in the string constant pool can be shared by multiple reference variables, but the string objects in the heap memory cannot.

Finish:

Before closing this IT blog, I would like to express my thanks and best wishes to all readers. Thank you for your reading and attention, and thank you for your interest and enthusiasm in the IT field.

As a person who is learning and exploring the IT field, I know that there is still a lot of room for improvement in my level in many aspects. But I will continue to learn and update the blog in order to share my knowledge and experience with readers and improve my blog production level.

I hope my blog can help you solve problems and stimulate your interest and enthusiasm. I also hope that you can provide more suggestions and feedback to help me improve and progress continuously.

Finally, I wish every reader can go further and further in the journey of exploration in the IT field, continuously improve their skills and level, and realize their dreams and goals. thank you all!

 

        If there is an error, please contact the author to change

        

        

                

Guess you like

Origin blog.csdn.net/screamn/article/details/129961269