Comments
Comments in a program are called inline
documentation
They should be included to explain the purpose of
the program and describe processing steps
They do not affect how a program works
Java comments can take following forms:
// this comment runs to the end of the line
/*
*/
White Space
Spaces, blank lines, and tabs are called white
space
White space is used to separate words and
symbols in a program
Extra white space is ignored
Programs should be formatted to enhance
readability, using consistent indentation
Identifiers
Identifiers are the words a programmer uses in a
program
For naming class, methods, variables, objects,
labels, package and interfaces.
An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign
Identifiers cannot begin with a digit
Java is case sensitive - Total, total, and
TOTAL are different identifiers
keyWords
The Java reserved words: all are in lower case
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
Literals
Sequence of characters (digits, letters, other
chars)
Types of literals:
Integer
Floating point
Character
String
Boolean
NULL
Operators
Symbol that takes one or more arguments and
operates on them to produce a result
Separators
Symbol used to indicate where codes are divided
and arranged
Parantheses ()
Braces {}
Brackets []
Semicolon ;
Comma ,
Period .
Errors
A program can have three types of errors
The compiler will find syntax errors and other
basic problems (compile-time errors)
If compile-time errors exist, an executable version of the
program is not created
Constants
A constant is an identifier that is similar to a
variable except that it holds the same value during
its entire existence
As the name implies, it is constant, not variable
The compiler will issue an error if you try to
change the value of a constant
Types of constants
Backslash character constants
Escape Sequences
Some Java escape sequences:
Escape Sequence Meaning
\b
\t
\n
\"
\'
\\
backspace
tab
newline
double quote
single quote
backslash
Symbolic Constants
Assignment of a symbolic name to constant
E.g.: PASS_MARK
Cannot be declared inside a method, only as class
data member
In Java, we use the final modifier to declare a
constant
final int MIN_HEIGHT = 69;
Variables
Identifier that denotes a storage location to store a
data value
Every variable in a Java program must be declared
before it is used
A variable declaration tells the compiler what kind of data
(type) will be stored in the variable
The type of the variable is followed by one or more
variable names separated by commas, and terminated
with a semicolon
Variables are generally declared just before they are used
or at the start of a block indicated by an opening brace {
Start the names of variables, classes, methods, and
objects with a lowercase letter, indicate word
boundaries with an uppercase letter, and restrict the
remaining characters to digits and lowercase letters
topSpeed bankRate1 timeOfArrival
Variables
Basic types in Java are called primitive types
Numeric
Integer
Byte
Short
Int
Long
Floating point
Float
Double
Non numeric
Character
Boolean
Non-primitive / derived
Class
Interface
Arrays
Variables [4M]
Scope of Variables
Instance variable
Instance variables belong to an instance of a class.
Another way of saying that is instance variables
belong to an object, since an object is an instance of a
class. Every object has its own copy of the instance
variables.
Class variable
Class variables, however, only have one copy of the
variable(s) shared with all instances of the class.
Local variable
Declared and used inside methods
e.g.:
class Animal(){
private int _numberOfLegs = 4; //instance
variable private static int numberOfAnimals =
0; //class variable
}
Types of operators
Arithmetic
Relational
Logical
Assignment
=
Shorthand assignment
variable operator=
exp
Increment n Decrement
Conditional
Ternary operator
To decide which value should be assigned to the
variable
Bitwise
A=60
b=13
0011 1100
0000 1101
A&b 00001100 = 12
A<<2
1111 0000 = 240
Special
Instanceof
Mathematical functions[4M]
Programs [4M]
Reverse of a number/palindrome
Fibonacci series
Factorial of a number
Decimal to Binary
int w=1,d,s=0;
While(n!=0){
d=n%2;
s = d * w + s;
w=w*10;
n=n/2;
}