Data and Variables

Methods are only half of a Java class. The other half is data. Consider the following generalization of the HelloWorld program:

// This is the Hello Rusty program in Java
class HelloRusty {

    public static void main (String args[]) {

      // You may feel free to replace "Rusty" with your own name
      String name = "Rusty";

      /* Now let's say hello */
      System.out.print("Hello ");
      System.out.println(name);
  }

}

Here, rather than saying hello to a rather generic world, we allow Java to say hello to a specific individual. We do this by creating a String variable called “name” and storing the value “Rusty” in it. (You may, of course, have replaced Rusty with your own name.) Then we print out “Hello “. Notice that we’ve switched here from System.out.println method to the similar System.out.print method. System.out.print is just like System.out.println except that it doesn’t break the line after it’s finished. Therefore when we reach the next line of code, the cursor is still located on the same line as the word “Hello” and we’re ready to print out the name.

Comments are closed.