Summary of Basic Java Syntax

Summary of Basic Java Syntax Philip Chan November 7, 2006 1 Primitive Types byte short int long float double char boolean 2 Keyboard Input Scann...
Author: Erika Barton
17 downloads 0 Views 44KB Size
Summary of Basic Java Syntax Philip Chan November 7, 2006

1

Primitive Types

byte short int long float double char boolean

2

Keyboard Input

Scanner int short float double String String boolean

3

keyboard intValue shortValue floatValue doubleValue tokenValue lineValue booleanValue

= = = = = = = =

new Scanner(System.in); keyboard.nextInt(); keyboard.nextShort(); keyboard.nextFloat(); keyboard.nextDouble(); keyboard.next(); keyboard.nextLine(); keyboard.nextBoolean();

Screen Output

System.out.println(...); System.out.print(...);

4

Arithmetic Operators

+ * / % ++var var++ --var var--

5 = += -= *= /= %=

addition subtraction multiplication division modulo (remainder) pre-increment post-increment pre-decrement post-decrement

Assignment Operators assignment addition assignment subtraction assignment multiplication assignment division assignment modulo assignment 1

// new creates an object // object.method() // similarly for byte, long

// default delimiters: whitespace

6 < >= == !=

7 && || ! ∧ & |

8

Relational (Comparison) Operators less than less than or equal greater than greater than or equal equal not equal

Logical Operators and [short-circuit (lazy) evaluation] or [short-circuit (lazy) evaluation] not exclusive or and [complete (eagar) evaluation] or [complete (eagar) evaluation]

String Class

String String int char String String int String boolean int

9

stringValue concatString length charAtIndex substring substring startIndex lowerCaseStr sameContent ordering

10.1

"Hello"; stringValue1 + stringValue2; stringValue.length(); stringValue.charAt(index); stringValue.substring(startIndex, endIndex); // excluding endIndex stringValue.substring(startIndex); stringValue.indexOf(stringToSearchFor); stringValue.toLowerCase(); // similarly toUpperCase() stringValue1.equals(stringValue2); stringValue1.compareTo(stringValue2);

Math Class

Math.PI Math.E Math.abs(x) Math.ceil(x) Math.log(x) Math.log10(x) Math.pow(x, y) Math.round(x) Math.sqrt(x)

10

= = = = = = = = = =

π (3.14159...) e (2.71828...) |x| dxe ln x (loge x) log10 x xy nearest integer √ x

Branching (Conditional) Statements Simple if statement

if () ; else // else part is optional ;

2

10.2

Compound if statement

if () { ; ... } else // else part is optional { ; ... }

10.3

if-else-if statement

if () { ; ... } else if () { ; ... } else // else part is optional { ; ... }

10.4

switch statement

switch () { case : ; break; ... case : ; ... break; default: // default case is optional ; ... }

11 11.1

Loop Statements while loop

while () { ; ... }

3

11.2

do-while loop

do { ; ... } while ();

11.3

// note the semicolon

for loop

for (; ; ) { ; }

12

// "ICU"

Classes

12.1

A Basic Class

public class { // instance variables (attributes for each object) private ; // public methods (for each object) public () { ; ; return ;

// not needed if returnType is void

} }

12.2

A Class with Various Options

public class { // global constants public static final ; // semi-global constants for the class private static final ; // constant attributes for each object private final ; // instance variables (attributes for each object) private ; // constructor public () { ; ; }

4

// public methods (for each object) public () { ; ; return ;

// not needed if returnType is void

}

// private methods (helper methods for each object) private () { ; ; return ;

// not needed if returnType is void

}

// public static methods (for the class) public static () { ; ; return ;

// not needed if returnType is void

}

// private static methods (helper methods for the class) private static () { ; ; return ;

// not needed if returnType is void

} }

13 13.1

Arrays One-dimensional arrays for primitive types

[] = new [];

13.2

One-dimensional arrays for class types

// allocate space for addresses of objects [] = new []; // create an object for each array element, usually in a loop [] = new ();

13.3

Two-dimensional arrays for primitive types

[][] = new [][];

5

13.4

Two-dimensional arrays for class types

// allocate space for addresses of objects [][] = new [][]; // create an object for each array element, usually in a nested loop [][] = new ();

13.5

Ragged two-dimensional arrays

[][] = new [][]; // create an array for each row [] = new [columnLength];

6

Suggest Documents