Skip to main content

HOW TO CREATE A STRING IN JAVA

Simple example to use string in different ways.

Syntax :

String variable_name = "Hello World!"

Example :

  1. public class String_Demo{
  2.     public static void main(String args[]){
  3.       char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
  4.       String helloString = new String(helloArray); 
  5.       System.out.println( helloString );
  6.    }
  7. }

Description :

Step 1:Starting of class
Step 2:Start of main function.
Step 3:Define a character array
Step 4:Defining and initializing "helloString". Here value of the variable "Hello array" is copied to the String variable "helloString".
Step 5:Display content of variable "helloString" to console.