How are strings defined in Java?

In Java, you can define strings using the following two methods:

  1. A string literal enclosed in double quotation marks. For example:
String str1 = "Hello World";
String str2 = "Java";
  1. Create a string object by using the constructor of the String class. For example:
String str3 = new String("Hello");
String str4 = new String("World");

It is important to note that strings in Java are immutable, meaning once defined, they cannot be changed. If modifications are needed, you can use the StringBuilder or StringBuffer class.

bannerAds