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
Scanner keyboard
= new Scanner(System.in); // new creates an object
int
intValue
= keyboard.nextInt();
// object.method()
short shortValue = keyboard.nextShort(); // similarly for byte, long
float floatValue = keyboard.nextFloat();
double doubleValue = keyboard.nextDouble();
String tokenValue = keyboard.next();
// default delimiters: whitespace
String lineValue = keyboard.nextLine();
boolean booleanValue = keyboard.nextBoolean();
3 Screen Output
System.out.println(...); System.out.print(...);
4 Arithmetic Operators
+ * / % ++var var++ --var var--
addition subtraction multiplication division modulo (remainder) pre-increment post-increment pre-decrement post-decrement
5 Assignment Operators
= assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= modulo assignment
1
6 Relational (Comparison) Operators
< less than greater than >= greater than or equal == equal != not equal
7 Logical Operators
&& and [short-circuit (lazy) evaluation] || or [short-circuit (lazy) evaluation] ! not exclusive or & and [complete (eagar) evaluation] | or [complete (eagar) evaluation]
8 String Class
String stringValue
String concatString
int
length
char charAtIndex
String substring
String substring
int
startIndex
String lowerCaseStr
boolean sameContent
int
ordering
= "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);
= pareTo(stringValue2);
9 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)
(3.14159...) e (2.71828...) |x|
x
ln x (loge x) log10 x xy
nearest integer x
10 Branching (Conditional) Statements
10.1 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 Loop Statements
11.1 while loop
while () {
; ... }
3
11.2 do-while loop
do {
; ... } while ();
// note the semicolon
11.3 for loop
for (; ; ) {
; }
// "ICU"
12 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 Arrays
13.1 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
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- a type system for format strings university of washington
- summary of basic java syntax
- java string format 0
- string java format example programs pdf
- java printf method quick reference colorado state university
- java string format 8s
- java format string with parameters
- java syntax examples
- format string syntax java example
- method example
Related searches
- summary of history of philosophy
- basic java programming examples
- basic java programming examples pdf
- basic java code examples
- basic java code
- basic java programs for beginners
- summary of books of the bible pdf
- example of summary of an article
- summary of each chapter of huckleberry finn
- basic java programming tutorial
- summary of life of jesus
- basic java tutorial pdf