1.1-Variables and Printing
1.1-Variables and Printing
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