2.LETS LEARN JAVA BASIC-STARTING THE COMPUTER.
Your computer is in front of you. All switched off. There is no electricity flowing through any of the circuit inside the computer. Whether you have a laptop or desktop in front of you, can you identify I/P devices, O/P devices & Processing Unit (System Unit) in front of you? There is no electricity flowing through it. However, all of your data, operating system, computer etc. is safe stored in secondary memory even when power is off. That is the beauty of secondary memory (hard disk) present inside the laptop case or system unit. Other parts we have seen in previous chapters like CPU, primary memory etc. will be visible to you if you get a chance to visit a repair shop. You can discuss with the service engineer to understand what are the components in the computer.
Now switch on the computer. What happens? Actually many complex processes happen at it. However we will try to simplify it. The electricity flows through all the components of the computer. CPU also gets alive just like other components. We know CPU is the brain of the computer. So this part starts the work. What does it do?
We have understood that CPU is the instruction executor i.e. it will execute the instruction given to it. So it is ready to take the instructions and execute but where are the instructions? Well, all the instructions are in the secondary memory currently. But the instruction to load instructions from secondary memory should also need to be given to the CPU. And where does that instruction come from? Well the answer is BIOS (Basic Input Output System).
BIOS – Now as we introduce a new component in computer, we should also elaborate our knowledge on Primary memory. We know RAM (Random Access Memory) is a primary memory. But there is one memory called ROM (Read Only Memory) which is also a primary memory. It is a hardware (chip to be precise) fixed on your computer motherboard. It stores the BIOS. BIOS contains all the instructions to: Load instructions (related to operating system) from secondary memory Control all the hardware devices/components connected to computer. ANALOGY The best analogy that can be given here is our body. Our entire body parts are connected to brain via nerves. Any pain or sensation goes to and from the body part (say the finger) is also felt by the brain. The movement of feet, hand or body is also done on the instruction given by the brain.
Similarly, BIOS has instructions for CPU that take the control of all the input devices, output devices & System Unit components and drives them as per the instructions you get from here onwards.
We know that RAM is a temporary memory i.e. there is no data in it when power is switched off. Do you think ROM also has same tendency? The answer is NO. If it does, it would forget the BIOS instruction which is the starting point of loading instructions for CPU. Are we smart enough to do the job that BIOS does every time we start the computer? Probably NO for most of us. That’s why BIOS is installed on ROM chip as permanent data which remains on it even when computer is switched off. ROM only needs to read the starting instruction. CPU is not required to write anything on it so it is not designed as writable.
There is a complex process called NOR. There is a complex process called flashing through which data on the ROM can be over-written but it is complex & may lead the computer to a non-functional state.
What do you think CPU read instructions from BIOS directly or follow its usual way of reading the instructions from RAM the answer is latter. The instruction from ROM is first loaded into RAM which then goes to CPU for execution.
CPU goes on executing the instruction as delivered from ROM (BIOS). As told earlier, the instructions are conveyed to all physical devices/components, output device & input device, if secondary memory, & checks if they are connected functionally correctly.
If there is any issue, the CPU will try to send the message to the user by displaying error on screen/graphic. All these instructions are present on BIOS. Please note that BIOS is installed by the computer manufacturer so it can vary from one computer brand to another. So now we understand that the error helps we observe when there is issue in hardware device, it is reported by BIOS. Operating system has not yet loaded into RAM.
Once BIOS is loaded & into RAM & executed by CPU, next instruction CPU receives from BIOS is to start load the OS into RAM. We have discussed about OS. It is the umbrella under which all your software (like ms word excel games internet browser etc) are present & it is the main software which manages all the uses & devices connected to the computer & act as the interface between user & computer because doing all these for which OS does is very cumbersome for the user. OS loads software is now loaded into RAM. CPU starts executing the instruction of OS. In short, control is transferred from BIOS to OS. Now you see the OS logo on the screen like Windows logo or Ubuntu logo or soon OS load shows up login screen. device is required to enter the password and/or username. It depends on the setting of user access in your OS whether password only is required when there is only one user setup or username password if multiple users are setup or no password if there is no authentication set up. You authenticated successfully want home screen of the OS opened in front of you. (Show the desktop Screen of Windows / Ubuntu / Mac) (caution) You will see small pictured if it is GUI based OS. If it is brand new OS installed on your computer, you will not see many option / menu / icons but you will definitely see some mandatory icons & optionally see some OS as a place where a lot of things over all of the aspects of OS. Anyway you will see clock somewhere on the desktop. you will see “Computer” icon which shows the summary of all the secondary memory vol attached to the computer. The individual secondary memory will be called drive. There should be at least one drive visible of the time it as hard disk but it is not necessary. It is possible that your OS is installed in pendrive or memory card. So it is possible to see multiple drives like HD, pendrive, etc. I mean storage devices one command to computer will bedisplayed / represented through a device icon. It is also possible to divide a single hard disk into multiple drives & each drive will be represented separately. you will see “recycle bin” or “trash” A place where deleted files, folders, programs to go. It gives sometime to retrieve back the deleted files if your change your mind. However, it will be cleared than your dustbin (i.e). You will see device driver icon which displays all the drivers working on the computer. What are drivers? Driver is a software which is an essential interface between OS & a device connected to the computer. ANALOGY Remember our analogy in OS chapter. We demonstrated a shopping mall supermarket in a small town of china which is equivalent to our OS. An Indian going to that supermarket will be equivalent to the user. Suppose, supermarket is having a new deal with a total suppliers which supplies milk products. When the representative of both parties meet, there will be certain points discussed between the two 1)How much quantity of milk product will be supplied by the supplier everyday? 2)Which vehicle will arrive for supply? 3)At what time the vehicle will arrive? 4)Who will be the PoC from supplier? his contact # & email id? 5)What will be the payment method? Note the PoC (point of contact). Supermarket will interact with this supplier through this PoC. This PoC is equivalent to the driver. OS interacts to the a device through a driver. You insert a speaker to your computer for listening music. Does OS need driver to interact with the speaker (to send music/song to speaker from your music player software)? Ans: no You connect a mouse to your computer. Is driver required here? Yes. If no, how OS will recognise signal/movement from the mouse. You have installed graphic card in your computer because you love playing game. Is driver required for OS to interact with graphic card (even if graphic card is embedded into your motherboard)? Ans: yes Is driver needed just for do normal monitor display? Ans: yes. That otherwise would have shipped with your computer. So the summary organised today OS to interact with the devices of the computer. DO IT YOURSELF: Try connecting a device to the computer. Your OS will start downloading & configuring the device for that device. watch this happening. fi What is a file? Note: we are introducing these terms since these are common terms which we will come across frequently. In real life, your class X- notebook is an unique individual document. It is one paged. It is equivalent to one a file in computer. your weekly assignment is an individual document. It is multi-paged. It is also equivalent to a file in computer. However, file in computer is more than this. A file is or something which occupies some bits of data in the computer memory & it contains some data. This data can be text, audio, video, image or various other type of data that we will come across later. e.g. [Small hand-drawn diagram:] File ‹fi [ ] ‹fi memory Every file has a name. There is a clear format of the file name. & The filename has two parts
Hence the filename in computer contain two parts separated by a dot (.) First part is name. Second part is extension name which tells the computer what type of file is it. Typically extension name is of 3 letters but can be more or less too. If you have some files on your computer e.g. .txt, .jpg, .png etc. If it is a file containing only text, its extension will be .txt. So you can create such a file with the name like notepad.txt, list.txt etc. Have you observed the filename of a song stored on the computer? It has mp3 extension like Hello.mp3, Chak De India.mp3 etc.
Try to observe the filename of a video song or movie stored on the computer? It has extension like .mp4, .mkv, .mov, .wmv, .mpeg etc. You will see many official files having extension of .bat, .dll, .exe. Beware before you play with these files.
Hence, extension name matters a lot to the computer. This is the way it identifies what file is it. E.g. If you double click on Hello.mp3 the computer will try to open with music player application rather than opening in Notepad or MS-Word application. DO IT YOURSELF: Find out as many filename with different extension name.
What is a folder or directory?
Folder in computer is analogous to folder in our real life. Some people call it file in which they keep all their documents. Similarly folder in computer is used to store one or more files within it. Moreover, we can create a folder inside a folder. DO IT YOURSELF: Create a folder on your desktop (home screen). If on creating folder within this folder & create files (.txt) in each of the deepest folder. Something like this [Hand-drawn tree diagram of folders and files:]

What is saving a file means? Saving a file means sending the latest content of the file to the
secondary memory so that your content is not lost or over-ridden even if your computer is switched
off.
Having understood few basic terminologies we are eager to write our first program.
Hey but wait! Java or Just like our computer’s OS is having software for creating and editing a text
file (Notepad) or it is having the software for opening a website (browser) or it for playing a .mp3 file
(software) so there required software within it so that we can write & execute our Java program?
This needs to be checked. The Java programming language is developed by another company
(Oracle) & your OS is developed by another company (MS, Apple, etc.) so it is not necessary that
the software required for writing & executing a Java program will be already available on your brand
new OS.
Steps to check in Windows or Console similar name
Open Command Prompt (Cmd) by typing similar name
A black screen opens. This is the traditional way (1) you initially with OS or indirectly (2) complete. I
know it was difficult. (3) It was difficult that’s why computer revolutions brought Graphical User
Interface where you will see fancy icons & menu instead of memorizing & typing commands
onInstall means establish that software in the OS.
Boring dark background screen
Type: java -version
If it displays some version, then great
If it does not display & gives negative response, then we will have to install Java
Steps to install Java
Go to www.oracle.com
Go to JDK
Download
Once done, repeat the above step to check if Java is installed or not.
Please note that the software which has been installed is called JDK/JRE. You will ask why this
software does not show up appearance just like other software like browser, ms-word etc.
Don’t worry, that software is ready to take the main job of readying our program & sending to cpu
for execution. We will go with the most simplest way of writing a Java program & then later I will
introduce you the software like ms-edge, ms-word etc. which will have its UI appearance & will help
you code more conveniently. I funny way.Create a folder on your desktop (home screen). Namely
MyFirstJava. You can create this folder anywhere in the drive available on your computer. For e.g.
someone’s computer might show you some drives with names like C:, D:, E: etc. If you have
already attached external storage devices like pendrive, memory card, external harddisk etc., your
computer will show devices for those storage devices too.
Make a folder MyFirstJava to go inside this folder. You will see a blank area. Now since our Java
program does not have UI, we will take help of notepad application to write our Java program.
Open notepad application & write anything (blabla). Instead of Ctrl + S or File fi Save As, give a
name to the file but main point to note that notepad application will want to give this file an
extension (.txt). However, we don’t want that. We want this file to have an extension of .java.
Hence, enclose the filename both extension (.java) like MyFirstPrg.java. Click Save.
You will notice your file is saved correctly (.java) extension. We wanton our OS & our Java software
(JDK/JRE) to know that this file contains Java program & not just random text.
Now with this file opened in Notepad to write your first program in a high-level language i.e. Java
class myFirstPrg
{
public static void main(String[] args)
{
System.out.println(“Yeah, myself Sam wrote my first program successfully”);
}
}
Save & close this file. No worry if you don’t understand anything.
Now, we need to tell the Java software that we have a small “instruction” written in high-level
language Java & please help to ask CPU to execute this instruction. For that, we will open
Command Prompt again.
As you can see, CMD prompt was already there provided by OS. It is actually a representative of
OS through which you are interacting with computer. You can feel now that OS is providing you all
the ambience (facilities) to interact with computer.
Open cmd & go to the directory where your myFirstPrg.java is residing & type the command : cd
C:\MyFirstJava
Now it is time to repeat the concept.
Let the instructor (you have written in previous chapter) through compiler.
Only language you have written is though high level language. Then what do we need to do? We
need to compile it and make language into machine language (low level language) so the computer
can understand.
Remember, you have installed Java (JDK) to compile your Java source code into machine
language.
By now if you have successfully compiled your myFirstPrg.java
If yes, then great!
If not, go to the steps given below.
Fig 2.1 – Our program is an instruction which we gave as I/P to computer.
CPU processed it.
We got “message displayed on screen”.
Fig 2.2 – Yes, we provided I/P i.e. program (instruction) through an input device named keyboard. It
got processed by our CPU.
We got the “message” displayed on O/P device named monitor.
Nobody technology is changing rapidly & some devices can act as both I/P & O/P device.
Some of you might be knowing touch-based laptop or tab tablets.
Do it yourself! Try to find out features of your device.
There is one difference in analogy #2.1. Your juice has predefined set of instructions which it
follows every time you pour an input. In computer, each instruction varies with each program & be
need to make clean.
Congratulations, you have successfully compiled & executed your first program (if you have not
done it before). But other steps you were successful in getting one task done by the compiler.
You can very well revisit the concept covered so far & apply that knowledge with this chapter on to
get your understanding better. By we demonstrated following in initial chapter by:
Fig 2.1
Initially, our program is an instruction which we gave as I/P to computer.
CPU processed it.

We got “message displayed on screen”.
Fig 2.2 – Yes, we provided I/P i.e. program (instruction) through an input device named keyboard. It
got processed by our CPU.

We got the “message” displayed on O/P device named monitor.
Tell computer what to do with the I/P we data we provide. But in the above example, we didn’t
provide I/P data, we just provided set of instruction (program).
FUN ACTIVITY – If you take newspaper everyday at your place, pull out 4-5 advertisements of
computer & try to understand the features they mention, especially CPU.
Apply the concepts of Fig 2.4 on above hands-on to understand what would have happened.
You typed your program in a file named myFirstProgram.java and saved it by going to File fi Save.
Alternatively, you can press Ctrl + S button simultaneously.
Saving is important if you want to see your written program again & again.
Referring Fig 2.4, you entered program through I/P device (keyboard) & as you saved it, it got
stored in your secondary memory permanently. It will not delete now even if you switch off the &
restart the computer.
As you compiled your Java program, (it is called java command) & your myFirstProgram class got
created, & also got stored permanently in your secondary memory.
As you executed your compiled program, i.e. you did java my First Program the instruction got
loaded into your primary memory. Then Control Unit gives instruction by instruction & sent the
message we provided in the program to the O/P device (monitor).
In Fig 3.1, you should be now able to fit ‘javac [Java compiler] & relate what ‘javac
myFirstProgram.java’ command would be doing. It just invokes Java compiler & requests to
translate the instruction & gave in high-level language (Java) into machine language.
Do you think o Analogy 3.1 applies here?
No, here it didn’t take instruction by instruction & compiled it compiled entire set of instruction in one
go.
Do you think Analogy 3.2 applies here?
Yes, it fully applies. You can execute myFirstProgram.class as many times as you wish.
Analogy 3.1. – Sam’s mother
You
Sushi
Sam
Computer
javac (Java compiler)
Apply Fig 4.1 on your hands-on now.
From Fig 4.2, can you tell if javac is system software or application software.
From (analogy 5.1), we can say that as we created myFirstProgram.java & saved it, we actually
made it permanent. Now, it will still remain on the computer even when you restart computer. It
became permanent as it is saved to secondary device memory. Same is the case with
myFirstProgram.class.
Applying analogy 5.3 Can you tell how the content of myFirstProgram.java is stored in secondary
memory. Note that my First Program also has occupied separate space on memory.
Explanation of program – Let’s try to understand the small program we wrote above
1 import java.io.*;
2 class myFirstPrg
3 {
4 public static void main (String[] args)
5 {
6 System.out.println (“Yeah, myself Sam wrote my first program successfully”);
7 }
8 }
What about the software you just created (myFirstPrg)? Yes, let us also a very small software.
If you glance through the program, you might be able to relate that Java is actually an Object
Oriented Programming language since we can see words like “class”.
Start applying the concept from chpt 6 in Java – all the activities revolve around class. We will start
to write class.
First like car is a class in. As we defined class as an umbrella under which objects of similar
attributes & behaviours are kept or defined, there is no restriction to class name. Literally we can
put any name here. There is no restriction to class name, no we have created a class named
myFirstPrg, we can create n nos of objects of the class myFirstPrg.
Understand at from another example. We create a class Car & then we could create n nos of
objects of the class Car as no.
Object 1 —— TataNexon
Object 2 —— Maruti
Object 3 —— TataElectra
& so on.
We know as we also mentioned that a class has attributes & behaviours.
Attributes – are represented in Java through class variables (here we have none)
Behaviours – are represented in Java through methods (here only defines 1 method).
In short this method is showing the behaviour of the class that it displays a text on the output
device. To display the same the user need to provide the text enclosed within the instruction in high
level language – System.out.println(“your text”); (More details will come later).
Note the usage of curly brackets. It is used to mark the beginning & ending of a section of the
program. For e.g. Curly bracket at line #3 tells that class definition starts from there & curly bracket
at line #7 marks the end of class definition.
Similarly curly bracket at line #5 tells the method’s definition starts from wherever it ends at line #7.
So next next
We created & defined a class name myFirstPrg
We didn’t create its object explicitly
We didn’t define any attributes of this class
We defined one behaviour of the class by defining method named main ()
We understood meaning of curly braces
Do it yourself
Let’s create one more program where we can apply few more concepts we learnt while introduction
of classes & objects.
Create a file named Car.java in a folder you created earlier (myFirstJava).
Write the following piece of code in it.
class Car
{
String color; // Car (arguments)
String fuelType;
String engine; // this.color = col
String makeAndModel;
String type;
public void run()
{
System.out.println (“Running…”);
}
public String return Smoke()
{
return “Smoke”;
}
public void applyBrake()
{
System.out.println (“Stopping the car…”);
}
}
Save & close this file.
Now create another file named TestCar.java. Write following code in it.
class TestCar
{
public static void main (String[] args)
{
Car myCar = new Car (“white”, “petrol”, “624cc”, “Tata Nexon”, “hatchback”);
Car my Neighbour Car = new Car (“grey”, “diesel”, “1000cc”, “Maruti Baleno”, “sedan”);
myCar.run();
System.out.println (myCar.returnSmoke());
myCar.applyBrake();
}
}
Explanation
In TestCar we created two objects of a Car type.
Note: You can think of class as type. When you create an object in Java, you create the object of a
particular type. In real world also, any object can be classified as belonging to a type.
Ask few question to yourself to get the understanding of the concept from any object from real
world.
e.g. if mobile – we ask what type of object is it? we one person can say it is an object of electronic
type. Other person can say it is an object of wireless device type.
Both are correct. You can create a class named Electronic or a class named wireless & create an
mobile object against both class.
See the way how we have populated the attributes / value of each object. Needless to say, color,
fuel Type, engine, make and Model are attributes of the object of this class (type).
Using these two objects, we are implementing / executing behaviours of the two objects viz. run,
returnSmoke, apply Brake.
The Car has complete definition of the Car class to which we have defined attributes &
behaviours.
Remember, “attributes & “behaviours” are the terms we are using from the real world.
We can see how we are implementing them in Java. Similarly, there are Java – specific term here
Attributes – are represented in Java, through class variables.
Behaviours – are represented in Java through methods (here only defines 1 method)