A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely.
The earliest programming languages predate the invention of the computer, and were used to direct the behavior of machines such as Jacquard looms and player pianos. Thousands of different programming languages have been created, mainly in the computer field, with many more being created every year. Most programming languages describe computation in an imperative style, i.e., as a sequence of commands, although some languages, such as those that support functional programming or logic programming, use alternative forms of description.
The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning). Some languages are defined by a specification document (for example, the C programming language is specified by an ISO Standard), while other languages, such as Perl 5 and earlier, have a dominant implementation that is used as a reference.
List of programming languages:
A
B
|
C
|
D
E
Java Programming:
Basics of Java Programming ( A Work In Progress) Wh a t Co n st it u t e s a Java Pr og r am ?
A Java p r o gr a m con si st s o f on e o r m o re sou r ce f ile s.
Ea ch sou r ce f ile is ca lle d <cl a ss n am e >. ja va, wh e r e <cl a ss n am e > is t h e n a m e o f t h e
cl a ss con t a in e d in t h a t so u r ce f ile a n d . ja va is t h e e xt e n si on t h a t id e n t if ie s th e f ile a s a
Java sou r ce cod e f ile . I n g en e r a l, e a ch sou r ce f ile con t a in s o n e cl a ss. T h e n a m e o f t h e
cl a ss m u st m at ch t h e n a m e o f t h e f ile ( wit h ou t t h e e xt e n si on ) e xa ct ly.
T o e xecute a Java p r o gr a m , you f ir st n e e d t o com p ile t h e so u r ce cod e in to b yt e co d e .
Byt e cod e f ile s h a ve t h e na m e <cl a ss n a m e >.cl a ss. I f you ’re wo r ki n g f r om t h e she ll, t h is
is d o n e u si n g t h e ja vac com ma n d ( wh e r e “ c” is f o r comp ile r ) . Usua lly we will b e
com p ilin g u si n g t h e Xcod e I DE. O n ce com p ile d , t h e pr o gr a m is e xe cut e d u si n g t h e
Java in t e r pr e t er ( a . k. a . t h e Java Vir t u a l M a chin e o r JVM) . Fr o m th e she ll, th e JVM is
in voked wit h t h e “ ja va” comm a n d . Aga in we will u sua lly b e d o in g t h is wit h t h e Bu ild a n d
G o o r De bu g o p t ion s o f th e Xcod e I DE.
A Java p r o gr a m m u st h a ve a r ou t in e called ma in ( ) , wh ich is t h e st ar t in g p o in t f or
p r o g r am e xecu t io n . m a in () will a lwa ys lo o k som e t h in g like th is:
public static void main (String args[]) {
// insert code here...
System.out.println("Hello World!");
}
So u r ce Co d e F o rm a t t in g
Ke e p in m in d th e f o llo win g wh e n wr it ing Java sou r ce code :
• Java is case sen si t ive ( “ fo o ” , “ F oo ” , an d “ f OO ” a r e a ll con si d e r e d t o b e d if f e re n t in
Java) .
• Ever y lin e o f code in Java m u st en d in a se m i-colo n ( “ ;” ) .
• Java d o e sn’t ca r e ab o u t wh ite spa ce ( lin e br ea ks, t a b s, e t c. ) b ut con si st e nt a n d
t h o u g h t fu l u se o f wh it e spa ce m a kes co d e much e a si e r to r e ad a n d is p a r t o f
g o o d p ro g r am m in g st yl e .
• Yo u can cr e at e comm e n t s in Java u si n g / / f or a si n g le - lin e co m m en t o r / * */ f o r a
b lo ck com m en t . Exam p le s:
// This comment is just this one line
/* This comment includes many lines. Code inside is inactive
(“commented out”)
int x;
x = 5;
*/
Ba si cs o f Va r ia b le s a nd Assig n m e n t
L ike a lm o st e ve r y pr o g ra m m ing la n g u ag e , Java h a s var iab le s a n d a n a ssign m e n t
st a t e m e n t t o p u t valu e s in to a var ia b le . T h e re a r e t wo cl a ssif icat io n s o f va r ia b le s in
Java: p r im it ive t ype s an d r ef e r e n ce var iab le s.
T h e pr im it ive t ype s we a r e in t e re st e d in a r e in t, d o u b le , a n d Bo o le an , f o r in t e g er ,
f lo a t in g p o in t, a n d t r u e / fa lse valu e s r e spe ct ively. An e xam p le of d e cl ar in g a nd
a ssig n in g t o p r im it ive t yp e s:
int x; // declare an integer variable x
x = 3; // assign the value 3 to x
double y; // declare a floating point variable y
y = 0.3; // assign the value 0.3 to y
boolean b; // declare a boolean variable b
b = (x < y); // b will be assigned false, because
// the expression x < y evaluates to false
A r e f e r e n ce var ia b le e it h er p o in t s t o a n o b je ct o r is n u ll (n u ll is a keywor d f or a r ef e r en ce
var ia b le t ha t is e m pt y) . An e xa m p le o f d e cl ar in g a n d u si ng r e fe r e n ce var iab le s:
Foo f1;
Foo f2;
f1 = new Foo(“I’m a foo!”); // create a new Foo and have f1
// point to it
f2 = f1; // f2 now points to the same Foo as f1
f1 = null; // now f1 doesn’t point to anything but
// f2 still points to the Foo created before
Ba si cs o f Cla sse s
Java is a n o b je ct - or ie n t e d p r o gr a mm in g ( OO P) la n g u ag e a n d t h e f un d a me n t a l u n it f o r
o r g a n izi n g co d e is t he cl a ss. A cl a ss ha s p r op e r t ie s (a . k. a . m e m be r d a ta ) wh ich co n t a in
cha r a ct er ist ics a n d d a t a ( in g e n er a l, no u n s or a d je ct ives) an d m e th o d s ( cod e) f o r
a ct io n s ( ver b s) . Pr o p er t ie s a n d m e t ho d s t o ge t h e r ar e calle d, colle ct ively, me m b er s.
He r e is a n e xam p le o f a pa r t ia l cl a ss fo r ho ld in g a comp le x n u mb e r in r e ct an g u la r f o r m.
public class RectComplexNumber {
double a; // the real part of the complex number
double b; // the complex (i) part of the complex number
// constructor—create a new complex number with given values
public RectComplexNumber(double aInput, double bInput) {
a = aInput;
b = bInput;
}
// add this complex number to addend and return the result
public RectComplexNumber add(RectComplexNumber addend) {
return new RectComplexNumber(a + addend.a, b + addend.b);
}
// etc.
}
Usi n g Bu ilt - I n Cla sse s an d Pa cka g e s
T h e Java De velo p er ’s Kit ( JDK) com e s wit h a h u g e a mo u n t of b u ilt - in Java code t h a t
can b e u sed t o bu ild a p p licat ion s. Ho we ver , fo r a Java cl a ss t o u se t h is code it m u st
id e n t if y t h e pa cka g e t h a t con t a in s t ha t cod e . T h is is d o n e a t t h e t o p of t h e sou r ce f ile
u si n g t h e imp o r t ke ywo r d . Exam p le :
import java.util.*; // gain access to all the code in the
// java.util package (* means all)
import java.io.File; // gain access to the File class of
// the java.io package
Pu b lic vs. Pr ivat e
L ike a ll O O P la n g u ag e s, Java h a s me cha n isms t o sup p or t e n cap sula t io n . I n ge n e r a l,
we d e cl a r e a s p u b lic a ll pr o p er t ie s a nd m e th od s we wa n t code o u t si de o u r p a cka g e t o
u se. O t h er wise t h e d e f a u lt is t h a t a ll cod e in th e sam e p a cka g e ca n a cce ss t h e
m e m b er s. I f we o n ly wa n t th e code in t h at p a rt icula r cl a ss t o a cce ss m em b er s, we
d e cl a r e t he m a s p r ivat e .
G o o d e n cap sula t io n st yl e sug g e st s t h a t a ll p r op e r t ie s h a ve n o n- p u b lic or p r ivat e a cce ss.
I n o t h e r wo r d s, t h e “o u t si d e wor ld ” sh o u ld on ly b e a b le to a cce ss or m od if y p r op e r t ie s b y
callin g m e t h od s. Th e r e , a r e , h o we ver , e xce p t io n s.
Ba si c F lo w o f Co n tr o l
T h e r e a r e t wo m a in f lo w- o f- con t ro l con st ru ct s: b r a n che s ( if - th e n ) a n d lo op s. Th e Java
syn t a x f o r a b r an ch is
if (x > 10000) {
System.out.println(“x is big!”);
} else if (x > 100) {
System.out.println(“x is medium.”);
} else {
System.out.println(“x is small.”);
}
No t e t h a t th e cod e in si d e t he cur ly br a cke t s for e a ch if or e lse ca n b e a r b it ra r ily comp le x
a n d t h u s m ig h t con t a in a dd it io n a l br a n che s.
Be lo w is a cod e f ra g m en t t h a t illu st r a t e s t h e ba si c Java syn t a x fo r a loo p :
int i; // loop variable
int total; // accumulator
// this loop adds up the integers from 1 to MAX
for ( i = 1; i++; i <= MAX) {
total = total + i;
}