My other sites

Latest News

New Summer Look :)










Java's Basic Syntax I

  - Identifiers, Variable Declaration and General Syntax

- Language Keywords

- Comments

- Identifiers

- Simple Types - Datatypes

- Literals - Data Type Values

- Complex Types

- Type Issues

- Operator Precedence
 


Java Identifiers, Variable Declaration and General Syntax


- Case sensitive

- Each statement finishes with ;

- To begin and end a block you use { }

- Space, tab, and enter characters can be used to make the code more readable

- Variable declaration, constants, comments, terminology similar to C++


- Identifiers can contain any combination of
upper-case
, lowercase, numbers and '_', '$'

- An identifier must not begin with a number


Java Lanaguage Keywords

abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while


Java Comments

/**
 * This is a special kind of comment
 * that is used to hold javadoc comments
 * More on this later...
 */
public class Comments {
/*
 * this is a comment that spans multiple 
 * lines
 */
  public static void main(String[] args) {
    // this is a one line comment
    System.out.println( "hey" ); // comment to the end of the line
  }
}

 

Identifiers

- Identifiers are simple names used within a programming language

- In Java, identifiers can be any combination of letters, numbers, '_' and '$'

- However, identifiers cannot begin with a number

- Identifiers in Java are also case-sensitive
Valid Identifiers:

abc

a1b2c3

_123

$_$_$
Invalid Identifers:

1hello

@hello@

hello&world
Recommended:

helloWorld

finished

startIndex

theLastPoint
Not Recommended:

j

p

hello_world

hello1

hello2



Simple Types - Datatypes

Type Data Storage Example
byte signed 8-bit -2^7 … 2^7 - 1
short signed 16-bit -2^15 … 2^15 - 1
int signed 32-bit -2^31 … 2^31 - 1
long signed 64-bit -2^63 … 2^63 - 1
boolean 1 bit true or false
float 32-bit (IEEE-754) 3.4e+38
double 64-bit (IEEE-754) 1.7e+308
char 16-bit (unicode) '\n', '\t', '\\''


Literals - Data Type Values

Data Type Example Values
Integers 42, 0, -22, +69, 345678
Octals 0177, 00, 0777, -023
Hexadecimals 0x0, -0x1, 0x34af, 0XA9B3
Longs 0L, 0l, 12345678L, 0xab12345L, -87654321L
Floats 123.456, -654.321, 0.0, 0.98, 1.23f, 45.678F, 2.567d, 7.374D, 6.564e+23,
Boolean true, false
Characters 'a', 'b', 'A', '3', '{', ' ', '\b', '\n', '\r', '\\', '\t', '\'', \123, \234, \012
Strings "This is a string","Hello World!!!\n", "left\tcentre\tright\t"
Null null


Complex Types

For Complex types we use classes

class Employee{ }

Employee myEmployee = new Employee();

Any Type: Type my_obj = new Type();

 
Arrays: int[] five = { 0, 1, 2, 3, 4 };

char[] separators = { ',', '.', ';', ':' };



Type Issues

Type Casting = To change a basic type into another

int myint = 70;
char ch = (char) myint;


Casts that results in no loss of information :



From Type
byte
short
char
int
long
float
To Type
short, char, int, long, float, double
int, long, float, double
int, long, float, double
long, float, double
float, double
double



Operator Precedence

Operator Operand Type(s) Assoc Operation Performed
++ arithmetic R pre-or-post increment (unary)
-- arithmetic R pre-or-post decrement (unary)
+, - arithmetic R unary plus, unary minus
~ integral R bitwise complement (unary)
! Boolean R logical complement (unary)
(type) any R cast
*, /, % arithmetic L multiplication, division, remainder
+, - arithmetic L addition, subtraction
+ string L string concatenation
<< integral L left shift
>> integral L right shift with sign extension
>>> integral L right shift with zero extension
<, <= arithmetic L less than, less than or equal
>, >= arithmetic L greater than, greater than or equal
instanceof object,type L type comparison
== primitive L equal (have identical values)
!= primitive L not equal (have different values)
== object L equal (refer to same object)
!= object L not equal (refer to different objects)
& integral L bitwise AND
& boolean L boolean AND
^ integral L bitwise XOR
^ boolean L boolean XOR
| integral L bitwise OR
| boolean L boolean OR
&& boolean L conditional AND
|| boolean L conditional OR
?: boolean, any,any R conditional (ternary) operator
= variable, any R assignment
*=, /=, %=, +=, -=, <<=,>>=, >>>=, &=, ^=, |= variable, any R assignment with operation