String Literal in java With Example using NetBeans
Description:
A string literal is a value that consists of multiple consecutive characters. “Hello world” or “Hello” are string literals. String literals can be printed to the screen or assigned to variables. So in this article, I will explain how to write string literals in Java programs.
In this article you will learn the following topic:
- Representation of string literals in java
Representation of string literal in Java
A string literal is a value consisting of multiple characters, and is a literal that is used very frequently, such as when displaying a message on the screen. String literals in Java are expressed by enclosing them in double quotation marks (“).
“Hello world”
“Hello”
In other programming languages, character strings can be expressed by enclosing them in single quotation marks (‘) in addition to double quotation marks, but an error will occur in Java.
A string literal can be a single character. An empty character (0 character) is also acceptable.
“B”
“”
Character literals can be printed on the screen or stored in variables. (A string literal can be assigned to a variable of class String.
System.out.println(“Hello”);
String str = “Hello”;
How to use String Literal in java:
Let’s create a simple program and try it. After writing the following code in a text editor or any ide, save it as StringLiteralExample.java.
Then Open the command prompt and type
javac StringLiteralExample.java
Then press enter, after pressing the enter button simply type java StringLiteralExample, and press the enter button. But in my case I am using the NetBeans ide
package com.mycompany.javabasics;
/**
*
* @author Fawadkhan
*/
public class StringLiteralExample {
public static void main(String[] args) {
System.out.println(“Programming digest”);
System.out.println(“Programming”);
}
}
Output:
It printed the string literals “Programming digest” and “Programming” respectively to the screen.
I explained how to write a string literal in a Java program.