Module1 2
Module1 2
Module 1
Programming Concepts
What Is Programming?
Programming is the way of writing a sequence of instructions to tell a computer to perform a
specific task.
�e sequence of instructions for a computer is known as a program.
A set of well-defined notations is used to write a program. �e set of notations used to write a
program is called a programming language.
�e person who writes a program is called a programmer.
A programmer uses a programming language to write a program.
Low-level languages
0010010010 10010100000100110
0001000100 01010010001001010
�e above instructions are to add two numbers in machine language.
It is very difficult to write, read, and understand a program written in a machine language.
We needed to represent the instructions for computers in some notations that were easier to
write, read, and understand, so computer scientists came up with another language called an
assembly language.
An assembly language provides different notations to write instructions. It is little easier to
write, read, and understand than its predecessor, machine language.
A program written in an assembly language to add two numbers looks similar to the
following:
li $t1, 15
add $t0, $t1, 12
Assembly language is easier to write, read, and understand than machine code.
�e instructions that are written in an assembly language must be translated into machine
language before the computer can execute them.
A program that translates the instructions written in an assembly language into machine
language is called an assembler.
DTTH
1
Java Fundamentals
High-level languages
A program written in a high-level programming language is also called source code.
They are closer to the written languages that humans are familiar with.
The instructions to add two numbers can be written in a high-level programming
language, for example. Java looks similar to the following:
int x = 15 + 27;
A high-level language is easier and more intuitive to write, read, understand, and
modify than the programs written in machine and assembly languages.
So there’s a need for a way to translate a program written in a high-level language to
machine language.
The translation is accomplished by a compiler, an interpreter, or a combination of both.
A compiler is a program that translates programs written in a high-level programming
language into machine language.
Compiling a program means translating a program written in a high-level language into
machine language.
Sometimes, translating a program written in a high-level programming language into a
lower-level programming language, which is not necessarily the machine language.
The code that is generated by a compiler is called compiled code.
The compiled program is executed by the computer.
Another way to execute a program written in high-level programming language is to use
an interpreter.
Java Fundamentals
Compiler
A compiler is a computer program that reads a program written in a high-level language such
as FORTRAN, PL/I, COBOL, etc.
It can translate it into the same program in a low-level language including machine language.
�e compiler also finds out the various errors encountered during the compilation of a
program.
�e compiler converts high-level language into low-level language using various phases.
A character stream inputted by the customer goes through multiple stages of compilation
which at last will provide target language.
Interpreter
An interpreter is a program that executes the programming code directly instead of just
translating it into another format.
It translates and executes programming language statements one by one.
An interpreter takes less time to interpret a source program as distinguished by a compiler.
DTTH
3
Java Fundamentals
Installing JDK
Step by Step Installation Of JDK
Step 1: Download JDK from the Site
Step 2: Install the JDK exe File
Java Fundamentals
DTTH
5
Java Fundamentals
Click on New, and type PATH in the Variable Name, and enter the path of the bin of installed
JDK in the Variable Value field.
If we already have the PATH variable, we can edit it by adding it to the existing values.
Open the command prompt and enter the command “java –version”, and if it runs successfully,
Java has been successfully installed.
Now that we have seen the steps to install JDK, let the programming fun begin!
DTTH
7
Java Fundamentals
Module 2
Writing a Java Program
History of Java
Java is one of the most popular programming languages worldwide.
It was created by James Gosling and Patrick Naughton, employees of Sun Microsystems, with
support from Bill Joy, co-founder of Sun Microsystems.
Sun officially presented the Java language at SunWorld on May 23, 1995.
Then, in 2009, the Oracle company bought the Sun company, which explains why the
language now belongs to Oracle.
DTTH
9
Java Fundamentals
Package Declaration
�e general syntax for a package declaration is
package <your_package_name>;
For example, package com.jdojo.intro;
package intro;
package com.jdojo.intro.common;
A package name may consist of one or more parts. �e parts are separated by a dot (.).
However, if a package declaration appears in Java source code, it must contain a package
name, which must have at least one part.
You can have maximum of one package declaration in a Java source file.
Import Declarations
Import declarations in Java source code are optional. You may develop a Java application
without using even a single import declaration.
It saves you some typing and makes your code cleaner and easier to read.
In an import declaration, you tell the Java compiler that you may use one or more classes
from a particular package.
Unlike a package declaration, there is no restriction on the number of import declarations in the
source code. The following are two import declarations:
import com.jdojo.intro.Account;
import com.jdojo.util.*;
DTTH
11
Java Fundamentals
The standard Java library is distributed over a number of packages, including java.lang,
java.util, java.net, and so on.
A class can use all classes from its own package and all public classes from other packages.
You can access the public classes in another package in two ways.
The first is to add the full package name in front of every classname. For example:
java.util.Date today = new java.util.Date();
The simpler, and more common, approach is to use the import keyword.
Once you use import, you no longer have to give the classes their full names.
You can import a specific class or the whole package by placing import statements at the top
of your source files. For example, you can import all classes in the java.util package with the
statement:
import java.util.*;
For just a single class;
import java.util.Date;
source file
class
There can only be one public class per . java file, as public classes must have the same name
as the source file.
Java Fundamentals
DTTH
13
Java Fundamentals
Java Comments
Comments can be used to explain Java code, and to make it more readable.
It can also be used to prevent execution when testing alternative code.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
This example uses a single-line comment before a line of code:
// This is a comment
System.out.println("Hello World");
System.out.println("Hello World");
HTML Tag
Because javadoc is used to create HTML files, documentation comments can contain HTML
tags. For example, the documentation comment
/** Sorts integer array using <STRONG>MySort</STRONG> algorithm */
/**
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
* <p>
* Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*/
Useful javadoc Tags for Classes and Interfaces
@author - Adds an "Author:" entry that contains the specified name. javadoc does not output
authorship information unless the -author option command-line argument is specified.
Syntax : @author name-text
Java Fundamentals
@version – This tag allows you to tie into Source Code Control Systems, which will
automatically provide accurate versioning and date updates. javadoc does not output version
information unless the -version option command-line argument is specified.
Syntax : @version version-text
@see – extremely flexible; @see can also be used to preface character strings or it can be used
to preface other members in the same or other classes. Adds a "See Also" heading with a link
or text entry that points to reference.
Syntax : @see reference
@link – Inserts an in-line link with the visible text label that points to the documentation for
the specified package, class, or member name of a referenced class.
Syntax : {@link package.class#member label}
@since - Adds a "Since" heading with the specified since-text to the generated
documentation.
Syntax : @since release
Useful Tags for Constructors and Methods
@param - Adds the specified parameter and its description to the "Parameters:" section of the
current method. The doc comment for a method or constructor must contain one @param tag
for each parameter the method expects. The tag cannot be used in class, interface or field doc
comments.
Syntax : @param parameter-name description
@return - Inserts a "Returns:" section that contains the specified description. This tag should
appear in every doc comment for a method, unless the method returns void or is a constructor.
The tag must not appear in class, interface, or field doc comments.
Syntax : @return description
@exception - Adds a Throws subheading to the generated documentation, with the classname
and description text.
Syntax : @exception class-name description
@throws – The @exception and @throws tags are synonyms.
Syntax : @exception class-name description
Example
/**
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
*
* @author Amy
* @version 1.0
* @since 2022-06-15
*/
public class HelloWorld {
DTTH
15
Java Fundamentals
Example
/**
* <b>Hello, World!</b>
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
* <p>
* Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*
*
* @author Amy
* @version 1.0
* @since 2022-06-15
*/
public class HelloWorld {
Example
Following program uses few of the important tags available for documentation comments. You can
make use of other tags based on your requirements.
The documentation about the AddNum class will be produced in HTML file AddNum.html but at the
same time a master file with a name index.html will also be created.
import java.io.*;
/**
* <b>Add Two Numbers!</b>
* The AddNum program implements an application that
* simply adds two given integer numbers and Prints
* the output on the screen.
* <p>
* <b>Note:</b> Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*
* @author Amy
* @version 1.0
* @since 2022-06-15
*/
public class AddNum {
/**
* This method is used to add two integers. This is
* a the simplest form of a class method, just to
Java Fundamentals
/**
* This is the main method which makes use of addNum method.
* @param args Unused.
* @exception IOException On input error.
* @see IOException
*/
DTTH
17