Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

College of Information Engineering

Department of Computer f Networks Engineering &


Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

Lecture 1

Introduction to Java Programming

1.1 What is Java?

The Java programming language in a high-level language that can be characterized


by object-oriented, robust, distributed and understand. The Java language is
developed by Sun Microsystems. It is modeled after C++, the Java language was
designed to be small, simple, and portable across platforms and operating systems.
It used to develop large-scale enterprise applications, to enhance the functionality of
World Wide Web servers, to provide applications for consumer devices (such as cell
phones, pagers and personal digital assistants) and for many other purposes.

The Java technology will help the user to do the following:


• Get started quickly: Java is easy to learn, especially for programmers already
familiar with C or C++.
• Write less code: Comparisons of program metrics (class counts, method counts,
and so on) suggest that a program written in the Java programming language can be
four times smaller than the same program in C++.
• Write better code: The Java programming language encourages good coding
practices, and its garbage collection helps you avoid memory leaks. Its object
orientation, its JavaBeans component architecture, and its wide-ranging, easily
extendible API let you reuse other people's tested code and introduce fewer bugs.
• Develop programs more quickly: Your development time may be as much as
twice as fast versus writing the same program in C++. Why? You write fewer lines of
code and it is a simpler programming language than C++.
• Avoid platform dependencies: You can keep your program portable by avoiding
the use of libraries written in other languages.
1
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

• Write once, run anywhere: Because Java applications are compiled into machine-
independent byte codes, they run consistently on any Java platform.
• Distribute software more easily: You can upgrade applets easily from a central
server. Applets take advantage of the feature of allowing new classes to be loaded "on
the fly," without recompiling the entire program.

1.2 Types of Java Programs


There are two types of programs in Java (Applications and Applets). The program
can run on any machine that has the Java Virtual Machine (JVM) installed.

Java Application: Is a program (such as a word-processor program, a spreadsheet


program, a drawing program or an e-mail program) that normally is stored and
executed from the user's local computer.

Java applet: Is a small program that normally is stored on a remote computer that
users connect to via a World Wide Web browser. The remote computer is known as a
Web server. Applets are loaded from a remote computer into the browser, executed in
the browser and discarded when execution completes.

1.3 Rules for Naming the source Java File


There are three main rules:
a) Creating source code files with the java extension.
b) The source code file uses the name of the primary or the first class declaration.
c) The java compiler is case sensitive, and the filenames must match the primary
class name exactly.

2
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

1.4 The main concept of object-oriented programming in Java:


a) All Java programs define objects that have a behavior and a stat. everything
the object knows about its variables and methods describes its state. The
actions of an object can perform describe its behavior.
b) All Java programs use encapsulation and message passing, which allow you
to use a Java object as long as you know what values the object accepts.
c) All Java programs define one or more classes that group sets of related
methods and variables.
d) All java programs use inheritance, which enables you to create classes that
inherent the functionality of existing class.
e) All Java programs use access modifiers that control access to methods and
variables.

1.5 Integrated Development Environment (IDE)


IDE is a software application which helps the user to write and compile code easily
by providing features like text editing, debugging plugins etc. while providing
compilation by the click of one button. An IDE or programming editor is required to
utilize the Java class libraries and frameworks. To decide which IDE is suitable for
writing code depends on several things including the requirement of the projects
under development along with the team development process. Java has many IDEs
but three of the most popular ones are NetBeans, Eclipse and IntelliJ.

1.5.1 Netbeans
NetBeans is an open-source, free Java IDE with a modular architecture. It has a
multi-language editor, debugger, profiler, versioning control, and developer
collaboration. It supports the development of other languages like PHP and C++. It
integrates all the functionality of Java. NetBeans runs on all major operating systems
3
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

like Windows, Linux, and Mac OS. It is an easy to install IDE and provides skeleton
applications and templates with sample applications as well and let developers save
time by letting them start coding on a skeleton project. so that they don’t have to go
through the process of implementing modules and pre-written code themselves.

NetBeans is implemented with a modular architecture. Modules are groups of classes


that implement a specific feature so that the developer can use modules to make their
work easier. Users can also create new modules which can be provided to other users.
Plugin Manager provides tools to manage features and plugins provided from the
programming languages to other users. While Services window makes it easier for
the user to use databases and web services.

Moreover, it is originally a part of the Sun systems has now slipped into the hands of
Oracle. This platform works as a framework for the development of JAVA swing
desktop applications. No additional SDK is required for the NetBeans IDE in JAVA
SE which uses the plugins and platform based applications for developing.

4
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

1.5.1 Eclipse
Eclipse is an open-source and free IDE with a modular architecture. It is the most
popular Java IDE. It provides support for the development of many languages but it is
mostly focused on Java and C/C++ development. Eclipse is compatible to run on all
types of operating systems like Windows, Linux, and Mac OS. Eclipse also provides
the support for working on documentation and modeling using papyrus and provides
tools for implementation of UML, SysML, OCL etc.

Eclipse is known for its extended support for plugins. It provides the support for users
to make their own Plugin Development Environment or PDE to make plugins. It also
provides customizability, GUI building, reporting etc. Eclipse also has a Standard
Widget Toolkit or SWT, which is used to access and use GUI elements from the
operating system on which the program is being developed. Eclipse also provides
also used to provide android development tools but was ended in 2015.

5
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

The advantage using the Eclipse is it has a plenty of plugins which are customizable
and versatile. It works in the background compiling code and reports errors as they
come up. Eclipse comes with a lot of features of editing, browsing, refactoring and
debugging for Java server development.
Eclipse is whole of visual containers which offer a set of views of editors. Basically,
it does everything you want your IDE to be and integrates with most of the tools.
Developers choose Eclipse for its ease in use and open source platform. It can be a
little trouble for newbies to work with but steadily it becomes a great experience to
work with it.

6
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

1.5.3 Intellij
IntelliJ IDEA which is a part of JetBrains is available in both free source and a
commercial edition. Already, it has the Resharper plugin for Visual Studio in C#
development by JetBrains. IntelliJ supports a variety of languages such as Clojure,
Groovy, Scala, Java and many more; it also has features such as advanced
forecasting, code analysis, and smart code completion. It supports PHP, Ruby,
Python, SQL, and ActionScript. Recently, it has launched version 12 which comes
with the new Android UI for Android app development. It allows users to submit
more plugins by using its built-in Swing components and features several user-
written plugins for enterprise edition.

7
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

1.6 Netbean Vs Eclips Vs Intellij


In the end, all the IDEs are free and you cannot go wrong with any of them that are
provide great environments to develop in any language especially Java. Both have
their respective strengths and weaknesses and provide great tools in different areas.
Both are great at what they do and in the end, they will get the job done. The As a
beginner developer? Choose NetBeans. If you want to use customize plugins or just
want to use the most popular Java IDE? Choose Eclipse. The following table
summarized the finding for each one in short.

8
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

1.7 First Program in Java


A Java application is a computer program that executes when you use the java
command to launch the Java Virtual Machine (JVM). The first simple program that
displays a line of text. Below the program followed by a box that displays its output.
File name is (Welcome1.Java)

The basic understanding of the first program in Java:


-A class declaration for class Welcome1. Every Java program consists of at least one
class that the programmer define. The class keyword introduces a class declaration
and is immediately followed by the class name (Welcome1). Keywords (sometimes
called reserved words) are reserved for use by Java and are always spelled with all
lowercase letters.

- Filename for a public Class


A public class must be placed in a file that has a filename of the form
ClassName.java, so class Welcome1 is stored in the file Welcome1.java

9
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

-Class Names and Identifiers


By convention, class names begin with a capital letter and capitalize the first letter of
each word they include (e.g., SampleClassName). A class name is an identifier—a
series of characters consisting of letters, digits, underscores (_) and dollar signs ($)
that does not begin with a digit and does not contain spaces. Some valid identifiers
are Welcome1, $value,_value, m_inputField1 and button7. The name 7button is not a
valid identifier because it begins with a digit, and the name input field is not a valid
identifier because it contains a space. Normally, an identifier that does not begin with
a capital letter is not a class name Java is case sensitive—uppercase and lowercase
letters are distinct—so value and Value are different (but both valid) identifiers.

-Class Body
The first left brace {, begins the body of every class declaration. A corresponding
right brace , }, must end each class declaration.
- The parentheses after the identifier main indicate that it’s a program building block
called a method. Java class declarations normally contain one or more methods. For
a Java application, one of the methods must be called main; otherwise, the Java
Virtual Machine (JVM) will not execute the application. Methods perform tasks and
can return information when they complete their tasks.

-System.out
The system.out object—which is predefined for you—is known as the standard
output object. It allows a Java application to display information in the command
window from which it executes.

-System.out.println

11
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

Method System.out.println displays (or prints) a line of text in the command


window. The string in the parentheses is the argument to the method.
Finally, the basic understanding of how Welcome1 uses object-oriented programming
concepts, let's review the source code for Welcome1 line by line.
The first line of the application declares the class as Welcome1. Because the
declaration does not specify what class the application extends, by default Welcome1
extends the Object class. The open bracket signifies the beginning of the Welcome
class:
Class Welcome1 {
The next line declares many things about a method called main:
Public static void main(String[] args) {
The declaration of the previous items are define item by item as follows:
public: The modifier public states that the method is accessible to other class.
static: The modifier static states that the method is unchanging and implicitly final,
meaning the method cannot be overridden.
void: The modifier void states that the method does not return a value.
main(): The word main specifies that main method for the application. Arguments for
the method are placed between the open and close parentheses. The main()
method is required in Java applications to tell the interpreter where to start.
main, unlike main() C/C++, does not return a value. Java handles its
exceptions internally rather than sending them back to the system.
(string[] args): This specifies that the main method takes an argument that is an object
of string.
{: The open bracket signifies the beginning of the method.

The next statement calls the println method of the system class and prints the
sentence "welcome to java programming". As with other methods, the arguments to

11
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer f Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

pass to the method are declared between open and close parentheses. Each statement
in Java ends in semicolon:
System.out.println("welcome to java programming ");
The final two lines of welcome1 program are close brackets. The first close bracket
completes the block of code associated with the main() method. The second close
brackets completes the block of code associated with welcome class.

Exmple2:

Example3:
public class Main {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("to");
System.out.println("information");
System.out.print("engineering");
} // end method main
} // end class Main
Output:
Welcome
to
information
engineering

12
Lecturer: Dr. Lahieb Mohammed Al-Yassiry

You might also like