Advanced Variables

Vocab Bar
Casting A way to change the type of an expression
Escape Characters Specific characters that allow special characters in Strings


Code Bar
String A variable representing text.

In a previous lesson we covered primitive variables. Now we will be reviewing Advanced Variables, specifically Strings. Strings are any line of text. They in technical terms are a sequence of ASCII characters.

    String text = "helloWorld";

This String helloWorld is stored into the variable text. The helloWorld, or the actual value the variable is being set to, is the String literal. String literals must be enclosed with double quotes.

Inside the String, we have a special characters sequences, called escape characters to do specific tasks that usually cannot be done. For example, when defining a String, you use double quotes. So putting a double quote inside the String would mess up the program. To put double quotes inside a string, we use a backslashes.

    System.out.println("\"four score and seven years ago\””);

The above line of code will produce:

    "four score and seven years ago"

Often programmers need to print multiple lines. Instead of having multiple print lines, you can use \n. The \n will break the line. The code below will print first line, breaks the line, and repeats for the second and third line.

    System.out.println("first line\nsecond line\nthird line");

That will produce lines:

    first line

    second line

    third line

The next, and debatably most important escape character, is the tab escape. The tab escape prints a space which rounds the length up to the next 4. For example, if you have a line with three characters, a \t will produce a tab length 1. But if you have a line with five characters, the tab will be length 3. Take a look at the code below.

    System.out.println("1\tend");

    System.out.println("12\tend");

    System.out.println("123\tend");

    System.out.println("1234\tend");

This code will produce a good example on the rounding of tab escapes:

    1   end

    12  end

    123 end

    1234    end

Since backslashes are used in writing the escape characters, backslashes cannot be printed stand alone. There is also the escape character //; this prints a backslash. The forward slash, or /, does not have an escape character, and therefore can be put normally in String literals.

    System.out.println("  /\\");

    System.out.println(" /  \\");

    System.out.println("/    \\");

    System.out.println("\\    /");

    System.out.println(" \\  /");

    System.out.println("  \\/");

The code above will the print a diamond:

      /\
     /  \
    /    \
    \    /
     \  /
      \/

The String, however, also has primitive-like characteristics. To define a String, you follow the primitive setup:

    String text = "This is a String Literal!";

You can redefine Strings in the same way and add to it as well.

    String text = "This is a String Literal!";

    text = "Now it has changed."

    text += " and again.";

Importantly, you cannot subtract from Strings. You can only add. To subtract from Strings, there is a method in the String class, called a substring. To invoke substring on a String, type the string name, place a dot afterward, and follow that with parenthesis and a semicolon. Inside the parenthesis you can put either 1 or 2 integers. If you only put one number, the text loses the first n characters (n being the number inputted). If you input two integers, then the String would be the characters with index above or equal to the first number, but below the second. The first character has index 0, not 1.

    String text = "Lorem Ipsum";

    System.out.println(text);

    text = text.substring(6);

    System.out.println(text);

    text += " and this";

    System.out.println(text);

    text = text.substring(6, 9);

    System.out.println(text);	

This code will first print “Lorem Ipsum”. Then it will cut off the first 6 characters “Lorem ”, leaving “Ipsum”, which will be printed out. Then it adds “and this” to “Ipsum”. So Java will print “Ipsum and this”. And finally the last substring returns everything above or equal index 6 and less than index 9, leaving “and”.

All advanced variables have aspects that differ from primitives. However, sometimes it is necessary to change an advanced variable or even a primitive into a different primitive. Programmers can do this by casting. Casting can only be applied on certain variables to certain variables. For instance, all numeric variables can be cast into other numeric variables: floats to ints, doubles to longs. Also, all variables can be cast into Strings. Additionally, Strings cannot be cast into numeric variables.

Whenever you change a number that has decimal places, into a variable that has no decimal places, it doesn’t round. If you were to cast the double 1.5 into an int, it would have the value 1. This is because instead of rounding the number, Java truncates the number. This means for negative numbers, it truncates upward.

To cast, you only need to add a parenthesis with the type inside.

    String text = (String) ((double) 5);

    System.out.println(text);

The code above prints "5.0" because the casting in inner parenthesis changes the int 5 into a double. By changing it into a double, the value is changed to 5.0. Than the outer casting changes the double into a String (the String being "5.0"). Normally, you wouldn’t need to cast the double into a String, because defining the double as a String it automatically casts the variable.

Written by Jason Zhang

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.