Basic Syntax and Concepts
- Java is case-sensitive
Variables
Three types of variables
Fields - member variables in a class
Local variables - variables in a method or block of code
Parameters - variables in method declarations
NOTE: No global variables in Java
Data type
- Primitive data type
boolean,byte,short,int,long,float,double,char- Overflow and underflow
BigDecimalclass overcomes the precision issues of the floating number typescharin Java is 2 bytes to allow you to store Unicode characters- e.g.
'\u0040' == 'A'
- e.g.
- Class data type
- Casting
- Primitive data type
Declaration and Initialisation
Literals
- Good habit: add appropriate data type suffix
Operators
- Operator and operand
- Mathematical operators
- Only overriding operator in Java:
+,+=- Other data type variables will be automatically casted to a String
- Only overriding operator in Java:
- Assignment operator
- Abbreviating operators
++,--- Relational operators
- Logical operators
- Bitwise operators
- Ternary operator
- Casting operators
,- Operator precedence
Expressions and Statements
Expression is formed by combining variables, literals, method return values and operators
Statement is an executable line or code block
Declaration statement
Expression statement
- Assignment expressions
++,--- Method calls
- Object creation
Control flow statement
Code organisation: whitespace and indentation
Flow control
if-elseswitchStringcan be used- Enhanced switch
whiledo whileforforeach
Method
Function in Java
Java method is pass-by-value
A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters
Method Overloading
- Methods have the same name, but take a unique list of argument types
- Overloaded methods CANNOT differ only by return type
- Type promotion
- Can cause compile time error if there are more than one methods matched
Variable Arguments
Variable numbers of inputs
Must be the last parameter
void show(int... x) { for (int e : x) System.out.println(e); } // All valid show(); show(10); show(10, 20); show(10, 20, 30); int[] x = {1, 2, 3}; show(x);
Command-line Arguments
String[] argsinmain()
