Professional Documents
Culture Documents
• Input – Parameters
• Example:
void setDirection(double direction)
• Output – Return type
• Example
void setDirection(double direction)
boolean isTailDown()
Variables
◦ Data is stored in variables
◦ A variable is a memory location with a name
attached to it
◦ Can think of a ‘box’ inside the computer with the
name written on the outside of the box
◦ Variables must be declared before they can be
used
◦ What’s ‘in the box’ is the value of the variable
◦ Java needs to know what type of data you want
to store
Data types
◦ Java must know the type of data with which it is
working.
◦ All data can be categorized by its type.
◦ Types used this semester:
◦ int used for integers
◦ double used for decimal numbers
◦ String used for words or sentences
◦ boolean used for true or false
Primitive vs Object
◦ int, double, and boolean are known as primitive
data types
◦ No methods
◦ Don’t use new operator to create a new
◦ Strings are objects
◦ Lots of methods to manipulated Strings
◦ Don’t need to used the new operator (The only
exception to this requirement)
Declaration Statements
◦ Before variables can be used they must be
declared
type name;
Example declarations
◦ int x;
◦ int y;
◦ int count;
◦ double temperature;
◦ double cost;
◦ boolean headlightsOn;
◦ String message;
Fields
◦ When the variables are attributes in of a class
they are fields
◦ AKA
◦ Instance variables
◦ Data members
/**
* Computes the average of two ints as a double
* @precondition none
* @postcondition none
* @param first the first int
* @param second the second int
* @return average the average
*/
public double average(int first, int second) {
// Code codes here
}
Print statements
◦ Prints a String to the output console
◦ System.out.println(string);
◦ Prints string to the screen followed by a carriage
return
◦ System.out.print(string);
◦ Prints string to the screen and cursor remains at
end position
Shorthand operators
◦ Incrementing a variable
◦ Common to increment by one
x = x + 1; // Increments by one
◦ Can also be written as
x++;
◦ Assignment shortcuts
◦ Common to update a variable
x = x + 5; // Adds 5 to x
◦ Can also be written as
x+=5;
x += 5; // White space doesn’t matter
Converting between types
◦ Need to assign like types to like types
int x, y;
double z;
boolean valid;
String result;
int x, y;
double z;
x = 3;
y = 5;
z = x; // Legal
z = y + 3.2; // Legal
x = 3.2; // Illegal
String operators
◦ Java will do some conversions to a String for you
int x = 3;
double z = 5.2;
String result;
result = x; // Illegal
result = “Result: ” + x; // Legal as one of
operands is a string so will automatically
convert the x to a string for you, result
equals “Result: 3” after assignment
Casting
◦ Forcing one type to another
◦ Typically used to convert a double to an int
◦ Beware of doing this as will be losing precision.
int x = 3;
double z = 5.2; Type cast
x = z; // Illegal
x = (int)z; // Forces the double to be an int, but
will lose the fractional portion, x equals 5 after
the assignment
Next class
◦ Exam 1