![]() |
||||||
![]() |
||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
![]() |
![]() |
Basics of the Java Language files: AddEmUp.java (help)
1. /** Variables Variables can be thought of as cubbyholes to store pieces of data for later use. Before using a variable you must declare it. In the declaration you give the variable a name and you specify the type of data the variable will hold. The declaration looks like:
As discussed previously, instance variables hold the state of a given object. Each object has its own memory allocated to hold these values. Data Types In Java every variable must have a data type. If you think of a variable as a cubbyhole, then the data type is the cubbyhole's shape. You cannot put "square" data into a cubbyhole designed for "round" data. Likewise, if you have declared a variable to be of type int as we did above, then you cannot put float (floating point or decimal data) into it. |
||||
![]() |
![]() |
![]() |
![]() |
![]() |
Literals Literals are how specific values of Java primitive data types are represented in the source code. For example, on line 10 of AddEmUp we initialize the variable total to a value of "0". The "0" in the code is an integer literal. Other examples of literals are: Operators Operators are things like the plus sign "+". They perform functions like addition or subtraction on one or more values which are called the operands. Operators can be unary, binary, or ternary. A unary operator works on one operand. For example, ++ is a unary operator that increments the value of its operand by one. Binary operators require two operands. For example, + is a binary operator that adds its left and right operands together. Finally, a ternary operator is one that has three operands. The only ternary operator in Java is ?: which is a kind of if/else statement. |
||
![]() |
![]() |
![]() |
|
|||||||||||||||||||||||||||||||
![]() |
![]() |
![]() |
|
||
![]() |
![]() |
![]() |
|
||||||||||||||||||||||
![]() |
![]() |
![]() |
|
||
![]() |
![]() |
![]() |
|
||||||||||||||||
![]() |
![]() |
![]() |
|
||
![]() |
![]() |
![]() |
|
||||||||||||||||||||||
![]() |
![]() |
![]() |
Expressions: If you consider variables, operators and method calls to be "words" of the Java language, an expression is equivalent to a "phrase." An expression is a series of variables, operators and method calls that evaluate to a single value. The expression performs the computation specified as well as returns the value that is the result of the computation. The code 2 + 3 is an example of a simple expression. Statements: Statements are somewhat similar to "sentences" in Java. There are three types of statements to consider:
An expression statement can be constructed by combining one or more expressions and terminating them with a semicolon ";". In the AddEmUp program above we have the following expression statements: Blocks of Code: The next level up in organization is a block of code. This is roughly equivalent to a "paragraph" in the language. A block is a grouping of zero or more statements between balanced curly braces. Local variables can be defined within a code block, restricting their scope to within that block.
|
||
![]() |
![]() |
![]() |