Home  >  Article  >  Java  >  How to define string in java

How to define string in java

王林
王林Original
2020-05-15 11:35:416345browse

How to define string in java

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

1. Direct assignment

String str = "Hello World";

2. Constructor instantiation

String str = new String("Hello World");

(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");
	}
}

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!

Statement:
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