Broad Network


Java String Basics

Java Basics - Part 10

Forward: In this part of the series, I explain the basics of string, which is text as a group, in Java.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 10 of my series, Java Basics. In this part of the series, I explain the basics of string, which is text as a group, in Java. I assume you have read the previous parts of the series, because this is a continuation.

Character
Text is made up of characters. 'a', 'A', 'b', 'B', '5', '8' are examples of characters. Note: a digit is a character if it is in single quotes. A digit that is not in single quotes is a number. So, '2' is a character, while 2 is a number. Each of 'a', 'A', 'b', 'B', '5', '8' is a character literal. A literal is the actual value for a variable. You can assign a character literal to a variable in a statement as in:

    char varA = '7';

and

    char varB = 'F';

Remember, char means character type.

String Literal
A string literal is text in double quotes. "Hello World", "I love you.", "This is a sentence.", and "I am sentence A. You are sentence B in the same string", are four string literals. Just as you have the data (object) type, int, for a whole number literal, you have the data type, String, for a string literal. You can assign a string literal to a variable as in the following statements:

    String oneA = "the first phrase";

    String twoB = "the second phrase";

    String threeC = "the third phrase";

In the first statement, the data type is String, the variable is, oneA; this is followed by the assignment operator and then the string literal. Do not forget the semicolon at the end of a statement. In the second statement, the data type is of course, String and the variable is, twoB. In the third statement, the variable is, threeB.

Note: Java is case sensitive and so the sting type has to be written as String, and not STRING or STRing or StRiNg, etc. The casing of the data type has to be respected as in the above three statements.

String Length
If you have a string, Java can give you the length of the string. This is the number of characters that are in the string. The syntax is:

    stringVariableName.length()

Note the dot between the string variable name and the reserved word, length. Also note the parentheses just after length. The return whole number can be assign to an int variable. Read and try the following example that has a string of 31 characters including the full stop.

class StrLen
    {
        public static void main(String[] args)
            {

                String str = "Just assume I am a long string.";

                int len = str.length();    

                System.out.println(len);

            }
    }

The output should be 31. Simple! The first statement in the main method declares the string with the variable, str. The second statement counts the number of characters with the expression, “str.length()” and assigns the answer to the int variable, len. The third statement uses len without quotes (since it is a variable) to print 31, which is the value of len, to the console (output).

Now, instead of using the string variable in an expression like “str.length()”, you can use the string literal in place of the variable as in the expression:
    
    "This is string literal".length()

There is the dot between the string literal and the reserved word, length. Do not forget the parentheses after the reserved word, length. Read and try the following program that illustrates this:

class StrLiteral
    {
        public static void main(String[] args)
            {

                int len = "This is some remark".length();

                System.out.println(len);

            }
    }

Concatenating String Literals Together
Hey, you can concatenate string literals. Concatenation means joining. You can join string literals. It is simple; just use the + sign as in the following statement:

    String str3 = "First string 1" + " Second string 2";

The variable, str3, will then have, "First string 1 Second string 2". Read and try the following code that illustrates this:

class StrConca
    {
        public static void main(String[] args)
            {

                String str3 = "First string 1" + " Second string 2";

                System.out.println(str3);

            }
    }

Note that in the expression, “System.out.println(str3)”, str3 is not in quotes because it is a variable. You can concatenate more than two strings by just using the plus sign as in:

                String str4 = "First string 1" + " Second string 2" + " Third string 3";

If you do not want the last character of a string to be right next to the first character of the next string, then begin the next string with a single space, as in the above statement of three strings (even the previous two strings).

I hope you are beginning to like Java. I have more interesting things for you. When you finish the complete course, you will be able to produce many of the fascinating things you see on Internet and office computer screens.

String and the Output
I have been using the expression - System.out.println() - to output text. In other words, I have been using the expression to output string. Now, if what goes into the parentheses is a number, then the number should not be in quotes, for example:

    System.out.println(35)

If what goes into the parentheses is a character literal, then it is typed in single quotes as in:

    System.out.println('A')

If what goes into the parentheses is a string literal, then it goes in double quotes as in:

    System.out.println("Yes the Phrase")

If what goes into the parentheses is a variable; it does not matter whether the variable holds a number or character or string literal; in this case, the variable is typed without any quotes as in:

    System.out.println(str1)

where str1 is the variable.

Note: The quotes (single or double) you get from a word processor are not the same like the ones you get from a text editor. In programming code, it is the quotes from the text editor and not the ones from the word processor that matter.

Well, the above is what I have for Java string basics. We take a break here and carry on in the next part with other Java basics.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message