Lecture 1 - Java Programming-Introduction.docx
Lecture 1 - Java Programming-Introduction.docx
Introduction
Topics
Java is a powerful and versatile programming language for developing software running on
mobile devices, desktop computers, and servers.
In 1995, renamed Java, it was redesigned for developing Web applications. After that it
becomes enormously popular. Its rapid rise and wide acceptance can be traced to its design
characteristics. Java is simple, object oriented, distributed, interpreted, robust, secure,
architecture neutral, portable, high performance, multithreaded, and dynamic.
Java initially became attractive because Java programs can be run from a Web browser. Such
programs are called applets
Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons,
and so on, to interact with users on the Web and process their requests. Applets make the Web
responsive, interactive, and fun to use.
Applets are embedded in an HTML file. HTML (Hypertext Markup Language) is a simple
scripting language for laying out documents, linking documents on the Internet, and bringing
images, sound, and video alive on the Web.
Java can be used to develop rich Internet applications. A rich Internet application (RIA) is a
Web application designed to deliver the same features and functions normally associated with
desktop applications.
Java is now very popular for developing applications on Web servers. These applications
process data, perform computations, and generate dynamic Web pages. Many commercial
Websites are developed using Java on the backend.
Java is a versatile programming language: it is used to develop applications for desktop
computers, servers, and small handheld devices. The software for Android cell phones is
developed using Java.
Java syntax is defined in the Java language specification, and the Java library is defined in the
Java API. The JDK is the software for developing and running Java programs. An IDE is an
integrated development environment for rapidly developing programs.
The Java language specification is a technical definition of the Java programming language’s
syntax and semantics. The application program interface (API), also known as library, contains
predefined classes and interfaces for developing Java programs.
Java is a full-fledged and powerful language that can be used in many ways. It comes in three
editions:
Java Standard Edition (Java SE) to develop client-side applications. The applications
can run standalone or as applets running from a Web browser.
Java Enterprise Edition (Java EE) to develop server-side applications, such as Java
servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).
Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell
phones.
This java program displays the message “Welcome to Java!” on the console. The word
“console” is a computer term that refers to the text entry and display device of a computer. The
program is shown in Listing 1.
LISTING 1 Welcome.java
Line 1 defines a class. Every Java program must have at least one class. Each class has a name.
By convention, class names start with an uppercase letter. In this example, the class name is
Welcome.
Line 2 defines the main method. The program is executed from the main method. A class may
contain several methods. The main method is the entry point where the program begins execution.
The main method in this program contains the System.out.println statement. This statement
displays the string Welcome to Java! on the console (line 4).
Note1: Every statement in Java ends with a semicolon (;), known as the statement terminator.
Note2: A pair of curly braces in a program forms a block that groups the program’s components. In
Java, each block begins with an opening brace ({) and ends with a closing brace (}).
Note3: Every class has a class block that groups the data and methods of the class. Similarly, every
method has a method block that groups the statements in the method.
Note3: Blocks can be nested, meaning that one block can be placed within another, as shown in the
following code.
Caution: Java source programs are case sensitive. It would be wrong, for example, to replace
main in the program with Main.
Table 1 below shows some special characters (e.g., { }, //, ;), They are almost used in every
program.
Syntax Errors: Like any programming language, Java has its own syntax, and it is compulsory
to write code that conforms to the syntax rules. If a program violates a rule for example, if the
semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled,
the Java compiler will report syntax errors.
4- Beginning to Write Java Program
Now, by writing a program i.e. with Java and like any other language, it is possible to solve
problems. Through these problems a beginner can learn elementary programming using
primitive data types, variables, constants, operators, expressions, and input and output.
Writing a program involves designing algorithms and translating algorithms into programming
instructions, or code.
An algorithm describes how a problem is solved by listing the actions that need to be taken and
the order of their execution. Algorithms can help the programmer plan a program before writing
it in a programming language. Algorithms can be described in natural languages or in pseudo
code (natural language mixed with some programming code).
The following example computes the area of circle. Hence, the program needs to read the radius
entered by the user from the keyboard. This raises two important issues:
Reading the radius.
Storing the radius in the program.
At first, the second point “storing” is considered and then implementing program by reading
input from keyboard will be learned.
public class ComputeArea
{
public static void main(String[ ] args)
{
double radius;
double area;
Note: The plus sign (+) in lines 13–14 is called a string concatenation operator. It combines
two strings into one. If a string is combined with a number, the number is converted into a string
and concatenated with the other string. Therefore, the plus signs (+) in lines 13–14 concatenate
strings into a longer string, which is then displayed in the output.
To perform console output, it just requires using the println method to display a primitive value
or a string to the console.
Console input is not directly supported in Java, but it is possible to use the Scanner class to
create an object to read input from System.in, as follows:
The whole line Scanner input = new Scanner(System.in) creates a Scanner object and
assigns its reference to the variable input. An object may invoke its methods.
To invoke a method on an object is to ask the object to perform a task. For example the
invoke of the nextDouble( ) method to read a double value as follows:
double radius = input.nextDouble( );
ComputeAreaWithConsoleInput.java
1 import java.util.Scanner; // Scanner is in the java.util package
2
3 public class ComputeAreaWithConsoleInput {
4 public static void main(String[] args) {
5 // Create a Scanner object
6 Scanner input = new Scanner(System.in);
7
8 // Prompt the user to enter a radius
9 System.out.print("Enter a number for radius: ");
10 double radius = input.nextDouble( );
11
12 // Compute area
13 double area = radius * radius * 3.14159;
14
15 // Display results
16 System.out.println("The area for the circle of radius " + radius + " is " + area);
17 }
18 }
Note :
The Scanner class is in the java.util package. It is imported in line 1. There are two types of
import statements: specific import and wildcard import. The specific import specifies a single
class in the import statement. For example, the following statement imports. Scanner from the
package java.util.
import java.util.Scanner;
The “wildcard import ” imports all the classes in a package by using the asterisk as the
wildcard. For example, the following statement imports all the classes from the package
java.util.
import java.uitl.*;
6- Identifiers
Identifiers are the names that identify the elements such as classes, methods, and variables in a
program. All identifiers must obey the following rules:
7- Named Constants
A named constant is an identifier that represents a permanent value. The value of a variable may
change during the execution of a program, but a named constant, or simply constant, represents
permanent data that never changes. The syntax for declaring constant is shown in the following:
A constant must be declared and initialized in the same statement. The word final is a Java
keyword for declaring a constant.
ComputeAreaWithConstant.java
1 import java.util.Scanner; // Scanner is in the java.util package
2
3 public class ComputeAreaWithConstant {
4 public static void main(String[] args) {
5 final double PI = 3.14159; // Declare a constant
6
7 // Create a Scanner object
8 Scanner input = new Scanner(System.in);
9
10 // Prompt the user to enter a radius
11 System.out.print("Enter a number for radius: ");
12 double radius = input.nextDouble( );
13 // Compute area
14 double area = radius * radius * PI;
15 // Display result
16 System.out.println("The area for the circle of radius " + radius + " is " + area);
17 }
18 }
Format specifiers begin with a percent sign (%) followed by a character that represents
the data type. For example, the format specifier %s is a placeholder for a string.
The format string in the previous code specifies that printf should output two strings,
each followed by a newline character.
Conditions in if statements can be formed by using the equality operators (== and !=) and
relational operators (>, <, >= and <=) summarized in the figure below. Both equality operators
have the same level of precedence, which is lower than that of the relational operators. The
equality operators associate from left to right. The relational operators all have the same level of
precedence and also associate from left to right.