0% found this document useful (0 votes)
2 views31 pages

Introduction to Java Programming Basics

Uploaded by

onyes29reuel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Introduction to Java Programming Basics

Uploaded by

onyes29reuel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link].

ng

JAVA Programming
COS 202

(Module 1, Lecture 2)

Dr. Akputu Oryina Kingsley


Module 1, Lecture Note 2 2

Coverage
▪ Characteristics of the Java Programming Language: Why Java?
▪ Java Program anatomy, language terms or Compile simple programs using Disk Operating
System (DOS)
▪ Lab 1: Introduction to IDEs : Eclipse and NetBeans
▪ Life Cycle of a Java Application (Program)

Dr. Akputu Oryina Kingsley


Why Java Programming? (1/3) 3

▪ Java is a general-purpose programming language


o It is used in all industries for almost any type of application
▪ Java Is Simple
o No language is simple, but Java is a bit easier than the popular object-oriented
programming language C++, which was the dominant software-development
language before Java. Java is partially modeled on C++, but greatly simplified
and improved.

▪ Java is Object-Oriented
o Java is inherently object-oriented.
o Although many object-oriented languages began strictly as procedural
languages, Java was designed from the start to be object-oriented.

▪ Java Is Distributed
o Distributed computing involves several computers working together on a
network. Java is designed to make distributed computing easy.
o Since networking capability is inherently integrated into Java, writing network
programs is like sending and receiving data to and from a file.

Dr. Akputu Oryina Kingsley


Why Java Programming? (2/3) 4

▪ Java is Interpreted
o You need an interpreter to run Java programs. The programs are compiled into the Java
Virtual Machine code called byte code.
o The byte code is machine-independent and can run on any machine that has a Java
interpreter, which is part of the Java Virtual Machine (JVM).
▪ Java is Robust
o Robust means reliable. No programming language can ensure complete reliability. Java
puts a lot of emphasis on early checking for possible errors.
o Java compilers can detect many problems that would first show up at execution time in
other languages
▪ Java is Secure
o As an Internet programming language, Java is used in a networked and distributed
environment.
o If you download a Java applet (a special kind of program) and run it on your computer, it
will not damage your system because Java implements several security mechanisms to
protect your system against harm caused by stray programs. The security is based on the
premise that nothing should be trusted.

Dr. Akputu Oryina Kingsley


Why Java Programming? (3/3) 5

▪ Java Is Architecture-Neutral
o Java is interpreted. This feature enables Java to be architecture-neutral, or to
use an alternative term, platform-independent.
o With a Java Virtual Machine (JVM), you can write one program that will
run on any platform.
▪ Java Is Portable
oBecause Java is architecture neutral, Java programs are portable. They can
be run on any platform without being recompiled.
oMoreover, there are no platform-specific features in the Java language.

▪ Java Is Multithreaded
oMultithreading is a program’s capability to perform several tasks
simultaneously.
oFor example, downloading a video file while playing the video would be
considered multithreading.

Dr. Akputu Oryina Kingsley


Java Programming: History (1/2) 6

▪ The Java programming language was originally created in 1995 by James


Gosling from Sun Microsystems (currently a subsidiary of Oracle
Corporation).

▪ Currently Java is one of the most popular programming languages for


everything from programming:
o Games
o mission-critical applications such as those for controlling Mars rovers
or orbiting Nigeria’s SAT 1, 2 and 3 or trading on Wall Street or
[Link]
▪ The goal of Gosling was to provide a simpler and platform-independent
alternative to C++. You’ll see what platform independence means a little
later
▪ Now lets see some of the reasons why Java can be your OOP language
of choice
Java Programming: History (2/2) 7

Evolution of the Java Language:


▪ James Gosling and Sun Microsystems
▪ Oak
▪ Java, May 20, 1995, Sun World
▪ HotJava
o The first Java-enabled Web browser
▪ Early History Website:

[Link]

Dr. Akputu Oryina Kingsley


JDK and JRE 8

▪ Technically you can write the source code of your Java program in
any plain text editor that you prefer (Notepad, TextEdit, vi, etc.), but
to compile your program you’ll need additional tools and code
libraries that are included in the Java Development Kit (JDK).

▪ If you are planning to use a specific computer to develop Java


programs, you’ll need to download and install JDK.

▪ If you are planning to use this computer only to run Java programs
that were compiled somewhere else, you just need the Java
Runtime Environment (JRE).
▪ When you install the JDK on your machine, it includes JRE.

Dr. Akputu Oryina Kingsley


Get familiar with more terms in Java 9

▪ The student needs to get familiar with the variances in the two main
java versions : Java SE (Standard Edition) and Java EE
(Enterprise Edition).
o Java Standard Edition (J2SE)
o J2SE can be used to develop client-side standalone
applications or applets.
o Java Enterprise Edition (J2EE)
o J2EE can be used to develop server-side applications such as
Java servlets and Java Server Pages.
o Java Micro Edition (J2ME).
o J2ME can be used to develop applications for mobile devices
such as cell phones.
o In this course uses J2SE to introduce Java programming; in
addition we shall introduce ourselves to J2EE too.

Dr. Akputu Oryina Kingsley


10
Lab1: Write & Compiling Simple Java Programs in DOS

▪ Create a directory, c:\Year2\Pract1


▪ Write simple programs in Notepad and Compile in DOS
▪ Study structure of a typical Java Program
▪ Introduction to Eclipse IDE
▪ Obtain resources:
o Since you’ll be learning how to develop Java programs, download JDK from
the following website:
[Link]

o You must have installed JDK and set JDK’s bin directory in your environment
path (e.g., c:\Program Files\java\jdk1.6.0_14\bin in your environment path.
Install Eclipse IDE following the YouTube Video below or on slide 26:
[Link]

o Need help ?

Dr. Akputu Oryina Kingsley


Validation Check after Installation 11

▪ Senility check for JDK availability:


o On the Command prompt terminal type : javac –version
o You should see your directory address returned without syntax error as shown in the screen shot
below:

▪ Life Cycle of a typical Java Program

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
[Link] ("Welcome to Java!");
}
}

▪ If you use Notepad, select All Files in the Save as Type drop-down to avoid
autoattachment of the .txt suffix.

Dr. Akputu Oryina Kingsley


Compilation on DOS (Command Prompt) 12

o As home work, students should consults relevant resources, including the YouTube,
and attempt to run the programme in DOS environment.

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
[Link] ("Welcome to Java!");
}
}

▪ If you use notepad, select All Files in the Save as Type drop-down to
avoid autoattachment of the .txt suffix.

o Hint: the program class name must be saved with the .java
extension

Dr. Akputu Oryina Kingsley


Anatomy of a Java Program and language Terms 13

▪ Anatomy of a typical java program include some of the following parts or


language terms :
o Comments
o Reserved words
o Modifiers
o Statements
o Blocks
o Classes
o Methods
o The main method

▪ Comments
Three types of comments in Java:
Line comment: A line comment is preceded by two slashes (//) in a line.
Paragraph comment: A paragraph comment is enclosed between /* and */
in one or multiple lines.
javadoc comment: javadoc comments begin with /**and end with */. They are used
for documenting classes, data, and methods. They can be extracted into an HTML
file using JDK's javadoc command.

Dr. Akputu Oryina Kingsley


14
Reserved Words

▪ These are words or keywords that have a specific meaning to the


compiler and cannot be used for other purposes in the program.

▪ For example, when the compiler sees the word class, it understands that
the word after class is the name for the class. Other reserved words
include public, static, and void.

Dr. Akputu Oryina Kingsley


15

Modifiers

▪ Java uses certain reserved words called modifiers that specify the
properties of the data, methods, and classes and how they can be used.

▪ Examples of modifiers are public and static. Other modifiers are private,
final, abstract, and protected.

▪ A public datum, method, or class can be accessed by other programs. A


private datum or method cannot be accessed by other programs.

Dr. Akputu Oryina Kingsley


16
Statements

▪ A statement represents an action or a sequence of actions.

The statement :

[Link] ("Welcome to Java!")

in the program Welcome to Java is a statement to display the


greeting "Welcome to Java!"

▪ Every statement in Java ends with a semicolon (;).

Dr. Akputu Oryina Kingsley


Blocks 17

▪ A pair of braces in a program forms a block that groups


components of a program.

public class Test {


public static void main(String[] args) { Class block

[Link]("Welcome to Java!"); Method block


}
}

Dr. Akputu Oryina Kingsley


18
Classes

▪ The class is the essential Java construct.


▪ It is a template or blueprint for objects.
▪ To program in Java, you must understand classes and be able to
write and use them.
▪ The mystery of the class will continue to be unveiled throughout this
course.
▪ For now, though, understand that a program is defined by using one
or more classes.

Dr. Akputu Oryina Kingsley


19

Methods
▪ What is [Link]?
▪ It is a method: a collection of statements that performs a sequence of
operations to display a message on the console.
▪ It can be used even without fully understanding the details of how it
works.
▪ It is used by invoking a statement with a string argument. The string
argument is enclosed within parentheses.
▪ In this case, the argument is "Welcome to Java!" You can call the same
println method with a different argument to print a different message.

//This program prints Welcome to Java! This is a method


public class Welcome {
public static void main(String[] args) {
[Link]("Welcome to Java!");
}
}

Dr. Akputu Oryina Kingsley


20

main Method
▪ The main method provides the control of program flow. The Java
interpreter executes the application by invoking the main method.

▪ The main method looks like this:

public static void main(String[] args) {


// Statements;
}

//This program prints Welcome to Java! Enter main method


public class Welcome {
public static void main(String[] args) {
[Link]("Welcome to Java!");
}
}

Dr. Akputu Oryina Kingsley


21
Lab 1 (Cont…)

▪ Keep in mind that Java is a case-sensitive language, which means that if you named the program,
‘Welcome’ with a lower case ‘w’, you should not try to start the program as ‘Welcome’.

Note: Your first dozen syntax errors will be caused by improper capitalization.

▪ Lets have a really short explanation of some of the terms and language elements used in the
Welcome program.

▪ First this program has a class: Welcome. As a practice, give the Java class and its file the same
name.

Dr. Akputu Oryina Kingsley


22

❑ Life Cycle of A Java Program


▪ Java requires the source code of your program to be compiled first.
▪ The source code gets converted to either machine-specific code or a bytecode that is understood
by some run-time engine or a virtual machine.
▪ Not only will the program be checked for syntax errors by a Java compiler, but some other libraries
of Java code can be added (linked) to your program after the compilation is complete (deployment
stage).
LAB Session 1((Lesson 1) )
❑ Setting up Basic Text editors for program coding
▪ In this Lab session we will write a very basic Java program that will output the words “HelloWorld”
on your computer’s screen.
▪ Technically you can write the source code of your Java program in any plain text editor that you
prefer (Notepad, Notepad++, TextEdit, vi, etc.)

❑ JDK and JRE


▪JDK should be installed if you are using a particular machine to develop Java Programs
▪ If you will be only be using a computer to run program compiled elsewhere, JRE could be installed.
▪Notably JDK comes with JRE.

Dr. Akputu Oryina Kingsley


Process Explanation for the Welcome 23

program
Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
[Link]("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac [Link]
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main([Link][])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result


24
Explanation of Some of the Terms and Language Elements in
the Welcome program

▪ The program contains the class Welcome which contains a method,


main(). Methods in Java classes represent functions (actions) that a
class could perform.
▪ A Java class may have several methods. But if one of them is called
main() and is declared (if it has a method signature), as in our class, this
makes this Java class executable.
o As a practice give the Java class and its file the same name.
o There could be exceptions to this rule, but not in this simple program
▪ If a class doesn’t have a method main(), it can be used from other
classes, but you can’t run it as a program.
▪ The method main() calls the method println() to display the text “Hello
World” on the screen.
▪ The method signature (similar to a title) of the method main():
public static void main (String[] args)

Dr. Akputu Oryina Kingsley


25

Explanation of Some of the Terms… cont..

▪ The method signature includes the access level (public), instructions on usage
(static), return value type (void), name of the method (main), and argument list
(String[] args).
▪ The Key words:
▪ public : means the method main() can be accessed by any other Java class.
▪ static : means that you don’t have to create an instance of this class to use this
method.
▪ Void: means that the method main() doesn’t return any value to the calling
program.
▪ String[] args : tells us that this method will receive an array of characters as the
argument (you can pass external data to this method from a command line).
▪ The body of the main method contain the following line:
[Link](“Welcome to Java”)

Dr. Akputu Oryina Kingsley


26

Explanation of Some of the Terms… cont..


▪ The body of the main method contain the following line:
[Link](“Welcome to Java”)
o The preceding println() method is used to print data on the system console
(command window).
o Java’s method names are always followed by parentheses.
▪ System here represents another Java class.
▪ The dot notation [Link] means that the variable out is defined
inside the class System.

▪ [Link]() tells you that there is an object represented by a variable


called out and it has a method called println().

Dr. Akputu Oryina Kingsley


Exercise 27

▪ Watch the following YouTube for the Eclipse IDE


installation and configuration:
[Link]

Dr. Akputu Oryina Kingsley


28

LAB Session 2
❑ Introduction to Eclipse IDE

▪ Compiling and Running Programs Using IDEs

▪ While our first Java program was written in a plain text editor and compiled from a command
window, this is not a productive way of developing software.

▪ Professional programmers use one of the Integrated Development Environment (IDEs), which
include an editor, a compiler, type-ahead help, a debugger, and a lot more.

▪ Popular Java IDEs


▪ NetBeans Open Source by Sun
▪ Intellij IDEA (JetBrains)
▪ Eclipse Open Source by IBM

▪ Eclipse is the most widely used IDE justifying our choice of it for developments.

Dr. Akputu Oryina Kingsley


29
LAB 2 (cont…)

The Eclipse IDE

▪ During installation selected the following workspace directory: c:\PracticalJava\workspace.

Dr. Akputu Oryina Kingsley


30

Lab 2 (cont…)

▪ To be precise, you are looking at Java EE perspective (note the Java EE tab at the top), which is a
collection of views.
▪ On the left you see a Project Explorer view.
▪ The area in the middle is reserved for the code editor view — you start using it as soon as you
create your first Java class.
▪ The Outline view is on the right — you’ll see the names of your classes, methods, and variables
▪ Since we are just starting there is no need to work in the Java EE perspective. We can get by in the
Java perspective.

▪ Click the little icon with the plus sign by the Java EE tab and select “Java perspective.

Creating the first program ‘Welcome’ in Eclipse

▪ In Lab Lesson 1 we simply created the class Welcome, but in Eclipse you have to create the
project first.
▪ Select File ➪ New ➪ Java Project and enter Welcome as the name of the project in the pop-up
window.
▪ Next window will ask us to specify the folder where compiled Java classes of the Welcome project
should be created.

Dr. Akputu Oryina Kingsley


31
LAB 1 (cont…)
Note:
▪ By default, Eclipse creates the HelloWorld folder for this project with a bin sub-folder for
compiled classes and an src sub-folder for the source code.

▪ Recall that in Lab session 1, using plain text editor, both [Link] and [Link] were
sitting inthe same folder, which is a bad practice.

▪ This project has an empty folder, src-you can save the source code of [Link]
there when ready.

▪ The JRE folder contains all required libraries supporting the JVM where Welcome will run.

▪ These library files have .jar in their names. Java SDK comes with a jar utility that allows you to
create a file archive that contains one or more compiled classes.

▪ While the JRE folder contains classes created by developers of the JRE itself, most real-world
applications consist of groups of files (packages) located in one or more jars.

End of Module 1Lecture Note 2

Dr. Akputu Oryina Kingsley

You might also like