Escolar Documentos
Profissional Documentos
Cultura Documentos
http://www.tutorialspoint.com/java/java_quick_guide.htm
What is Java?
Java is:
Object Orient ed
Plat form independent :
Simple
Secure
Archit ect ural- neut ral
Port able
Robust
Mult i-t hreaded
Int erpret ed
High Performance
Dist ribut ed
Dynamic
About Java programs, it is very import ant t o keep in mind t he following point s.
Case Sensitivity - Java is case sensit ive which means ident ifier Hello and hello would have
different meaning in Java.
Class Names - For all class names t he first let t er should be in Upper Case.
If several words are used t o form a name of t he class each inner words first let t er should be in
Upper Case.
Example class MyFirstJavaClass
Metho d Names - All met hod names should st art wit h a Lower Case let t er.
If several words are used t o form t he name of t he met hod, t hen each inner word's first let t er
should be in Upper Case.
Example public void myMethodName()
Pro gram File Name - Name of t he program file should exact ly mat ch t he class name.
When saving t he file you should save it using t he class name (Remember java is case sensit ive)
and append '.java' t o t he end of t he name. (if t he file name and t he class name do not mat ch
your program will not compile).
Example : Assume 'MyFirst JavaProgram' is t he class name. Then t he file should be saved as
'MyFirstJavaProgram.java'
public static vo id main(String args[]) - java program processing st art s from t he main()
met hod which is a mandat ory part of every java program..
Java Modifiers:
Like ot her languages, it is possible t o modify classes, met hods, et c., by using modifiers. There are
t wo cat egories of modifiers.
Access Mo difiers : default , public , prot ect ed, privat e
No n-access Mo difiers : final, abst ract , st rict fp
We will be looking int o more det ails about modifiers in t he next sect ion.
Java Variables:
We would see following t ype of variables in Java:
Local Variables
Class Variables (St at ic Variables)
Java Arrays:
Arrays are object s t hat st ore mult iple variables of t he same t ype. However an Array it self is an
object on t he heap. We will look int o how t o declare, const ruct and init ialize in t he upcoming
chapt ers.
Java Enums:
Enums were int roduced in Java 5.0. Enums rest rict a variable t o have one of only a few predefined
values. The values in t his enumerat ed list are called enums.
Wit h t he use of enums it is possible t o reduce t he number of bugs in your code.
For example if we consider an applicat ion for a fresh juice shop it would be possible t o rest rict t he
glass size t o small, medium and Large. This would make sure t hat it would not allow anyone t o order
any size ot her t han t he small, medium or large.
Example:
class FreshJuice{
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest{
public static void main(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice. FreshJuiceSize.MEDIUM ;
System.out.println("Size :" + juice.size);
}
}
No te: enums can be declared as t heir own or inside a class. Met hods, variables, const ruct ors can be
defined inside enums as well.
Java Keywords:
The following list shows t he reserved words in Java. These reserved words may not be used as
const ant or variable or any ot her ident ifier names.
abst ract
assert
boolean
break
byt e
case
cat ch
char
class
const
cont inue
default
do
double
else
enum
ext ends
final
finally
float
for
got o
if
implement s
import
inst anceof
int
int erface
long
nat ive
new
package
privat e
prot ect ed
public
ret urn
short
st at ic
st rict fp
super
swit ch
synchronized
t his
t hrow
t hrows
t ransient
volat ile
while
t ry
void
Comment s in Java
Java support s single line and mult i-line comment s very similar t o c and c++. All charact ers available
inside any comment are ignored by Java compiler.
public class MyFirstJavaProgram{
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String []args){
// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}
A lit eral is a source code represent at ion of a fixed value. They are represent ed direct ly in t he code
wit hout any comput at ion.
Lit erals can be assigned t o any primit ive t ype variable. For example:
byte a = 68;
char a = 'A'
St ring lit erals in Java are specified like t hey are in most ot her languages by enclosing a sequence of
charact ers bet ween a pair of double quot es. Examples of st ring lit erals are:
"Hello World"
"two\nlines"
"\"This is in quotes\""
Java language support s few special escape sequences for St ring and char lit erals as well. They are:
No tatio n
Character represented
\n
Newline (0x0a)
\r
\f
Formfeed (0x0c)
\b
Backspace (0x08)
\s
Space (0x20)
\t
t ab
\"
Double quot e
\'
Single quot e
\\
backslash
\ddd
\uxxxx
Descriptio n
Example
A + B will give
30
Subt ract ion - Subt ract s right hand operand from left hand operand
A - B will give 10
Mult iplicat ion - Mult iplies values on eit her side of t he operat or
A * B will give
200
B / A will give 2
B % A will give
0
++
B++ gives 21
--
B-- gives 19
Descriptio n
Example
==
(A == B) is
not t rue.
!=
(A != B) is
t rue.
>
(A > B) is
not t rue.
<
(A < B) is
t rue.
>=
(A >= B) is
not t rue.
<=
(A <= B) is
t rue.
Descriptio n
Example
&
<<
>>
>>>
Shift right zero fill operat or. The left operands value
is moved right by t he number of bit s specified by
t he right operand and shift ed values are filled up
wit h zeros.
Descriptio n
Example
&&
Called Logical AND operat or. If bot h t he operands are non zero t hen t hen
condit ion becomes t rue.
(A && B)
is false.
||
Called Logical OR Operat or. If any of t he t wo operands are non zero t hen
t hen condit ion becomes t rue.
(A || B) is
t rue.
!(A && B)
is t rue.
Descriptio n
Example
C = A + B will
assigne value of A +
B int o C
+=
C += A is equivalent
to C = C + A
-=
C -= A is equivalent
to C = C - A
*=
Mult iply AND assignment operat or, It mult iplies right operand
wit h t he left operand and assign t he result t o left operand
C *= A is equivalent
to C = C * A
/=
C /= A is equivalent
to C = C / A
%=
C %= A is equivalent
to C = C % A
<<=
C <<= 2 is same as
C = C << 2
>>=
C >>= 2 is same as
C = C >> 2
&=
C &= 2 is same as C
=C&2
^=
C ^= 2 is same as C
=C^2
|=
C |= 2 is same as C
=C|2
(class/interface type)
Operato r
Asso ciativity
Post fix
Left t o right
Unary
++ - - ! ~
Right t o left
*/%
Left t o right
Addit ive
+-
Left t o right
Shift
Left t o right
Relat ional
Left t o right
Equalit y
== !=
Left t o right
&
Left t o right
Left t o right
Bit wise OR
Left t o right
Logical AND
&&
Left t o right
Logical OR
||
Left t o right
Condit ional
?:
Right t o left
Assignment
Right t o left
Comma
Left t o right
Synt ax:
The synt ax of a while loop is:
while(Boolean_expression)
{
//Statements
}
Synt ax:
The synt ax of a do...while loop is:
do
{
//Statements
}while(Boolean_expression);
Synt ax:
The synt ax of a for loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
Synt ax:
The synt ax of enhanced for loop is:
for(declaration : expression)
{
//Statements
}
Synt ax:
The if St at ement :
An if st at ement consist s of a Boolean expression followed by one or more st at ement s.
Synt ax:
The synt ax of an if st at ement is:
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
Synt ax:
The synt ax of a if...else is:
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
Synt ax:
The synt ax of a if...else is:
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the one of the above condition is true.
}
Synt ax:
The synt ax for a nest ed if...else is as follows:
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
Synt ax:
The synt ax of enhanced for loop is:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
A met hod definit ion consist s of a met hod header and a met hod body. Here are all t he part s of a
met hod:
Mo difiers: The modifier, which is opt ional, t ells t he compiler how t o call t he met hod. This
defines t he access t ype of t he met hod.
Return T ype: A met hod may ret urn a value. The ret urnValueType is t he dat a t ype of t he
value t he met hod ret urns. Some met hods perform t he desired operat ions wit hout ret urning a
value. In t his case, t he ret urnValueType is t he keyword vo id.
Metho d Name: This is t he act ual name of t he met hod. The met hod name and t he paramet er
list t oget her const it ut e t he met hod signat ure.
Parameters: A paramet er is like a placeholder. When a met hod is invoked, you pass a value
t o t he paramet er. This value is referred t o as act ual paramet er or argument . The paramet er list
refers t o t he t ype, order, and number of t he paramet ers of a met hod. Paramet ers are
opt ional; t hat is, a met hod may cont ain no paramet ers.
Metho d Bo dy: The met hod body cont ains a collect ion of st at ement s t hat define what t he
met hod does.
Class - A class can be defined as a t emplat e/ blue print t hat describe t he behaviors/st at es
t hat object of it s t ype support .
A sample of a class is given below:
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
If a met hod does not handle a checked except ion, t he met hod must declare it using t he thro ws
keyword. The t hrows keyword appears at t he end of a met hod's signat ure.
You can t hrow an except ion, eit her a newly inst ant iat ed one or an except ion t hat you just caught , by
using t he thro w keyword. Try t o underst and t he different in t hrows and t hrow keywords.
For a complet e det ail of t he Java Programming language, it is recommended t o go t hrough our simple
Java Tut orial.