Java Language Basics Article

Share this article

Variables (cont’d.)

Once created and assigned a value, a variable may be used anywhere the value it contains might be used. Let’s look at a simple program that prints out the room temperature as stored in a variable. Open your text editor and type the following (remember, don’t type the line numbers), then save the file as PrintRoomTemp.java:

1  /**  
2   * PrintRoomTemp.java  
3   * A simple Java program that prints out the temperature  
4   */  
5    
6  class PrintRoomTemp {  
7    public static void main(String[] args) {  
8      int roomTemp = 20; // Room temperature  
9    
10     // Print out the temperature  
11     System.out.print("Current room temperature: ");  
12     System.out.print(roomTemp);  
13     System.out.print(" degrees Celsiusn");  
14   }  
15 }

This program is a lot like the Hello program we saw above, with a few notable exceptions:

  • Line 8: The first thing this program does is to create a variable called roomTemp and assign it a value of 20.
  • Lines 11-13: In Hello, we used System.out.println(...) to print messages on the screen a line at a time. In this program, we have used System.out.print(...) instead. The only difference between these two commands is that println prints a line break at the end of its output, so that the next character to be displayed on the screen will appear at the start of the next line. print doesn’t add this line break, so several print commands may be strung together to print a single line on the screen.
  • Line 12: This is an example of using a variable where a value would normally be expected. Everywhere else that we’ve used print or println, we’ve fed it a string of text (surrounded by double quotes) as the exact value to be printed out. In this case, we’ve given it a variable name instead. For the print command to know what to print out, it has to look inside the variable for the value stored within. So this line just prints out whatever value is stored in the roomTemp variable at the time.
  • Line 13: Since this print statement represents the end of the line of text that we want to display, you could use a println instead to output the requisite line break. Instead, I elected to stick with a print statement to demonstrate how to output a line break without using println. Notice that the text string to be printed out by this line ends with n. This is a special character code that, when printed out by Java, gets converted to a line break. If a situation arises where you want to actually print out a backslash followed by the letter ‘n’, you must prefix your backslash with a second backslash (i.e. “\n“). The first backslash cancels the special meaning of the second. In other words, \ is a special character code that gets converted to a single backslash when printed out, so Java no longer sees the n as a special character code.

Compile the program as usual by typing javac PrintRoomTemp.java at the command line, and then, assuming the compiler didn’t point out any typing errors, run the compiled version of the program:

D:javaJavaLanguageBasics> java PrintRoomTemp
Current room temperature: 20 degrees Celsius

Although there were a few new tricks in this program, its main purpose was to demonstrate how to use a value stored in a variable. In this case, we printed the value out, but as you’ll see in later examples, variables can be used in many other ways as well.

Go to page: 1 | 2 | 3 | 4 | 5 | 6 | 7
Kevin YankKevin Yank
View Author

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week