Home > Java > JavaBase > body text

How to define string in java

王林
Release: 2020-05-15 11:35:41
Original
6399 people have browsed it

How to define string in java

There are two ways to instantiate strings in java, namely:

1. Direct assignment

String str = "Hello World";
Copy after login

2. Constructor instantiation

String str = new String("Hello World");
Copy after login

(Video tutorial recommendation: java video)

Specific example:

//直接赋值
public class StringDemo{
	public static void main(String[] args) {
		String str1 = "Hello qty";
		String str2 = "Hello qty";
	}
}
//构造方法实例化
public class StringDemo{
	public static void main(String[] args) {
	String str1 = new String("Hello qty");
	}
}
Copy after login

Recommended tutorial: java entry program

The above is the detailed content of How to define string in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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