Hello everyone, meet again. I'm Jun Quan. I wish every programmer can learn more languages.
Javastudy Common articles
0x00 Preface
It is inevitable to call various APIs to develop programs in development. Let's learn some related concepts of api first.
0x01 use of API
First, let's take a look at an explanation of the api.
API(Application Programming Interface). Java API is a programmer's dictionary, which is provided to the user in JDK Documentation of the classes we use. These classes encapsulate the underlying code implementation. We don't need to care about how these classes are implemented, just learn Just learn how to use these classes. So we can learn the classes provided by Java and know how to use them by querying the API.
Here is a copy of the api explanation collected online.
As you can see, this api is a description document of the usage class provided to us by jdk.
Steps for using api documents.
1. First check the package. Which package is this class under? If it is in Java Lang packages do not need to be imported. They can be used directly. If they are other packages, they need to be imported.
2. View the explanation and description of the class.
3. Learn the construction method of this class
0x02 Scanner class
Scanner this is a basic that can be parsed Simple text scanner for types and strings.
First, we need to import a class
improt java.util.Scanner
Then, use the constructor to instantiate the object of this class.
Scanner sc = new Scanner(System.in);
Next, you can call his method. First, check the method of this class and call it.
List several methods:
byte nextByte() Scan the next mark of the input information as a byte. byte nextByte(int radix) Scan the next mark of the input information as a byte. double nextDouble() Scan the next mark of the input information as a double. float nextFloat() Scan the next mark of the input information as a float. int nextInt() Scan the next mark of the input information as a int.
Let's receive the number entered by the keyboard here. There is only the netxtInt method.
Scanner sc = new Scanner(System.in); int i = sc.nextInt;
The system here In system input refers to entering data through the keyboard.
For methods that only accept once, we can use anonymous objects to receive.
Format:
new Scanner(System.in);
matters needing attention:
An anonymous object can only be used once
Anonymous objects can also be used as method parameters or return values
public class Input { public static void main(String[] args) { input(new Scanner(System.in)); } private static void input(Scanner sc) { System.out.println(sc); } }
0x03 Random class
This is a class that generates pseudo-random numbers. Before using it, you must first look at its construction method.
Random() Create a new random number generator. Random(long seed) Use single long Seed creates a new random number generator.
After reading the construction method, you can instantiate an object.
Random random = new Random();
After instantiating an object, it can be used to instantiate the called method.
int i = random.nextInt();
In this way, the random class is used to generate a random value and assign it to the i variable
public class Input { public static void main(String[] args) { Random random = new Random(); int i = random.nextInt(); System.out.println(i); } }
0x04 ArrayList class
The array contacted in front can only store fixed length and fixed type. In order to solve this problem, java provides a container arrayList collection class, so that we can store and manipulate objects more conveniently.
First, let's take a look at the construction method.
java.util.ArrayList
: this class needs to be import ed for use. , representing a specified data type, called a generic type. E. From the initial letter of Element. Where e appears, we make Replace it with a reference data type to indicate which reference type elements we will store.
ArrayList() Construct an empty list with an initial capacity of 10. ArrayList(Collection<? extends E> c) Construct a containing the specified collection A list of elements that are in accordance with the collection The iterators of return their ordered. ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.
Instantiate object:
public class Input { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); String s1 ="Xiao Ming"; String s2 ="Xiao Hong"; String s3 ="Little green"; list.add(s1); list.add(s2); list.add(s3); System.out.println(list); } }
The operations of elements are basically reflected in adding, deleting and querying. Common methods are:
Public Boolean add (E): adds the specified element to the tail of this collection.
public E remove(int index): removes the element at the specified position in this collection. Returns the deleted element.
public E get(int index): returns the element at the specified position in this collection. Returns the obtained element.
public int size(): returns the number of elements in this collection. When traversing a collection, you can control the index range to prevent out of bounds.
Then try it all
{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); String s1 ="Xiao Ming"; String s2 ="Xiao Hong"; String s3 ="Little green"; list.add(s1); list.add(s2); list.add(s3); System.out.println("get"+list.get(0)); System.out.println("get"+list.get(1); System.out.println("get"+list.get(2)); System.out.println("zise"+list.size()); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
The ArrayList object cannot store basic types, only reference types, but the wrapper type corresponding to the basic type can be stored. Therefore, if you want to store data of basic class types, the data types in < > must be converted before writing
Basic type | Basic type packing class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Objects can also be stored in collections
public class Input { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<>(); Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); list.add(s1); list.add(s2); list.add(s3); System.out.println(list); } }
0x06 static
For the static keyword, it can be used to modify member traversal and member methods. The modified members belong to classes, rather than just entering an object. In other words, since it belongs to a class, it can be called without creating an object.
When a member is modified by static, the variable is called a class variable. Each object of this class shares the value of the same class variable. No object changes the value of this class variable, but you can also operate on this class variable without creating it.
Define static variable format:
static data type variable name;
static int num;
Define static method format:
public static void method(){ System.out.println("123") }
Precautions for static method invocation: 1. Static methods can directly access class variables and static methods.
2. Static methods cannot directly access ordinary member variables or member methods. Conversely, member methods can directly access class variables or static methods.
this keyword cannot be used in static methods.
3. Static methods can only access static members.
Static modified members need to be accessed directly using the class name, which is similar to the previous interface static methods.
Class name.Variable name Class name.Static method
0x07 Array class
Array contains various methods for manipulating arrays, such as sorting and searching. All its methods are static methods, which can be called It's simple. In other words, you do not need to instantiate an object, you can call it directly through the class name.
Take a look at the use of a static method of this class
sort(int[] a) For the specified int Type arrays are sorted in ascending numerical order. toString(int[] a) Returns a string representation of the contents of the specified array
Code example:
public static void main(String[] args) { int[] arr = {1,29,3,3,64}; String s = Arrays.toString(arr); System.out.println(s); Arrays.sort(arr); String a = Arrays.toString(arr); System.out.println(a); }
0x08 Math class
math is a class used for mathematical operations. Some of its methods are static methods, so we don't need to instantiate the object, so we don't need to learn its construction method. We can call it directly by class name.
Check out some of his methods
abs(double a) return double Absolute value of the value.
code:
public static void main(String[] args) { double abs = Math.abs(-13.3); double ceil = Math.ceil(93.3); System.out.println(abs); System.out.println(ceil); }
These methods are relatively simple. For example, we develop a directory scanner similar to Yujian. We have 23 dictionaries that need to be requested by 5 threads. If each thread requests 4 dictionaries, there will be three more dictionaries. If there is no thread to request, the extra dictionaries will be ignored, What if the ignored dictionary is just the background path? Therefore, these are the points to pay attention to in tool development. We need to open another thread to request some more data, so we can use up rounding at this time.
0x09 end
In developing programs, we always use various kinds, so we can't rely on memorization. We should learn how to check the manual to develop a program.
Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/119954.html Original link: https://javaforall.cn