Home » Online Computer Education » Learn Fundamentals of Java Programming “String Variables in Java Programming ” Lesson 5

Learn Fundamentals of Java Programming “String Variables in Java Programming ” Lesson 5

String Variables

In the Percentage Calculator program of the previous section, we used variables to store numeric data. Quite often we want variables to store textual data, for example, the name of a student.

A variable of the primitive data type char can be used to store a single character. To assign a value to a char variable we enclose the character between single quotes.

char middle_name = ‘M’;

To store more than one character, we use the String class in Java. To assign a text value to a String variable we enclose the text between double quotes. For example,

String first_name = “Mayank”;
String last_name = “Saxena”;

To print char or String variables, we can use the System.out.println() method.

System.out.printl n(first_name+” “+ middle_”+last_name);

The output from the statement given above is:

Mayank M Saxena
Note the use of the + operator. In Java, when used with numbers as operands, the + operator adds the numbers. When used with Strings as operands, the + operator concatenates the strings together. Also, note that the single space String literal (” “) is used so as to print a gap between the first and middle name and middle and last name.


The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *