12.3 literal
1. We can create String object by using literal
a). String name ; // Declaration
name = “Nireekshan” ; // Initialization
(or)
b). String name = “Nireekshan” ;
-------------------------------------------------------------------
Program : Creating String object by using literal
Program name : Demo1.java
Output :
First name is : Prasad
Second name is : Kumar
class Demo1
{
public static void main(String args[])
{
String name1 ;
name1 = “Prasad” ;
String name2 = “Kumar”;
System.out.println(“First name is:”+name1);
System.out.println(“Second name is:”+name2);
}
}
Compile : javac Demo1.java
Run : java Demo1
Output :
First name is : Prasad
Second name is : Kumar
-------------------------------------------------------------------
Note :
- If we are giving anything between double quotes that will come under string data, even we did same thing in Hello World program
- If we assign that string data to String object then we can reuse in program if it is required.
Thanks for your time.
Nireekshan