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

Module1 2

The document discusses programming concepts and Java fundamentals. It covers what programming is, how computers understand instructions, different types of programming languages like low-level and high-level languages. It also discusses how compilers and interpreters are used to execute programs written in high-level languages. The document then talks about installing Java Development Kit and steps to write a basic Java program.

Uploaded by

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

Module1 2

The document discusses programming concepts and Java fundamentals. It covers what programming is, how computers understand instructions, different types of programming languages like low-level and high-level languages. It also discusses how compilers and interpreters are used to execute programs written in high-level languages. The document then talks about installing Java Development Kit and steps to write a basic Java program.

Uploaded by

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

Java Fundamentals

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.

How does a person tell a computer to perform a task?


 All high-level programming languages are like natural human languages, which makes them
easier for humans to read and write but impossible for computers to understand without the
help of a translator.
 Computers understand instructions only in binary format, which is a sequence of 0s and 1s.
 �e sequence of 0s and 1s, which all computers understand, is called machine language or
machine code.
 Sometimes machine code is referred to as native code as it is native to the machine for which
it is written.
 Programs written in machine language are very difficult, if not impossible, to write, read,
understand, and modify.

Two types of programming languages


 Low-level programming languages (eg. Assembly language)
 High-level programming languages (eg. C,C++,Java, Python, C#)

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

 In a compiled language, the target machine directly translates the program.


 In an interpreted language, the source code is not directly translated by the target machine.
Instead, a different program, the interpreter, reads and executes the code.
 An interpreter does not translate the whole program into machine language at once. Rather, it
reads one instruction written in a high-level programming language at a time, translates it into
machine language, and executes it.
 Sometimes a combination of a compiler and an interpreter may be used to compile and run a
program written in a high-level language.
 For example, a program written in Java is compiled into an intermediate language called
bytecode.
 An interpreter, specifically called a Java Virtual Machine (JVM) for the Java platform, is used
to interpret the bytecode and execute it.
 An interpreted program runs slower than a compiled program.
 Most of the JVMs today use just-in-time compilers (JIT), which compile the entire Java
program into machine language as needed.
 Sometimes another kind of compiler, which is called an ahead-of-time (AOT) compiler, is
used to compile a program in an intermediate language (e.g. Java bytecode) to machine
language.

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

Step 3: Check the Directory

Step 4: Update the Environment Variables


 We will need to update our system’s Environment variables with our installed JDK bin path to
run the Java programs because while executing the programs, the command prompt will look
for the complete JDK bin path.
 The PATH variable in our system provides the exact location of executables that will be used
for running Java programs, such as javac and java. The CLASSPATH variable provides us
with the library files location.
 If we do not set the PATH variable, we will specify the full path to the JDK bin every time we
run a program.
 For example: C:\> “C:\Program Files\Java\jdk-11.0\bin\javac” TestClass.java
 So to set these variables, first right-click on My PC and select Properties.

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.

 Click on the OK button to apply the changes.


Java Fundamentals

Step 5: Verify the Java Installation

 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.

Java Programming Language


 Java is a language for writing computer programs.
 A Java program, which is written using the Java programming language, is a set of
instructions to be executed by a computer to perform a task.
 Java is a high-level, class-based, object-oriented programming language and software
platform that runs on billions of devices, including notebook computers, mobile devices,
gaming consoles, medical devices and many others.
 The rules and syntax of Java are based on the C and C++ languages.
 Due to its platform independence, object-oriented approach to development, and the main
distinctive feature, the write-once, run anywhere promise, Java has become the most
popular programming language in the world.

How Java works


Java Fundamentals

DTTH
9
Java Fundamentals

How to Create, Compile and Execute a Java program


Step 1 : Creation of a Java program
 Write a Java program on any editor or IDE (NetBean, Eclipse, JBuilder,etc).
 After creating a Java program you should provide .java extension to file.
 It holds one class definition.
 The class represents a piece of your program; even a very tiny application might need just a
single class.
 There may be only one public class per source code file and the class must go within a pair of
curly braces.
Here is the sample Java file (Welcome.java) to be complied into bytecode.

public class Welcome{


public static void main(String[] args){
System.out.println("Hello Wrold");
}
}
Step 2 : Compiling a Java Program
 Compiling is the process of translating the source code into a special binary format called
bytecode.
 This is accomplished using a program (usually called a compiler) javac, which comes with
JDK from Oracle Corporation.
 We use command such as javac, which is the Java compiler.
 After opening command prompt or shell console we compile Java program with .java
extension as javac Welcome.java.
 After executing the javac command, if no errors occur in our program we get a .class file
which contains the bytecode. It means that the compiler has successfully converted Java
source code to bytecode.

 Generally bytecode is been used by the JVM to run a particular application.

Step 3: Running the Compiled Code


 A Java program is run by a JVM.
 A JVM is invoked using a program called java, which is the java.exe file installed on your
machine along with the JDK.
 As Java compiler is invoked by javac command, the JVM is been invoked by java command.
 On the console window you have to type : java Welcome
 This command will invoke the JVM to take further steps for execution of Java program.
Java Fundamentals

Writing a Java program


Writing a Java program involves three steps:
 Writing the source code
 Compiling the source code
 Running the compiled code

Writing the Source Code


Java source code consists of three parts
 Zero or one package declaration
 Zero, one, or more import declarations
 Zero, one, or more type declarations: class, interface, or enum declarations
All three parts are optional. �e three parts, if present, must be specified in the above-mentioned
order. �e parts of the java program are following:

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;

Sequence of Java source code


A source file's elements must appear in this order:
1. Package declaration
2. Import statements
3. Class definitions

source file
class

method • Put a class in a source


file.
statement
• Put method in a 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

Java main() method


 The main() is the starting point for JVM to start execution of a Java program.
 Without the main() method, JVM will not execute the program.
 The syntax of the main() method is:

A well-structured Java Program

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"); // This is a comment

Java Multi-line Comments


 Multi-line comments start with /* and ends with */.
 Any text between /* and */ will be ignored by Java.
 This example uses a multi-line comment (a comment block) to explain the code:
/* This is a multi-line comment.
It can span more than one line. */

System.out.println("Hello World");

Java - Documentation Comments


 /** documentation */
 This is a documentation comment and in general its called doc comment.
 The JDK javadoc tool uses doc comments when preparing automatically generated
documentation.
 Javadoc is a tool which comes with JDK and it is used for generating Java code
documentation in HTML format from Java source code, which requires documentation in a
predefined format.
How to generate documentation
 To generate documentation, type one of the followings in command prompt according to your
doc comments.
>javadoc filename.java
>javadoc –package filename.java
>javadoc –d foldername –package filename.java
>javadoc –d foldername –author –version –package filename.java

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 {

public static void main(String[] args) {


// Prints Hello, World! on standard output.
System.out.println("Hello World!");
}
}

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 {

public static void main(String[] args) {


// Prints Hello, World! on standard output.
System.out.println("Hello World!");
}
}

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

* show the usage of various javadoc Tags.


* @param numA This is the first paramter to addNum method
* @param numB This is the second parameter to addNum method
* @return int This returns sum of numA and numB.
*/
public int addNum(int numA, int numB) {
return numA + numB;
}

/**
* This is the main method which makes use of addNum method.
* @param args Unused.
* @exception IOException On input error.
* @see IOException
*/

public static void main(String args[]) throws IOException {


AddNum obj = new AddNum();
int sum = obj.addNum(10, 20);

System.out.println("Sum of 10 and 20 is :" + sum);


}
}

DTTH
17

You might also like