Introduction-to-Java
Introduction-to-Java
History
class: Another Java keyword that indicates that the element being
defined here is a class. All Java programs are made up of one or more
classes. A class definition contains code that defines the behavior of
the objects created and used by the program.
HelloApp: An identifier that provides the name for the class being
Defined.
STRUCTURE OF JAVA PROGRAM (3/5)
System.out.println(“Hello, World!”);
There are certain rules for defining a valid java identifier. These rules must be followed,
otherwise we get compile-time error. These rules are also valid for other languages like C and
C++.
The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar
sign) and ‘_‘ (underscore). Identifiers should not start with digits([0-9]). For example “1Student” is a
not a valid java identifier.
Java identifiers are case-sensitive.
There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15
letters only.
Reserved Words can’t be used as an identifier. For example “int while = 20;” is an invalid statement
as while is a reserved word. There are 53 reserved words in Java.
Spaces are not permitted inside identifiers
By convention, variable names should start with a lowercase letter.
All variables must be initialized before you access them.
JAVA KEY WORDS
Example of Valid Identifiers
MyVar
MYVAR
myvar
x
i
X1
_myvar
$myvar
sum_of_item_cost
itv5L
Example of Invalid Identifiers
My Var
5L ITV
this
A+2
goto
Var-2
Sum_&_product
for
Variables
SYNTAX:
<DATATYPE> <VARIABLENAME>;
EXAMPLE
int counter;
int counter=0;