JAVA learning - splicing array, static keyword, JavaBean specification

recollections ...
recollections

At this stage, I feel that I can't write a blog with too many rules to follow. I don't move my pen and don't read. In fact, I also like to read and copy a paragraph with a pen when I used to learn new things, but did I really remember it in my mind? No, I really didn't remember it at all. I just went to practice calligraphy.

Now when I write a blog, I also feel that I want to make some knowledge and aftertaste it in my mind, so it should be how I write and how to come! After reading the summary of knowledge points written by many leaders, it is very short and the summary is also very good. After reading it, I benefited a lot. It's really comfortable!

Some time ago, I would also consider my own things. If possible, I might be seen by others. Isn't it hard to write nonsense. But I feel too beautifying myself and summarizing what I write, but I can't grasp my rhythm. In short, what I write is for myself. Blogging is to make the knowledge point more impressive in my mind. Sometimes I still feel strange after learning a certain knowledge point. After writing a blog, I feel better. Sure enough, after reviewing the knowledge points, I'd better see the summary of the big guys.

Section splicing array

1. Small exercise: define a method to splice the array into a string according to the specified format. The format reference is as follows, [word1#word2]

package Demo04; public class StringCh { /* Small exercise: define a method to splice the array into a string according to the specified format. The format reference is as follows, [word1#word2] */ public static void main(String[] args) { int[] ints = new int[]; toString22(ints); } public static void toString22(int[] ints) { for (int i = 0; i < ints.length; i++) { if (i == 0) { System.out.print("[word" + ints[i]+"#"); } else if (i > 0 && i < ints.length - 1) { System.out.print("word"+ints[i]+"#"); }else { System.out.print("word"+ints[i]+"]"); } } } }

2. Output results

3. Summary

1. When i coded the code, i found that when i added a conditional statement to the if conditional statement, i typed it as int. i seemed to have made such a mistake before, but i found it later.

2. I have made the error of array crossing the boundary before. This time I understand the principle and avoid it perfectly.

3. finally, when calling the method in the main method, I used the printout and didn't know what I thought. The method of no return value should be used only in the way of direct call, the program was wrong, it could not be empty, and there was a longer memory here.

Statistics of character types

  1. Enter a string and count the number of occurrences of various characters. The types are: upper and lower case letters, numbers and others

Analysis: this problem is still relatively simple. Enter a string and count the number of characters in it. Then you must split the string array and compare the characters one by one. When I first thought about this code, I didn't think how to judge what type of number it was. I held it for a long time. I thought whether it could be compared through a range such as ASC II code, but I also forgot the specific range. I wanted to write this topic myself by consulting the data

No, the code

package cn.java.day001.Demo01; import java.util.Scanner; public class MethodToStarry { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a string:"); String str = sc.next();//Here, the method of sc object is called, and then a String type variable is defined to receive char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.println(chars[i]); } int sumShuZi = 0; int sumDaXie = 0; int sumXiaoXie = 0; int sumQiTa = 0; for (int i = 0; i < chars.length; i++) { if (chars[i] >= '0' && chars[i] <= '9') { sumShuZi++; } else if (chars[i] >= 'a' && chars[i] <= 'z') { sumXiaoXie++; } else if (chars[i] >= 'A' && chars[i] <= 'Z') { sumDaXie++; } else { sumQiTa++; } } System.out.println("There are numbers in the string you entered" + sumShuZi + "individual"); System.out.println("Lowercase letters have" + sumXiaoXie + "individual"); System.out.println("Capital letters have" + sumDaXie + "individual"); System.out.println("Other characters are" + sumQiTa + "individual"); } }

  At first, I didn't pay attention to the equal sign, so when I input the relevant symbols, I found that the odd variables output either more or less. Later, I checked the relevant code and found that the equal sign is written less. If there is no equal sign, it is an open interval. Obviously, there will be two fewer numbers in each range, When you enter the numbers a, Z, a, Z, 0 and 9, the code will not consider them to be within the range, so this is the reason why it didn't succeed at first. Hey, it's a low-level problem.  

Section static keyword

1. The static keyword is first added after the modifier of the variable or method. If the variable or method is added with the static keyword, it means that the variable already belongs to the class, and the class can be used to call the method directly

2. private static int variable name, the objects of this class created share the data of this static variable

3. public static void method name, the created objects of this class share the data of this static method

4. After the general member method is modified by the static keyword, it is a static method. When it is used later, it is a general method created in the same state of the main method. After creation, you can directly use the class name to use the method directly.

5. After adding the methods and variables of the static keyword, the variables of this class generally cannot access the static variables or methods.

Such as code output

package cn.java.day001.Demo01; public class StaticMethod { private int age; private String name; static int room; public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public static int showName(int room) { int daRoom = 0; daRoom = this.age + 1; System.out.println("What's your name here"); return daRoom; } }

Here I randomly created some member variables and methods.

It is obvious that there is an error. Of course, I didn't find it on my own initiative. It was found after the program was running. Originally, this keyword can't be used in static methods. This is a contradictory problem. When static methods are used later, they are called with the class name, but this refers to the current object, so there is no object. IDE explained to me that it was not possible to call non static from static context.

6. Static static code block

Although the above is static, the knowledge points have little to do with the previous static code and static variables, which need to be memorized again.

The so-called static code block is to call the code when the class is used for the first time, but it will not be called again. Therefore, I feel that it is a bit biased towards disposable products, that is, it will be used once when instantiating the object, and it will not be used later. The specific call does not use the object, and the class name can be called stably. The specific application is unknown.

7. Strictly use the calling methods of static variables and member methods

In terms of memory, it seems that the creation order of static code takes precedence over member code. It should be in the method area. There is a static area in itself, that is, it keeps pace with the priority of class files. The calling of member code, that is, it is impossible to call member code in static methods, because it has not been seen in static methods, Member code is OK, and static code is created first. Just look at the static code block and run it together with the first call of the class.

Section JavaBean

1. JavaBean s recalled from memory

I've heard of it before, but I haven't seen it too early in the later study. Later, I learned that it is actually a specification. The original explanation is that JavaBeans are a standard specification for classes written in the Java language. Classes that conform to JavaBean are required to be concrete and public, have parameterless construction methods, and provide set and get methods for operating member variables.

I can't understand this sentence, and my brain refuses to think about the specific meaning of this sentence, so I'd better read the explanation directly.

Then I'll summarize what I've learned about the method of creating a class. This class is not used for operation, that is, a drawing. When creating a drawing, there must be general attributes, corresponding to member variables, and general behavior, corresponding to member methods. Then, if you want to make it easier to enter data later, create a construction method with parameters after a construction method without parameters and contents. At the same time, set the parameter value to automatically assign values to the defined member variables. Later, if you are afraid that users do not obey the rules when using variables, you have to add a private keyword and create a set and get method. Generally, these things are available and basically comply with the specification of a JavaBean.

Finally, that is

In a basic class, there must be member variables, member methods, parameterless construction methods, and set and get methods used in encapsulation. With this, JavaBean s are compliant.



6 October 2021, 23:50 | Views: 7884

Add new comment

For adding a comment, please log in
or create account

0 comments