Variables and Printing
Storing and Retrieving
• The first requirement for any computer
language is that it can:
Store values and retrieve them
• The java language
– creates variables of specified types;
– assigns values to these variables (Store); and
– Uses the assigned value when a variable is
referenced (Retrieve)
Display one message
• Yesterday we saw that we can use the method
System.out.println()
to display a message on the screen.
• To do that, we
– Enclose the message in double quotes
– Place the message inside the parentheses
System.out.println(“Hello, World!”);
– Put this statement inside the main( ) method
Methods
• What are methods?
– A method in java is a collection of instructions that
perform some action
– Every method has a unique name, and this name
ends with parentheses ( )
• If a method needs information to perform its
action
– That information is provided as argument(s)
– The arguments are placed inside the ( )
Using Methods
• One method can invoke or call another one
– The called method is referred to by name in a
statement
– Any arguments are provided in the ( )
• In the Hello World program
– The main( ) method calls the method
System.out.println( )
– The argument is the String “Hello, World!”
Display several messages
• To print several messages on separate lines, use
several System.out.println( ) statements in a
row:
System.out.println(“Hello, World!”);
System.out.println(“It’s a sunny morning”);
• The action of the method System.out.println( )
is to
– Display its argument on the screen
– Go to the next line on the screen
Several messages on one line
• System.out.print( ) – its action
– Prints its argument on the screen, but
the cursor stays at the end of the message (because the ln is
missing!)
• If you use one or more System.out.print( ) statements in a
row, you should make the last one a System.out.println( )
• These statements
System.out.print(“Hello, World”);
System.out.println(“Nice day”);
print on the screen the message
Hello, WorldNice day
Printing Numbers
• To print a number
– Use the number (no double quotes) as an
argument
– System.out.println(5) displays 5 on the screen
• If you put double quotes around a number it is
treated as a String
Examples of Printing
• System.out.print(“Number of rolls: “);
– This prints its argument, “Number of rolls: “
• System.out.println(5);
– This prints its argument, the number 5
• Combine these two inside main( )
System.out.print(“Number of rolls: “);
System.out.println(5);
And the output is
Number of rolls: 5
Naming Conventions (1)
• Names of classes begin with a Capital Letter, e.g.
Main, Balloon, Person, Creep, Sloth, etc.
• Names of constants are written all in capitals, words
separated by underscores, _,
e.g. PI, DEFAULT_SIZE, A_CUTOFF, etc.
• All other names
– Begin with a lower-case letter
– Use camelCase to separate the words in the name
e.g. isActive, firstName, numRemaining, countWords
Naming Conventions (2)
• Generally it’s a good idea to choose names
that are descriptive and easy to understand
• Names of methods generally sound like verbs
– getFirstName(), calculateGPA(), pop(), etc.
• Names of variables and fields in a class
(“properties”) generally sound like nouns or
adjectives
– lastName, lastPosition, endOfString, etc.
Variables
• A variable is the name of a memory location
that holds* a value.
• Use a declaration statement to create a variable
by naming it and specifying the type of data it
holds:
<DataType> variableName;
• Use an assignment statement to specify the
value the variable holds:
variableName = value;
Picture of a Variable
• int a; // declaration
• a = 397; // assignment
Variable memory
a 397
its value
Primitive Data Types
• Java has 8 “primitive” data types. In this
course we will use these 4 primitive types:
– int holds integers between -2,147,483,648
(-231) and 2,147,483,647 (231-1)
– double holds numbers with decimals or too large
to be held as ints
– boolean holds the values true and false
– char holds characters on the keyboard and
some other characters as well
Object Data Types
• String is another very important data type
– String is not a primitive data type but in some ways
behaves like a primitive data type
• Object data types are associated to classes
• Once we define a class, like Balloon, that class is
the data type for objects belonging to it
– Class Balloon { /* . . . */ }
– Balloon bob;
– bob is an object with data type Balloon
Declaring Variables
• A String variable
– String message; // declaration
• An int variable
– int count; // declaration
• You can declare two variables of the same
data type together
– int date, year; // declaration
Assigning Values to Variables
• java uses the symbol = to assign a value (on the right side) to a
variable (on the left side)
• Warning: in java, the = sign is the assignment operator, not a test
that two items have the same value!
• Some examples of creating variables and assigning values to them
– int age; // declaration statement
age = 7; // assignment statement
– double radius = 2.5; // both statements combined
double area;
area = Math.PI * radius * radius;
– String message;
message = “Goodbye, Cruel World!“;
Initialization Statements
• Instead of defining a variable and then
assigning a value to it, you can combine these
into a single initialization statement
– String month = “August”;
• This replaces the two statements
String month;
month = “August”;
– int date = 27;
– int year = 2014;
Printing Variables
• The statement
System.out.println(variableName);
prints the value of variableName
• What does this code do?
String name = “Silly Dufus”;
System.out.println(name);
Concatenating Strings
• Given two variables
– String s1 = “Hello,”;
– String s2 = “World!”;
Use the + sign to concatenate them together:
– s1 + s2 has value “Hello,World!”
• Both of these statements:
System.out.println(s1 + s2);
System.out.println(“Hello,” + “World!”);
display “Hello,World!” on the screen
Expressions
• An expression is a combination of two or more
values and/or variables with operators
– Operators for numbers include +, -, * and /
– The String class has only the operator +
• + concatenates the Strings it joins
• “Pot” + “belly” gives the name of a sandwich shop
• The value of an expression is the result of the
operations, after all variables are replaced by
their values
Printing Expressions
• The statement
System.out.println(expression);
prints the value of the expression
• What does this code do?
String firstName = “Silly”, lastName = “Dufus”;
System.out.println(firstName + “ “ + lastName);
• Answer:
– it concatenates the values “Silly”, a single Space character,
and “Dufus”
– Then it prints the result, which is
Silly Dufus
Hint – Using a Constant
• If you need to use the same String
e.g " " for a single Space or
", " for comma Space
many times in a program, you can instead
create a String constant with that value
– static final String SPACE = " ";
– static final String COMMA = ", ";
• Then, "Hello" + COMMA + "World!“ evaluates
to just what you expect
static final
• static final is the keyword expression
that creates a constant*
• A static final constant must be
– created by an initialization statement
– defined at class level: in the body of the class, but
not inside the body of any method
• The name of any constant should be in
ALL_CAPITAL_LETTERS_SEPARATED_BY
underscores