All TalkersCode Topics

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

How To Declare String In Java

Last Updated : Mar 11, 2024

How To Declare String In Java

In this article we will show you the solution of how to declare string in java, here you have to import packages, in case you using eclipse then each program have itself.

After public class definition you can easily declare any type of variable under ‘public static void main()’.

To declare variable java has some predefined rules you need to follow them. Below shown example will guide to achieve expecting result by your own.

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

Package is must, otherwise they cannot understand the code what you defined.

The class and void main definition are common and necessary thing actually for each program.

We know each programming code has rules for each process. Same as here you have to define data type of variable and variable name.

Then you can set your value as per your wish but they need to same data type. Otherwise error occur, and printing output mostly user know I think.

package java_prgm;
public class Test{
    public static void main(String[] args)
        String s="Harini";
        system.out.println("String is = "+s);
  1. The ‘Package java_prgm’ 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. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. Without this you can compile but not possible to run successfully, because it throws error ‘main method not found’.
  4. We know variables are container for storing data values. In java, there are different data types of variables exists.
  5. To declare variable has same syntax but the differ is data types. When you want to declare string variable first write ‘String’-data type.
  6. Then define variable name as per your wish, here used ‘s’ as variable name. Then string value are surrounded by double quotes text.
  7. Within quotes you can define any text value as value for it. To print anything in java you can use print() or println() methods.
  8. 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.
  9. 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 declare string variable 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 Test‘ line for get the output. Where you can see message ‘String is = Harini’.

The output may vary based on your string value, try to explore more. On eclipse just press of run button then you will get output inside terminal.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪