Complete JAVA Notes ???✍️ (1)
Complete JAVA Notes ???✍️ (1)
ABHISHEK MAURYA
What is JAVA 1
Java is a programming language and a platform. Java is a high level,
robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of
Oracle) in the year 1995 and released 23 January 1996. The first version of
java was JDK 1.0 and latest version of java 17.0.2
James Gosling is known as the father of Java. Before Java, its name was
Oak. Since Oak was already a registered company, so James Gosling and his
team changed the name from Oak to Java. The team of java called green team
and the first program name was Greentalk which is created by James
Gosling.
Platform: Any hardware or software environment in which a program runs, is
known as a platform. Since Java has a runtime environment (JRE) and API, it
is called a platform.
2
According to Sun, 5 billion devices run Java. There are many devices
where Java is currently used. Some of them are as follows:
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as irctc.co.in, etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
Why Use Java ? 3
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• Write once run any where
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch
to Java.
Features of Java
1. Object – Oriented 4
9
9
Successfully installed
9
Java Not installed
9
Java Variables ? 35
NOTE:
A class should always start with an uppercase first letter, and that the name of
the java file should match the class name.
Public: It is a Access Specifier used to public the Function .
Static: When java runtime starts, there is no object of the class present. That’s why the main
method has to be static so that JVM can load the class into memory and call the main
method.
After Object
Class Methods Static Vs Public 39
Constructors 44
Constructors is used to
Set the initial value for
the class attribute x
Encapsulation 46
Single Inheritance:
A B
53
Multilevel Inheritance
A B C
5454
Hierarchical Inheritance
55
A
B C
1. Compile-time Polymorphism
2. Runtime Polymorphism
• The package which are created by java programmer or user for their own use
are classed user defined package
•Abstract method: can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass (inherited from).
68
Interface 69
Java does not have a built-in Date class, but we can import the java. Time package
to work with the date and time API. The package includes many date and time
classes.
Class Description
Syntax
import java.util.ArrayList; // import the Array List class
ArrayList<String> cars = new Array List<String>(); // Create an ArrayList
77
ADD is used to insert element
REMOVE AND
CLEAR FUNCTION
Java AWT 82
Java AWT calls the native platform calls the native platform (operating
systems) subroutine for creating API components like TextField, ChechBox,
button, etc.
For example, an AWT GUI with components like TextField, label and button
will have different look and feel for the different platforms like Windows,
MAC OS, and Unix. The reason for this is the platforms have different view
for their native components and AWT directly calls the native subroutine
that creates those components.
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to
create window-based applications. It is built on the top of AWT (Abstract
Windowing Toolkit) API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight
components.
The javax.swing package provides classes for java swing API such as JButton,
JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
No. Java AWT Java Swing
1) AWT components are platform-dependent. 85
Java swing components are platform-independent.
3) AWT doesn't support pluggable look and Swing supports pluggable look and feel.
feel.
4) AWT provides less components than Swing. Swing provides more powerful components such
as tables, lists, scrollpanes, colorchooser,
tabbedpane etc.
5) AWT doesn't follows MVC(Model View Swing follows MVC.
Controller) where model represents data, view
represents presentation and controller acts as
an interface between model and view.
Login Page Project Using Netbeans (Jframe , AWT, Swing)
86
Variable Name: name
JDBC API is a Java API that can access any kind of tabular data, especially data stored
in a Relational Database. JDBC works with Java on a variety of platforms, such as
Windows, Mac OS, and the various versions of UNIX.
Why to Learn JDBC? 89
JDBC stands for Java Database Connectivity, which is a standard Java API for
database-independent connectivity between the Java programming language and a
wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that are
commonly associated with database usage.
To connect Java application with the MySQL database, we need to follow 5 following
steps.
In this example we are using MySql as the database. So we need to know following
informations for the mysql database:
1.Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2.Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database,
localhost is the server name on which mysql is running, we may also use IP address,
3306 is the port number and sonoo is the database name. We may use any database,
in such case, we need to replace the sonoo with our database name.
3.Username: The default username for the mysql database is root.
4.Password: It is the password given by the user at the time of installing the mysql
1.import java.sql.*;
2.class MysqlCon{
3.public static void main(String args[]){
4.try{
92
5.Class.forName("com.mysql.jdbc.Driver");
6.Connection con=DriverManager.getConnection(
7."jdbc:mysql://localhost:3306/sonoo","root","root");
8.//here sonoo is database name, root is username and password
9.Statement stmt=con.createStatement();
10.ResultSet rs=stmt.executeQuery("select * from emp");
11.while(rs.next())
12.System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
13.con.close();
14.}catch(Exception e){ System.out.println(e);}
15.}
16.}