Sie sind auf Seite 1von 2

Java Quick Reference Guide

Arithmetic Operators Forms of the if Statement


+ Addition
- Subtraction Simple if Example
* Multiplication
/ Division (integer or floating-point): 2/3 = 0, 2.0/3.0 =.666667 if (expression) if (x < y)
% Modulus (integer remainder) statement; x++;
2 % 3 = 2, 3 % 2 = 1, 6 % 3 = 0 Remember to use the methods
equals( ) or compareTo( ) when if/else Example
Relational Operators
comparing Strings! if (expression) if (x < y)
< Less than
Don't use relational operators statement; x++;
<= Less than or equal to
when comparing Strings. else else
> Greater than
statement; x--;
>= Greater than or equal to
== Equal to Remember to distinguish between
!= Not equal to integers and real numbers (called if/else if (nested if) Example
floating-point in Java). if (expression) if (x < y)
Logical Operators These are stored differently in statement; x++;
! NOT memory and have different ranges else else
&& AND of values that may be stored. if (expression) if (x < z)
|| OR integers: 2, 3, -5, 0, 8 statement; x--;
Assignment Operators floating-point: 2.0, 0.5, -3., .6 else else
= Assignment statement; y++;
+= Combined addition/assignment
-= Combined subtraction/assignment To conditionally execute more than one statement, you must
*= Combined multiplication/assignment create a compound statement (block) by enclosing the statements
/= Combined division/assignment in braces ( this is true for loops as well ):
%= Combined modulus/assignment
Form Example
Increment/Decrement ( used in prefix and postfix modes )
if (expression) if (x < y)
++ Increment prefix - inc(dec) variable, use in expression
{ {
-- Decrement postfix - use in expression, inc(dec) variable
statement; x++;
statement; System.out.println( x );
Object Instantiation: new: new int[ 10 ], new String( "Cerritos") } }
Java Types [ value / reference ]
A value type stores a value ( aka primitive type ) Conditional Operator ? :
int x = 3;
A reference type stores the address of an object ( aka class type ) Form:
String s1 = "CIS 103"; //Strings use double quotes expression ? true-expression : false-expression ;
Example:
Primitive Data Types ( value types )
x = a < b ? a : b;
boolean (flag variable) true or false
char 'A', 'n', '!' [single quotes] The statement above works like:
Integral ( byte, short, int, long ) 2, 3, 5000, 0 if (a < b)
Floating-point ( float, double ) 0.0, 123.456, .93 x = a;
else
Class String is an example of a reference type x = b;

The switch/case Construct ( break and default are optional ) Java Numeric Conversions:
Form: Example: Widening conversions are done implicitly.
switch (int-expression) switch (choice) double x; int y = 100;
{ { x = y; // value of y implicitly converted to a double.
case int-constant : case 0 : Narrowing conversions must be done explicitly using a cast.
statement(s); System.out.println( double x = 100; int y;
“You selected 0.” ); y = (int) x; // value of x explicitly cast to an int
[ break; ] break;
case int-constant : case 1: In mixed expressions, numeric conversion happens implicitly.
statement(s); System.out.println( double is the “highest” data type, byte is the “lowest”.
“You selected 1.” ); Conversion from a String to a number using Wrapper Classes
[ break; ] break;
double d = Double.parseDouble(dString);
[ default : default : float f = Float.parseFloat(fString);
statement; ] System.out.println( int j = Integer.parseInt(jString);
“You did not select 0 or 1.” );
} }
Use the break keyword to exit the structure ( avoid “falling through” Java Escape Sequences ( two symbols but only one character )
other cases). Use the default keyword to provide a default case if none
of the case expressions match ( similar to trailing “else” in an if-else-if … \n newline character '\n'
statement ). \t tab character '\t'
Java Quick Reference Guide
The for Loop ( pre-test loop )
Form: Example:
for (initialization; test; update) for (count = 0; count < 10; count++)
statement; System.out.println ( count );

for (initialization; test; update) for (int count = 1; count <= 10; count++)
{ {
statement; System.out.print( "The value of count is " );
statement; System.out.println( count );
} }

For definite while and do..while loops, remember there should be an initialization statement preceeding the loop body and an update
statement in the loop body (usually the last statement). Examples below don’t show these explicitly due to space…

The do-while Loop ( post-test loop ) The while Loop ( pre-test loop )
Form: Example: Form: Example:
do do while (expression) while (x < 100)
statement; sum += x; statement; y = y + x++;
while (test-exp); while (sum < 100)
while (expression) while (x < 100)
do do { {
{ { statement; y = y + x;
statement; y = x + 5; statement; x++;
statement; x++; } }
} while (expression); } while (x < 100);

Selection and Loop Java Arrays: Create an array ( 2 ways )


Structure Information
1. <type> <array-name>[ ] = new <type>[ size ];
Selection: 2. <type> <array-name>[ ] = { <initializer-list> };
 Unary or single selection
 Binary or dual selection //create an array of 20 elements.
 Case structure when int myArray[ ] = new int[20];
branching on a variable
 Simple selection //create an array of 3 elements set to the values in the initializer list.
int myArray[ ] = { 1, 2, 3 };
 One condition String stooges[ ] = { "Moe", "Larry", "Curly", "Shemp" };
 Compound selection
 Multiple conditions //assign value of first element in myArray to the integer variable x.
joined with AND / OR int x = myArray[0];
operators
//assign value of the last element in myArray to the integer variable y.
Looping: int y = myArray[ myArray.length-1 ];
 Java Pre-test loops
 while All arrays have a public field named length which holds the number of elements in the array. If x is a reference
 for to a two-dimensional array, x.length is the number of elements in the array in the first dimension. x[n].length
 Java Post-test loop is the length of an array in the second dimension.
 do…while
Java Methods: <type> <method-name> ( * <type> parameter1, * <type parameter2, … + + )
Loop Control: Simple procedural programs in Java will contain the static keyword and may also contain the keyword public.
 Counter-controlled Methods that will not return a value will have the return type void in the method header.
Math.PI  3.141592635…
aka definite loops have  public static void printHeadings( ) //no parameters, return type is void
3 expressions: { <method body> }
 Initialization
 static void printDetailLine( String name, int number, double gpa ) //3 parameters, return type is void
 Test
{ <method body> }
 Update
 Sentinel-controlled  static int getCount( ) //no parameters, return type is int
aka indefinite loops have 2 { <method body> }
expressions:  public static double max( double x, double y) //2 parameters, return type is double
 Test { <method body> }
 Update (Alter)
When a method is called, the data is passed to the parameters (if any) using arguments
 Java Loop Early Exit: //Arguments: "Jack Wilson", 100, 3.50 passed to Parameters: name, number, gpa
 break statement printDetailLine( "Jack Wilson", 100, 3.50);

Das könnte Ihnen auch gefallen