Upload
gregory-robertson
View
225
Download
0
Embed Size (px)
Citation preview
ControllingProgram Flow
Data Types and Variable
Declarations Controlling Program Flow
Everything is an Object You manipulate objects with
references : television\remote control
Reference can stand on its own : String s A safer practice, then, is always to
initialize a reference when you create it:
String s = "asdf";
Data Types
Object type ready-made types your own types : class
Special case: primitive types
For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable using new, an “automatic” variable is created that is not a reference.
The variable holds the value, and it’s placed on the stack so it’s much more efficient.
More
All numeric types are signed, so don’t go looking for unsigned types.
The size of the boolean type is not explicitly defined; it is only specified to be able to take the literal values true or false.
“ wrapper” classes if you want to make a nonprimitive
object on the heap to represent that primitive type, you use the associated wrapper :char c = ‘x’; (primitive)Character C =
new Character('x'); (nonprimiteve)
Using Java operators
Precedence Operator precedence defines how
an expression evaluates when several operators are present :
The easiest one to remember is that multiplication and division happen before addition and subtraction.
Assignment “=” Assignment of primitives is quite
straightforward.
when you assign primitives you copy the contents from one place to another.
A=B
Assignment“=” assign objects
When you assign “from one object to another” you’re actually copying a reference from oneplace to another.
Assignment.java
This phenomenon is often called aliasing. If you don’t want aliasing to occur in this case, You could forgot the assignment and say:
n1.i = n2.i;It goes against good object-oriented
design principles.
Aliasing during method calls Aliasing will also occur when you
pass an object into a method:
PassObject.java
In many programming languages, the method f( ) would appear to be making a copy of its argument Letter y inside the scope of the method.
But once again a reference is being passed so the line
y.c = 'z'; is actually changing the object
outside of f( ).
Mathematical operators The basic mathematical operators are
the same as the ones available in most programming languages: addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the
result.
Relational operators
< 、 > 、 <= 、 >= 、 == 、 != Equivalence (==) and
nonequivalence works with all built-in data types, but the other comparisons won’t work with type boolean.
Testing object equivalence
The relational operators == and != also work with all objects, but their meaning often confuses the first-time Java programmer :
public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); }}
the output is actually false and then true. Naturally, this surprises people at first.
What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine with == and !=.
More
public class EqualsMethod { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); }}
The result will be true, as you would expect.
But it’s not as simple as that. If you create your own class, like this:
class Value { int i;}public class EqualsMethod2 { public static void main(String[] args) { Value v1 = new Value(); Value v2 = new Value(); v1.i = v2.i = 100; System.out.println(v1.equals(v2)); }}
the result is false ! This is because the default
behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won’t get the desired behavior.
Logical operators Bitwise operators Shift operators Ternary if-else operator The comma operator
String operator + The + operator can be used to
concatenate strings If an expression begins with a String, then all operands that follow must be Strings : int x = 0, y = 1, z = 2; String sString = "x, y, z "; System.out.println(sString + x + y + z);
Java uses all of C’s execution control statements.
In Java, the keywords include if-else, while, do-while, for, and a selection statement called switch.
if-else The if-else statement is probably the most
basic way to control program flow. The else is optional, so you can use if in two forms:
if(Boolean-expression) statement or if(Boolean-expression) statement else statement
Iteration while(Boolean-expression) Statement do statement while(Boolean-expression); for(initialization; Boolean-expression;
step) statement
public class WhileTest { public static void main(String[] args) { double r = 0; while(r < 0.99d) { r = Math.random(); System.out.println(r); } }}
This uses the static method random( ) in the Math library, which generates a double value between 0 and 1. (It includes 0, but not 1.)
The conditional expression for the while says “keep doing this loop until the number is 0.99 or greater.” Each time you run this program you’ll get a different-sized list of numbers.