0% found this document useful (0 votes)
69 views

My Java Notes

The document describes how to write and run a simple Java 'Hello World' program. It provides step-by-step instructions on setting up an environment to compile and run a Java file that prints 'Hello World' to the console. It also gives an overview of object-oriented programming concepts in Java like classes, objects, inheritance, encapsulation and polymorphism with examples.

Uploaded by

phil2014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

My Java Notes

The document describes how to write and run a simple Java 'Hello World' program. It provides step-by-step instructions on setting up an environment to compile and run a Java file that prints 'Hello World' to the console. It also gives an overview of object-oriented programming concepts in Java like classes, objects, inheritance, encapsulation and polymorphism with examples.

Uploaded by

phil2014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1|Page

Java Hello World Program: How to Write &


Run?
You need the following 2 software to create your first Java Program

1. The Java SE Development Kit

Please refer our last tutorial to download and install JDK

2. A Text Editor

In this Java Hello World example, we’ll use Notepad. It is a simple editor
included with the Windows Operating System. You can use a different text
editor like NotePad++ or use online java compiler.

Hello World Java – Your First Java Program Video


This video will help you learn how to start a Java program:

Click here if the video is not accessible

Steps to Compile and Run first Java program


Here is a step by step process on how to run Java program:

Step 1) Open Notepad from Start menu by selecting Programs >


Accessories > Notepad.
2|Page

Step 2) Create a Source Code for your Hello World program in Java

• Declare a class with name A.


• Declare the main method public static void main(String args[]){
• Now Type the System.out.println(“Hello World”); which will print
Hello World in Java.
3|Page

1
class A {
2
public static void main(String args[]){
3
System.out.println("Hello World");
4
}
5
}
6
This code is editable. Click Run to Compile + Execute

Run

Step 3) Save the file for Java Hello World program


as FirstProgram.java make sure to select file type as all files while saving
the file in our working folder C:\workspace
4|Page

Step 4) Open the command prompt. Go to Directory C:\workspace.


Compile the code of your Hello world Java program using command,
javac FirstProgram.java

Step 5) If you look in your working folder, you can see that a file
named A.class has been created.
5|Page

Step 6) To execute the code, enter the command java followed by the
class name, as expected output Hello World is displayed now.
java A

Note: Java is case sensitive Programming language. All code, commands,


and file names should is used in consistent casing. FirstProgram is not
same as firstprogram.

Step 7) If you copy and paste the same code in IDE like Eclipse the
compiling and execution is done with the click of a button Using IDE is
convenient and improves your efficiency but since you are learning Java,
6|Page

we recommend you stick to notepad for simple Java program execution

OOPs Concepts in Java: What is, Basics


with Examples
what is OOPS?
Object-Oriented Programming System (OOPs) is a programming
concept that works on the principles of abstraction, encapsulation,
inheritance, and polymorphism. It allows users to create objects they want
and create methods to handle those objects. The basic concept of OOPs is
to create objects, re-use them throughout the program, and manipulate
these objects to get results.
OOP meaning “Object Oriented Programming” is a popularly known and
widely used concept in modern programming languages like Java.

OOPs Concepts in Java with Examples


The following are general OOPs concepts in Java:
7|Page

1) Class
The class is one of the Basic concepts of OOPs which is a group of similar
entities. It is only a logical component and not the physical entity. Lets
understand this one of the OOPs Concepts with example, if you had a class
called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota,
etc. Its properties(data) can be price or speed of these cars. While the
methods may be performed with these cars are driving, reverse, braking
etc.

2) Object
An object can be defined as an instance of a class, and there can be
multiple instances of a class in a program. An Object is one of the Java
OOPs concepts which contains both the data and the function, which
operates on the data. For example – chair, bike, marker, pen, table, car,
etc.

3) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object
acquires the properties and behaviors of the parent object. It’s creating a
parent-child relationship between two classes. It offers robust and natural
mechanism for organizing and structure of any software.

4) Polymorphism
Polymorphism refers to one of the OOPs concepts in Java which is the
ability of a variable, object or function to take on multiple forms. For
example, in English, the verb run has a different meaning if you use it
with a laptop, a foot race, and business. Here, we understand the meaning
of run based on the other words used along with it. The same also applied
to Polymorphism.
8|Page

5) Abstraction
Abstraction is one of the OOP Concepts in Java which is an act of
representing essential features without including background details. It is
a technique of creating a new data type that is suited for a specific
application. Lets understand this one of the OOPs Concepts with example,
while driving a car, you do not have to be concerned with its internal
working. Here you just need to concern about parts like steering wheel,
Gears, accelerator, etc.

6) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data
and code. In this OOPs concept, the variables of a class are always hidden
from other classes. It can only be accessed using the methods of their
current class. For example – in school, a student cannot exist without a
class.

7) Association
Association is a relationship between two objects. It is one of the OOP
Concepts in Java which defines the diversity between objects. In this OOP
concept, all objects have their separate lifecycle, and there is no owner.
For example, many students can associate with one teacher while one
student can also associate with multiple teachers.

8) Aggregation
In this technique, all objects have their separate lifecycle. However, there
is ownership such that child object can’t belong to another parent object.
For example consider class/objects department and teacher. Here, a single
teacher can’t belong to multiple departments, but even if we delete the
department, the teacher object will never be destroyed.
9|Page

9) Composition
Composition is a specialized form of Aggregation. It is also called “death”
relationship. Child objects do not have their lifecycle so when parent
object deletes all child object will also delete automatically. For that, let’s
take an example of House and rooms. Any house can have several rooms.
One room can’t become part of two different houses. So, if you delete the
house room will also be deleted.
Advantages of OOPs (Object-Oriented
Programming System):
• OOPs Concepts in Java offer easy to understand and a clear modular
structure for programs.
• Objects created for Object-Oriented Programs can be reused in other
programs. Thus it saves significant development cost.
• Large programs are difficult to write, but if the development and
designing team follow OOPS concepts, then they can better design
with minimum flaws.
• It enhances program modularity because every object exists
independently.

Comparison of OOPS with other programming


styles with help of an Example
Let’s understand with example how Java OOPs Concepts are different than
other programming approaches.

Programming languages can be classified into 3 primary types

1. Unstructured Programming Languages: The most primitive of all


programming languages having sequentially flow of control. Code is
repeated through out the program
2. Structured Programming Languages: Has non-sequentially flow of
control. Use of functions allows for re-use of code.
10 | P a g e

3. Object Oriented Programming Languages: Combines Data & Action


Together.

Click here if the video is not accessible

Let’s understand these 3 types with an example.

Suppose you want to create a Banking Software with functions like

1. Deposit
2. Withdraw
3. Show Balance

Unstructured Programming Languages


The earliest of all programming language were unstructured programming
language. A very elementary code of banking application in unstructured
Programming language will have two variables of one account number
and another for account balance
int account_number=20;
int account_balance=100;
Suppose deposit of 100 dollars is made.
account_balance=account_balance+100
Next you need to display account balance.
printf(“Account Number=%d,account_number)
printf(“Account Balance=%d,account_balance)
Now the amount of 50 dollars is withdrawn.
account_balance=account_balance-50
Again, you need to display the account balance.
printf(“Account Number=%d,account_number)
printf(“Account Balance=%d,account_balance)
11 | P a g e

For any further deposit or withdrawal operation – you will code repeat the
same lines again and again.

Structured Programming
With the arrival of Structured programming repeated lines on the code
were put into structures such as functions or methods. Whenever needed,
a simple call to the function is made.
12 | P a g e

Object-Oriented Programming
In our program, we are dealing with data or performing specific operations
on the data.

In fact, having data and performing certain operation on that data is very
basic characteristic in any software program.

Experts in Software Programming thought of combining the Data and


Operations. Therefore, the birth of Object Oriented Programming which is
commonly called OOPS.

The same code in Object Oriented Programming languages will have same
data and some action performed on that data.
Class Account{
int account_number;
int account_balance;
public void showdata(){
system.out.println(“Account Number”+account_number)
System.out.println(“Account Balance”+ account_balance)
}
}
13 | P a g e

By combining data and action, we will get many advantages over


structural programming viz,

• Abstraction
• Encapsulation
• Inheritance
• Polymorphism

You might also like