2.5. Concepts Summary
This chapter introduced many basic concepts: printing the result of a statement (expression), math operators, relational operators, types, casting, and variables.
Java programs are made of statements. Java statements can end in semicolons ';' just like sentences can end in periods "." in English. When you type statements in the definitions pane (when you define methods) they must have some sort of punctuation to show the end of the statement. One way to do this is to use a semicolon ';'.
If you leave off the semicolon in the interactions pane, it will print the result of the statement. If you do end a statement with a semicolon in the interactions pane, and you want to print the result, use System.out.println(expression); to print the result of the expression.
> int numPeople = 3; > double bill = 52.49; > double amountPerPerson = bill / numPeople; > System.out.println("Each person should pay: " + amountPerPerson); Each person should pay: 17.496666666666666
2.5.2. Relational Operators
A type is a description of the "kind of" thing something is. It affects how much space is reserved for a variable and how the bits in that space are interpreted. In this chapter, we talked about several kinds of types (encodings) of data.
Java compilers recognize integer (-3) and floating point values (32.43). The result of a mathematical expression depends on the types involved in the expression. Expressions that involve integer values will have integer results. Expressions that have floating point (decimal) values will have floating point results.
This can lead to unexpected results.
> 1 / 2 0
There are two ways to fix this problem. One is to make one of the numbers a floating point number by adding '.0' (it doesn't matter which one) and the other is to use casting to change the type of one of the numbers to a floating point number (the primitive type float or double).
> 1.0 / 2 0.5 > (double) 1 / 2 0.5
Variables are used to store and access values. You create variables by declaring them: type name; or type name = expression;. Declaring a variable reserves space for the variable and allows the computer to map the variable name to the address of that reserved space.
We introduced two types of variables: primitive and object. Primitive variables are any of the types: int, byte, short, long, float, double, char, or boolean. Object variables refer to an object of a class. Use the class name as the type when declaring object variables ClassName name; or ClassName name = expression;.
Primitive variables store a value in the reserved space for that variable. You can change the value using variableName = value;. You can access the value using variableName.
Object variables store a reference to an object in the reserved space for that variable. Object variables do not just store the address of the object. They store a reference to an object which allows the address of the object to be determined.
If the object variable doesn't refer to any object yet it has the value null. You can change what object a variable references using variableName = object-Reference;. You can access the referenced object using variableName.