Beruflich Dokumente
Kultur Dokumente
ON
“JAVA”
TAKEN AT:
SESSION: 2019-2020
ALWAR
I hereby declare that the work, which is being presented in Practical Training, entitled “ Java”
in partial fulfillment for the award of Degree of “Diploma in Electronics” and submitted to the
Department of Electronics Engineering, Government Women Polytechnic College, Ajmer is
a record of my own investigations carried under the Guidance of Shri Alok Mathur, Assistant
Professor, Centre for Electronic Governance, Jaipur .
I have not submitted the matter presented in this report anywhere for the award of any other
work.
Hitesh yadav
Roll No. :
Government Polytechnic College, neemrana
ACKNOWLEDGMENET
I would like to express my sincere gratitude and deep debt to our Head of Department Mr.
mukesh gothwal..and our training in-charge Ms. Priyanka nehra for their encouragement and
valueable advice.
I pay thank to Mr. ALOK MATHUR who has given guidance and a light to me during this
practical training. His versatile knowledge about “JAVA” has eased me in the critical times
during the span of this practical training Seminar.
I take this opportunity to express my gratitude to all those people who have been directly and
indirectly with me during the competition of this practical training seminar.
I acknowledge here out debt to those who contributed significantly to one or more steps. I take
full responsibility for any remaining sins of omission and commission.
Hitesh yadav
DIPLOMA IN COMPUTER SCIENCE
COMPANY PROFILE
Centre for Electronic Governance is an autonomous body of Government of Rajasthan under the
Department of Technical Education.
CEG get recognition in IT Training programs like Big data, Hadoop, AWS Cloud Computing,
CCNA Trainings, Android App Development, Java, ASP.NET Training, ASP.NET MVC 5
using Razor, Entity Framework, WCF, PHP, Auto CAD, Red Hat Certification training, C, C++,
Linux Training program, Embedded Training programs etc. in Jaipur.CEG is authorized
education partner of CISCO, Red HAT, Microsoft, Autodesk.
Training programs run at CEG are approved by Department of Technical Education, Govt. of
Rajasthan . CEG is also working as study Centre of IGNOU for MCA, BCA, CIT, PGDIS and
ACISE programs. CEG started software development and Live Project training to give exposure
to real-time applications to students.
CEG also Conduct Summer Training programs for M.Sc., B.Tech, M.Tech and for Diploma
students. 3D Printing & Robotic Lab has established as per Govt. of Rajasthan instruction.
Centre for Electronics Governance is working as Central Placement Cell (CPC) as a Nodal
Agency on behalf of Department of Technical Education Govt. of Rajasthan. State Level Central
Placement Cell works for placement of Engineering, Diploma and Non Technical students of
Rajasthan.
CEG organized State level centralized admission of B.Tech./B.E., B.Arch. MBA, MCA and
BHMCT
INDEX
1. Introduction
4. Inheritance
5. Exception Handling
6. Multithreading in Java
7. Applet
8. JDBC
9. Database
11. Conclusion
12. References
Chapter 1
INTERODUCTION
Java was originally developed by James Gosling at Sun Microsystems (which has since been
acquired by Oracle Corporation) and released in 1995 as a core component of Sun
Microsystems’ Java platform.
Secure
The Java platform provides various features for security of Java applications. Some of the high-
level features that Java handles are:
- provides secure platform for developing and running applications
- automatic memory management, reduces memory corruption and vulnerabilities
- provides secure communication by protecting the integrity and privacy of data transmitted
Portable
For programs to be dynamically downloaded to all the various types of platforms connected to
the Internet, some means of generating portable executable code is needed .As you will see, the
same mechanism that helps ensure security also helps create portability. Indeed, Java’s solution
to these two problems is both elegant and efficient.
Object-Oriented
Java was not designed to be source-code compatible with any other language. This allowed the
Java team the freedom to design with a blank slate. One outcome of this was a clean usable,
pragmatic approach to objects. The object model in Java is simple and easy to extend, while
simple types, such as integers, are kept as high-performance non-objects.
Robust
The multi-platform environment of the Web places extraordinary demands on a program,
because the program must execute reliably in a variety of systems. The ability to create robust
programs was given a high priority in the design of Java. Java is strictly typed language; it
checks your code at compile time and run time.
Java virtually eliminates the problems of memory management and de-allocation, which is
completely automatic. In a well-written Java program, all run time errors can –and should –be
managed by your program.
Multithreaded
Java was designed to meet the real-world requirement of creating interactive, networked
programs. To accomplish this, Java supports multithreaded programming, which allows you to
write programs that do many things simultaneously. The Java run-time system comes with an
elegant yet sophisticated solution for multiprocess synchronization that enables you to construct
smoothly running interactive systems. Java's easy-to-use approach to multithreading allows you
to think about the specific behavior of your program, not the multitasking subsystem.
1.3 Byte Code
The key that allows the Java to solve the security and portability problems is that the output of
Java compiler is Byte code. Byte code is a highly optimized set of instructions designed to be
executed by the Java run-time system, which is called the Java Virtual Machine (JVM). That is,
in its standard form, the JVM is an interpreter for byte code.
Translating a Java program into byte code helps makes it much easier to run a program in a wide
variety of environments. The reason is, once the run-time package exists for a given system, any
Java program can run on it.
Java programming uses to produce byte codes and executes them. The first box indicates that the
Java source code is located in a. Java file that is processed with a Java compiler called javac. The
Java compiler produces a file called a. class file, which contains the byte code. The .Class file is
then loaded across the network or loaded locally on your machine into the execution
environment is the Java virtual machine, which interprets and executes the byte code.
1.5 Java Architecture
Java architecture provides a portable, robust, high performing environment for development.
Java provides portability by compiling the byte codes for the Java Virtual Machine, which is then
interpreted on each platform by the run-time environment. Java is a dynamic system, able to load
code when needed from a machine in the same room or across the planet.
Compilation of code
When you compile the code, the Java compiler creates machine code (called byte code) for a
hypothetical machine called Java Virtual Machine (JVM). The JVM is supposed to execute the
byte code. The JVM is created for overcoming the issue of portability. The code is written and
compiled for one machine and interpreted on all machines. This machine is called Java Virtual
Machine.
Java EE, Java ME and Java SE - Java EE, Java ME and Java SE stands for Java Platform
Enterprise Edition, Micro Edition, and standard edition respectively.
Java EE is targeted for applications which run on servers. Java ME is targeted for resource-
limited devices like embedded devices. And, Java SE is the basic Java environment used for
creating standard programs.
JVM - JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a
Java program.
JRE - JRE (Java Runtime Environment) contains JVM, supporting libraries, and other
components to run a Java program. However, it doesn't contain any compiler and debugger.
JDK - JDK (Java Development Kit) contains JRE and tools such as compilers and debuggers for
developing Java applications .
Chapter 2
HARDWARE & SOFTWARE REQUIREMENT
Operating System:
Hardware:
SOFTWARE:
Editor : Notepad, NetBeans or other editor
Software : JDK 1.8 or more
Database : MySQL or other
Chapter 3
JAVA OOP CONCEPTS
Encapsulation. This is the practice of keeping fields within a class private, then
providing access to them via public methods. It’s a protective barrier that keeps the data
and code safe within the class itself. This way, we can re-use objects like code
components or variables without allowing open access to the data system-wide.
Polymorphism. This Java OOP concept lets programmers use the same word to mean
different things in different contexts. One form of polymorphism in Java is method
overloading. That’s when different meanings are implied by the code itself. The other
form is method overriding. That’s when the different meanings are implied by the
values of the supplied variables.
3.2 Access Modifiers in Java
As the name suggests access modifiers in Java helps to restrict the scope of a class,
constructor , variable , method or data member. There are four types of access modifiers
available in java:
1. Default – No keyword required
2. Private
3. Protected
4. Public
1. Default: When no access modifier is specified for a class, method or data member – It
is said to be having the default access modifier by default. The data members, class
or methods which are not declared using any access modifiers i.e. having default
access modifier are accessible only within the same package.
2. Private: The private access modifier is specified using the keyword private. The
methods or data members declared as private are accessible only within the class in
which they are declared. Any other class of same package will not be able to access
these members. Classes or interface can not be declared as private.
3. Protected: The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within same
package or sub classes in different package.
4. Public: The public access modifier is specified using the keyword public. The public
access modifier has the widest scope among all other access modifiers. Classes,
methods or data members which are declared as public are accessible from every
where in the program. There is no restriction on the scope of a public data members.
Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you as well as other programmers to understand
the behavior of the method because its name differs.
Method overloading increases the readability of the program.
Types of Inheritance
There are Various types of inheritance in Java:
1. Single Inheritance:
In Single Inheritance one class extends another class (one class only).
In above diagram, Class B extends only Class A. Class A is a super class and Class B
is a Sub-class.
2. Multiple Inheritance:
In Multiple Inheritance, one class extending more than one class. Java does not
support multiple inheritance.
As per above diagram, Class C extends Class A and Class B both.
3. Multilevel Inheritance:
In Multilevel Inheritance, one class can inherit from a derived class. Hence, the
derived class becomes the base class for the new class.
4. Hierarchical Inheritance:
In Hierarchical Inheritance, one class is inherited by many sub classes.
As per above example, Class B, C, and D inherit the same class A.
5. Hybrid Inheritance:
Hybrid inheritance is a combination of Single and Multiple inheritance.
As per above example, all the public and protected members of Class A are inherited
into Class D, first via Class B and secondly via Class C.
4.2 Encapsulation
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates.Other way to think about encapsulation
is, it is a protective shield that prevents the data from being accessed by the code outside this
shield.
Technically in encapsulation, the variables or data of a class is hidden from any other
class and can be accessed only through any member function of own class in which they
are declared.
As in encapsulation, the data in a class is hidden from other classes, so it is also known
as data-hiding.
Encapsulation can be achieved by: Declaring all the variables in the class as private and
writing public methods in the class to set and get the values of variables.
Advantages of Encapsulation:
Data Hiding: The user will have no idea about the inner implementation of the class. It will
not be visible to the user that how the class is storing values in the variables. He only knows
that we are passing the values to a setter method and variables are getting initialized with that
value.
Increased Flexibility: We can make the variables of the class as read-only or write-only
depending on our requirement. If we wish to make the variables as read-only then we have to
omit the setter methods like setName(), setAge() etc. from the above program or if we wish to
make the variables as write-only then we have to omit the get methods like getName(),
getAge() etc. from the above program
Reusability: Encapsulation also improves the re-usability and easy to change with new
requirements.
Chapter 5
Exception Handling
Java being an object oriented programming language, whenever an error occurs while
executing a statement, creates an exception object and then the normal flow of the program
halts and JRE tries to find someone that can handle the raised exception. The exception object
contains a lot of debugging information such as method hierarchy, line number where the
exception occurred, type of exception etc. When the exception occurs in a method, the
process of creating the exception object and handing it over to runtime environment is
called “throwing the exception”.
Once runtime receives the exception object, it tries to find the handler for the exception.
Exception Handler is the block of code that can process the exception object. If appropriate
exception handler is found, exception object is passed to the handler to process it. The
handler is said to be “catching the exception”. If there are no appropriate exception handler
found then program terminates printing information about the exception.
Exception is an event that interrupts the normal flow of execution. It is a disruption during
the execution of the Java program.
There are two types of errors:
1. Compile time errors
2. Runtime errors
Compile time errors can be again classified again into two types:
Syntax Errors
Semantic Errors
5.2 Java Exception Handling Keywords
1. try-catch – We use try-catch block for exception handling in our code. try is the start of
the block and catch is at the end of try block to handle the exceptions. We can have
multiple catch blocks with a try and try-catch block can be nested also. catch block
requires a parameter that should be of type Exception.
2. throw – We know that if any exception occurs, an exception object is getting created
and then Java runtime starts processing to handle them. Sometime we might want to
generate exception explicitly in our code, for example in a user authentication program
we should throw exception to client if the password is null. throw keyword is used to
throw exception to the runtime to handle it.
3. throws – When we are throwing any exception in a method and not handling it, then
we need to use throws keyword in method signature to let caller program know the
exceptions that might be thrown by the method. The caller method might handle these
exceptions or propagate it to it’s caller method using throws keyword. We can provide
multiple exceptions in the throws clause and it can be used with main() method also.
4. finally – finally block is optional and can be used only with try-catch block. Since
exception halts the process of execution, we might have some resources open that will
not get closed, so we can use finally block. finally block gets executed always, whether
exception occurred or not.
try{
statement(s)
Example
class JavaException {
int d = 0;
int n = 20;
try {
int fraction = n / d;
} catch (ArithmeticException e) {
System.out.println("End Of Main");
}
Chapter 6
Multithreading in Java
6.3 Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to
utilize the CPU. Multitasking can be achieved by two ways:
o Process-based Multitasking(Multiprocessing)
o Thread-based Multitasking(Multithreading)
As shown in the above figure, thread is executed inside the process. There is context-
switching between the threads. There can be multiple processes inside the OS and one
process can have multiple threads.
6.5 Life Cycle of Thread
Chapter 7
Applet in Java
An applet is a Java class that extends the java.applet.Applet class. A main() method is not
invoked on an applet, and an applet class will not define main(). Applets are designed to be
embedded within an HTML page.When a user views an HTML page that contains an applet,
the code for the applet is downloaded to the user's machine. A JVM is required to view an
applet. The JVM can be either a plug-in of the Web browser or a separate runtime
environment. The JVM on the user's machine creates an instance of the applet class and
invokes various methods during the applet's lifetime. Applets have strict security rules that
are enforced by the Web browser. The security of an applet is often referred to as sandbox
security, comparing the applet to a child playing in a sandbox with various rules that must be
followed.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
JDBC is a Java API for executing SQL statements. JDBC is standing for Java Database
Connectivity. It consists of a set of classes and interfaces written in the Java programming
language. JDBC provides a standard API for tool/database developers and makes it possible
to write database applications using a pure Java API.
Using JDBC, it is easy to send SQL statements to virtually any relational database. One can
write a single program using the JDBC API, and the program will be able to send SQL
statements to the appropriate database.
2. Loading the driver loading the driver is done using following method:
Class. for Name(“sun. Jdbc.odbc.JdbcOdbc Driver ”);
The driver specified is available in JDK 5.0 10
5.Execute Query : The SQL query that is to be sent to the database is executed by calling the
following method on statement object ,which returns a ResultsSet object:
ResultSet reset = stmnt.exicute Query (“select * from database”);
6.Retrive the result:The result return by the database to the Query executed
are extract from ResultSet using the method defined in ResultSet interface.
While(Reset.next())
System.out.println (“Name: ”+reset.getString (“name”));
7. Close the statements and connection using the close method, the statement and
connection are closed:
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sweta","root","");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
. ResultSet rs=stmt.executeQuery("select * from emp");
. while(rs.next())
. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
. }catch(Exception e){ System.out.println(e);}
. }
. }
Chapter 9
Database
A database management system (DBMS) is computer software designed for the purpose of
managing databases, a large set of structured data, and run operations on the data requested
by numerous users. Typical examples of DBMSs include Oracle, DB2, Microsoft Access,
Microsoft SQL Server, MySQL, SQLite. DBMSs are typically used by Database
administrators in the creation of Database systems.
9.1 MySQL
Originally conceived by the Swedish company MySQL AB, MySQL was acquired by Sun
Microsystems in 2008 and then by Oracle when it bought Sun in 2010. Developers can use
MySQL under the GNU General Public License (GPL), but enterprises must obtain a
commercial license from Oracle.
Today, MySQL is the RDBMS behind many of the top websites in the world and countless
corporate and consumer-facing web-based applications, including Facebook, Twitter and
YouTube.
MySQL is based on a client-server model. The core of MySQL is MySQL server, which
handles all of the database instructions (or commands). MySQL server is available as a
separate program for use in a client-server networked environment and as a library that can
be embedded (or linked) into seperate applications.
MySQL operates along with several utility programs which support the administration of
MySQL databases. Commands are sent to MySQLServer via the MySQL client, which is
installed on a computer.
MySQL was originally developed to handle large databases quickly. Although MySQL is
typically installed on only one machine, it is able to send the database to multiple locations,
as users are able to access it via different MySQL client interfaces. These interfaces send
SQL statements to the server and then display the results.
MySQL enables data to be stored and accessed across multiple storage engines, including
InnoDB, CSV, and NDB. MySQL is also capable of replicating data and partitioning tables
for better performance and durability. MySQL users aren't required to learn new commands;
they can access their data using standard SQL commands.
MySQL is written in C and C++ and accessible and available across over 20 platforms,
including Mac, Windows, Linux and Unix. The RDBMS supports large databases with
millions records and supports many data types including signed or unsigned integers 1, 2, 3,
4, and 8 bytes long; FLOAT; DOUBLE; CHAR; VARCHAR; BINARY; VARBINARY;
TEXT; BLOB; DATE; TIME; DATETIME; TIMESTAMP; YEAR; SET; ENUM; and
OpenGIS spatial types. Fixed- and variable-length string types are also supported.
For security, MySQL uses an access privilege and encrypted password system that enables
host-based verification. MySQL clients can connect to MySQL Server using several
protocols, including TCP/IP sockets on any platform. MySQL also supports a number of
client and utility programs, command-line programs and administration tools such as MySQL
Workbench.
Chapter 10
AWT and Swings
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
10.3 Difference between AWT and Swing
There are many differences between java awt and swing that are given below.
Swing contains components that you’ll use to build a GUI. I am listing you some of the
commonly used Swing components.
Chapter 8
Conclusion
Java offers the real possibility that most programs can be written in a type-safe language.
However, for Java to be broadly useful, it needs to have more expressive power than it does
at present.
The Java training allows its aspirants to gain extensive) knowledge of the advanced
programming technique and it covers all . Java is widely used because it is a very easy to
learn language with a rich standard library.
To create a small web application in cell phone to a large enterprise application that run on
internet, we use Java/ J2EE programming language. Java programming is simple, object-
oriented, robust, secure, architecture-neutral and portable, high performance, interpreted,
threaded and dynamic.
Chapter 11
References