Home > Java > javaTutorial > body text

How to use String in Java?

WBOY
Release: 2023-05-09 11:13:07
forward
1395 people have browsed it

1.String Overview

The String class is under the java.lang package, so there is no need to import the package when using it.

The String class represents a string, and all string literals (such as "abc") in Java programs are implemented as instances of this class.

2. Features

  • Strings are immutable, their values ​​cannot be changed after creation

  • Although the value of String are immutable, but they can be shared

  • Strings are effectively equivalent to character arrays (char[]), but the underlying principle is byte arrays (byte[])

3.String construction method

##public String(char[ ] chs)Create a string object based on the contents of the character arraypublic String(byte[ ] bys)Create a string based on the contents of the byte array ObjectString = “abc” Create a string object by direct assignment, the content is abc
Project Value
public String() Create a blank string object without any content
4.Characteristics of String objects

For string objects created through new, each new will apply for a memory space. Although the content is the same, the address value is different

char[ ] chs = {'a','b','c'};

String s1 = new String(chs);

String s2 = new String(chs);

In the above code, the JVM will first create a character array, and then there will be a new address every time new is issued, but the string contents referenced by s1 and s2 are the same.

5. Comparison of strings

Use "==" for comparison

  • Basic type: What is compared is whether the data values ​​are the same

  • Reference type: What is compared is whether the address values ​​​​are the same.

Strings are objects. The comparison of whether the contents are the same is implemented through a method. This method is called: equals()

public boolean equals(Object anObject); Compare this string with the specified object. Since we are comparing string objects, the parameter is passed directly to a string.

6. Implement user login

The code is as follows:

import java.util.Scanner;

public class ArrayDemo{

    public static void main(String[] args) {
		   String username="abc";
		   String passworld="123";
		   
		    //使用循环实现三次登陆机会
		   for(int i=0;i<3;i++){
               Scanner sc=new Scanner(System.in);

               System.out.println("请输入用户名:");
               String name=sc.nextLine();

               System.out.println("请输入密码:");
               String pwd=sc.nextLine();

               if(name.equals(username)&&pwd.equals(passworld)){
                   System.out.println("登陆成功!");
                   break;
               }else{
                   System.out.println("用户名或密码错误,请重新登录!");
               }
           }
    }
}
Copy after login

The above is the detailed content of How to use String in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template