All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Initialize String In Java

Last Updated : Mar 11, 2024

How To Initialize String In Java

In this article we will show you the solution of how to initialize string in java, as usual first we need to import package in case you are using eclipse then all program will import by itself during creation.

As we know string is immutable in java in that case, prefer either direction or indirection initialization process to achieve your goal. Here we used first term of process direct initialization and printed that value at terminal.

Step By Step Guide On How To Initialize String In Java :-

The main class of ‘Str_prgm’ we provided main method definition. In our example we followed direct initialization, so firstly you have to declare string data type variable.

Then you need to think about to initialization values that also need to string data type. And, if you giving number with double quotes then it will also considered as string data type instead of integer data type.

So you cannot do calculations with that value string value and addition operation also gives you concatenation result. Ensure initializing value and try to give text based word input.

//String Initialization
package java_prgms;
class Str_prgm
 public static void main(String args[])
        String str="Hi Everyone";
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class ‘Str_prgm’ we given public static void main method definition. Within that you have to provide string variable with string value.
  3. So we declared variable ‘str’ with string data type and initialized with ‘Hi Everyone’ string data type value. You can also change this string with any other as per your wish.
  4. Using standard outstream println() method we printed variable ‘str’ at end of code.
  5. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  6. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to initialize string in java. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Str_prgm‘ line for gets the output.

Then you can see output 'Hi Everyone’. On eclipse just press of run button then you will get output inside terminal.

If you want to do changes then you can modify according to changes you will get output.

Later we will discuss about indirect string initialization process in detail with proper example program to clarify your doubts thoroughly. Then you will get output based on your modification.

I hope this article on how to initialize string in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪