Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use API in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use API in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use API in Java" article.

Overview of 1.API1.1API

What is API?

API (Application Programming Interface): application programming interface

API in java

Refers to the JDK provides a variety of functions of the Java classes, these classes encapsulate the underlying implementation, we do not need to care about how these classes are implemented, we just need to learn how to use these classes, we can help documentation to learn how to use these API.

Specific use of 1.2API help documentation

Open the help document

Locate the input box in the index tab

Enter Random in the input box

See which package the class is under.

Look at the description of the class

Look at the construction method

Look at the member method

Overview of 2.String class 2.1String class

The String class represents a string, and all string literals in the Java program, such as "abc", are implemented as instances of this class. That is, all the double quote strings in the Java program are objects of the String class. The String class is under the java.lang package, so you don't need a guide package when using it!

Characteristics of 2.2String class

Strings are immutable and their values cannot be changed after creation

Although the values of String are immutable, they can be shared

A string is equivalent to a character array (char []), but the underlying principle is a byte array (byte []).

The construction method of 2.3String class

Common construction methods

Sample code

Public class StringDemo01 {

Public static void main (String [] args) {

/ / public String (): create a blank string object that contains nothing

String S1 = new String ()

System.out.println ("S1:" + S1)

/ / public String (char [] chs): creates a string object based on the contents of the character array

Char [] chs = {'averse,' baked,'c'}

String S2 = new String (chs)

System.out.println ("S2:" + S2)

/ / public String (byte [] bys): creates a string object based on the contents of the byte array

Byte [] bys = {97,98,99}

String S3 = new String (bys)

System.out.println ("S3:" + S3)

/ / String s = "abc"; create a string object by direct assignment, and the content is abc

String S4 = "abc"

System.out.println ("S4:" + S4)

}

}

The specific implementation results are as follows:

2.4 the difference between the two ways of creating string objects

Create by construction method

For string objects created through new, each time new requests a memory space, although the content is the same, but the address value is different.

Create by direct assignment

For a string given in "" mode, as long as the character sequence is the same (order and case), JVM will only create a String object and maintain it in the string pool no matter how many times it appears in the program code.

2.5 comparison of strings the function of the sign 2.5.1

Compare basic data types: compare specific values

Compare reference data types: compare object address values

The function of 2.5.2equals method

Method introduction

Public boolean equals (String s) compares whether the contents of two strings are the same and distinguishes between sizes

Sample code

Public class StringDemo02 {

Public static void main (String [] args) {

/ / get the object by the way of the constructor

Char [] chs = {'averse,' baked,'c'}

String S1 = new String (chs)

String S2 = new String (chs)

/ / get the object by direct assignment

String S3 = "abc"

String S4 = "abc"

/ / compare whether the string object address is the same

System.out.println (S1 = = S2)

System.out.println (S1 = = S3)

System.out.println (S3 = = S4)

System.out.println ("-")

/ / compare whether the string content is the same

System.out.println (s1.equals (S2))

System.out.println (s1.equals (S3)

System.out.println (s3.equals (S4)

}

}

The specific implementation results are as follows:

2.6 user login case 2.6.1 case requirements

Knowing the user name and password, please use the program to simulate user login. Give a total of three opportunities, after logging in, give the appropriate prompt

2.6.2 Code implementation

/ *

Train of thought:

1: know the user name and password, and define two strings to represent it.

2: the user name and password to be logged in are entered on the keyboard and implemented with Scanner

3: compare the user name and password entered by the keyboard with the known user name and password, and give the corresponding hints. Equals () method is used to compare the contents of a string.

4: use a loop to realize multiple opportunities. Here, the number of times is clear, use for loop to realize, and when login is successful, use break to end the loop.

, /

Public class StringTest01 {

Public static void main (String [] args) {

/ / A user name and password are known. Just define two strings to represent them.

String username = "itheima"

String password = "czbk"

/ / use a loop to realize multiple opportunities. Here, the number of times is clear. Use for loop to implement, and when login is successful, use break to end the loop.

For (int item0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report