Strings are non-primitive data-types, which stores a sequence of characters. Ex – ‘apple’ is a string, its character sequence is a, p, p, l, e.

** remember: in java, we represent string with double quotes (“ ”). Using single quotes (‘ ’), we represent characters. All string variables are instances of ‘string’ class.

Strings are non-primitive?

Strings are a sequence of characters. It’s mean, strings are derived from characters. String is non-primitive because only class can have methods. Primitive cannot. And String need many functions to be called upon while processing like substring, indexof, equals, touppercase. It would not have been possible without making it class.

We can create strings using string literal or using ‘new’ keyword.

Creating a string

  1. Using string literal(using double quotes)

String a=”microcodes”;

  • Using ‘new’ keyword

String a = new String (“microcodes”);

Remember: when we create a string using ‘new’ keyword, we basically creates a string object. From the second upper code example, you see string object is ‘a’ and String() is a constructor,

Both expressions give you a String object, but there is a subtle difference between them. When you create a String object using the new keyword, it always creates a new object in heap memory. On the other hand, if you create an object using String literal syntax e.g. “microcodes”, it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it already exists. Otherwise, it will create a new string object and put it in a string pool for future re-use. In the rest of this article, why it is one of the most important things you should remember about String in Java.

Let’s make a code, where you get input from user and print that string as output.

import java.util.Scanner;
class code1{
    public static void main(String[] args) {
        Scanner sc=new Scanner(;
System.out.println(“Enter your string: “);
        System.out.println(“your string is “+b);

Let’s make a code, which will print a string value and variable data-type also.

public class code2 {
    public static void main(String[] args) {
        String name=”microcodes”;
System.out.println(“type of variable(name) “);

** we have already discuss about .getClass() and .getSimpleName() methods in the variables and data-types article.

We will discuss about string operations. Visit next article for this…