Write your First Programe in java.(Your first java cup).
by Sandun Dhammika Perera.
Little Histroy about java.
Java is considered as an general purpose Object oriented programming language.
java language is by sun microsystems and at that time they created this language
for use in their consumer products . But today with using java , we can create many
general purpose applications . In that time (before java) they are heavily depends on
C++ language for consumer products . But they got problems in C++ shuch as dynamic
memory allocation , multiple inheritance . Like these things .let me said you C/C++
are very different . but C++ is mostly backward compatiable for C code . But not at
all. In C++ you can do object oriented programs as well as pure structured software
developing . That is why C++ is considered as an hybrid programming language .But in
java you always have to done your programming in Object Oriented manner. Before you
learn java , that is the reason that you had to learn Object oriented concepts well,
even before you write your first program in java.
What is an Compiler
in the old days there are no compilers . people write their programs using a macine
language . Machine language is something like this
hmm, look like very interesting ha , yes purly it was interested . But the
problem is that it is ineffiency to write a big complex program in machine language . It
can make lots of time and save lots of bugs in code . the above picture shows us how
program is loaded at the memory location 0920 to memory location 0A70 . each memory location
can be represent code or data . assume this represents code , then the thing is each location or
few locations are representing a one instruction . Threby sequence of 100, one thousand ,million
instructions represents a working program . what about if we can write the program as thir instructions
as they are like this .
Truly wonderful isn't it , you can write opcodes like add, call , xor . They are assembly
language instructions . Writing like this program , you have to compile through an assembler .the picture
about can be considered as a program that written in assembly language . But today development , I mean 99% of
the software enginnering does not delay on the underlaying assembly language .Beacuse assembly language is
more closer to the computer hardware not to the problem or a logic that you are going to slove . A good
programming language should implement these two properties well , but partically no programming language
can do it , shuch as a programming language closer to the problem have hides the underlaying things that
you can see in a language like assembly . what are the underlaying thing that compilers hide or abstract ?
My friend I can give an example , think that you're writing an game application.your using a timmer .
you can program using the timmer , but you never know know that machine underlaying things shuch as
interrupts using in your program . Sometimes this makes dependencies in programs . That's why those
languages like (hybrid) C++ allows you to write some of assembly language codes inside your C++ program
.In java you cannot directly done this(using native keyword you can) . However that was beyond our scope.
Java is generally considered as it was close to the problem or logic that you write . So a code in java
looks something like this .
if ( my_name == "sandun"){
System.out.println ("I am sandun");
}
if you don't underestand this segment of code , don't worry . Just read it . This is the
logic behind this "if my name is equals to sandun then print I am Sandun" . Oky that means .
"if my name is equals to sandun then print I am Sandun" << written in English
if ( my_name == "sandun"){
System.out.println ("I am sandun");
} << written in java
that makes cool ha ! . Java is like a language like english that are close to the human being .
compare this segment of code with the above assembly language code segment ! . That's why we need high level
languages.
What is the concept of Object Oriented.
The concept of Object oriented is very simple . Most people thinks more and hards this concept . Think first
in our nature everything is an object . So a computer also an object no ! . even the program that you write is
an object . Not onely one object ! you can see take a car as an object . It was constructed with thousands of
objects again sub-objects . even sub-objects also have sub-sub-objects . by the way In Object oriented
programming , you can think that your program as a set of objects . always try to think about the real
world objects and the objects are same when you learning object oriented concepts . Oky there are concepts of
object oriented , that you shoud underestand before you write your first program in java.
Encaplusation
Something big word means something small . Again think about real world objects .
Think about a telivision in your home . You can watch a drama using it(your faviourate love sotry ROMMEO and JULIT)
, if you don't like it you can change the channel , you can change the voloume of the sound and you can make color
changes to it . But are you interested about what are the things that inside your TV. However to draw the picture in the
screen in your TV , there are lots of electronic circuits are hided by a nice cover , only interfaces can be seen to
the outside . It isn't good ? We can see TV as a nice and clear object in that way . The idea behind encaplusation is
this . An object has it's things that seen from outside and some are internally hidden . In java there are four
access modifiers to implement this . they are public , default , protected and private . private methods has less
visibility than the public . public means open to everybody . So , encaplusation is data hiding is'nt it . no my friend
thre is another advantage in this .that is binding its attributes and methods . By binding object will become a nice interface
to the one who are public to the object ( in television who are watching television) . He needs to watch the television
but he does not needs and needs not to know what are the complex electronics inside my tv .
Inheritance
This is also a big word know. But also here it has an small meaning ! . Think about a real example .
let me say you are a inherited version of your parents .what that means means you like your parents but you
even have your specific behaviour is'nt it . Yes !.It something like superclass is like your father and you are
the subclass . The properties of your father is yours . Like this . The objects in programs are also can inherit.
when they are inherit they also gets their superclass attributes and methods . But the inherited object can be
have many specific things like "I done what I want to done! not my father said to me done" .
Polymorphysm
The word ploy means many forms . thik about an real world example again . Think about a door , you can
open it , alos you can open a window and you can open a book . That means the operation "open" have several meanings
in different types of objects . So implementing different types of operations for the one single form or a method
means polymorphysm. Thre are static polymorphysm and dynamic polymorphysm . In java language static polymorphysm is
implemented by the ovrloading and the dynamic polymorphysm is implemented by the overriding .
Message passing ( interface)
Oky our program is a set of objects that have attributes and methods . like real world example . A television
we can interact with that using its well definided interfaces . shuch as voloume controls ,on/off buttons and channel
chainging options . Well that makes the television running and useful . Like that in our program objects have a communation
mechanish to talk with each other . This is known as message passing . Normally this is implemented using the interface keyword
in java
Write Your First Program In Java
Before I discuss anything look at that code that bellow ,
First.java
1: class First{
2: public static void main ( String args []){
3: System.out.println ("Hello world ");
4: }
5: }
Hmm, this is known as a java source code . To be a source code file you should type these things in a text
editor and save the file as First.java. Please note that you don't have to include 1: 2: 3: in the code , simply
they are line numbers for your help.To type this code you needs an text editor . In windows you have already a one.
that is our notepad. you can open notepad via start menu but that makes not smart . To be smart, as a future programmer
please do these steps . push the start button and then select run.
in there you can type cmd.exe and press ok
then you have a command window , in the command prompt you can type commands .
in the command prompt you are in the C:Documents and settingsSNOWCAT , you have to
move to the C: and make a new folder java and we are writing our First.java inside it.You can change the
directory by using cd command cd .. for change the parent directory . in here for "SNOWCAT" my parent
directory is the "Documents and Settings" . and the command for making a new directory is the mkdir command
for example "mkdir java" will create a directory called java inside your current directory .and I want to move
inside to the java directory
well Then I can open the notepad here . by typing "notepad First.java" after you type this code
the things will look like this
Then this is the time to compile this program to compile you can use the javac command and to run you
can run the java command .
The simple syntax to compile is :
javac [Source Code File Name ]
The simple way to run your program is :
java [class file name ]
oky then lets compile .....
what was happened . 'javac; is not recognized as an internal or external command ,. Yes I forgot to tell
you this know . you have to set the path . To set the path you have to type something like this.

Wonderful isn't it
Yes it was compiled ! and run nicely . Wonderful isn't it !