-JAVA
What is Java?
Java is a high-level, general-purpose, object-oriented, and secure programming language
developed by James Gosling at Sun Microsystems, Inc. in 1991. It is formally known as OAK.
In 1995, Sun Microsystem changed the name to Java. In 2009, Sun Microsystem takeover by
Oracle Corporation.
Features of Java
o Simple: Java is a simple language because its
o syntax is simple, clean, and easy to understand.
o Object-Oriented: In Java, everything is in the form of the object. It means it has some
data and behavior. A program must have at least one class and object.
o Robust
o : Java makes an effort to check error at run time and compile time.
o Secure: Java is a secure programming language because it has no explicit pointer and
programs runs in the virtual machine.
o Platform-Independent: Java provides a guarantee that code writes once and run
anywhere. This byte code is platform-independent and can be run on any machine.
o Portable: Java Byte code can be carried to any platform. No implementation-
dependent features.
OOPs (Object Oriented Programming System)
Object-oriented programming is a way of solving a complex problem by breaking them into
a small sub-problem. An object is a real-world entity. It is easier to develop a program by
using an object.
o Class: An object is the instance of the class. We can define a class by using the class
keyword.
o Object: An object is a real-world entity that can be identified distinctly.
o Abstraction: An abstraction is a method of hiding irrelevant information from the
user.
o Encapsulation: An encapsulation is the process of binding data and functions into a
single unit. A class is an example of encapsulation.
o Inheritance: Inheritance is the mechanism in which one class acquire all the features
of another class.
o Polymorphism: The polymorphism is the ability to appear in many forms. In other
words, single action in different ways. For example, a boy in the classroom behaves
like a student, in house behaves like a son
Application
According to Sun, 3 billion devices run Java. There are many devices where Java is currently used.
Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as [Link], [Link], etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
JAVA PROGRAMMING FUNDAMENTALS
ACCESS SPECIFIER
Access specifiers or access modifiers are keywords in object-oriented languages that
set the accessibility of classes, methods and data members.
It defines which part of a program can be visible or accessible within the same class,
other class, same package and other package.
Access specifiers are also called as visibility modes.
Generally, access specifiers are keywords in java.
Example:-
Types of access specifier
Java provides following 4 types of access specifiers :-
1. Public access specifier
2. Private access specifier
3. Protected access specifier
4. No access/default access specifier
Public access specifier
When a class member is declared with a public keyword it is called as public access
specifier.
The class members declared with public access specifier can be accessed or visible
from any where of a program.
Private access specifier
When the members of a class declared with a private keyword they are called as
private access specifier.
The private members of a class can be accessed within the class only.
They cannot be accessible by other class, sub class of same package or other
package.
Example:-
Protected access specifier
When the members of a class are declared with a protected keyword then they are
called as protected access specifier.
The protected member of a class be accessible within the same class, sub class, other
class of same package and also can be accessible within the sub class of other
package.
No access/default access specifier
When the members of a class are declared without any specifier then they are called
as no access specifier.
They are accessible within same class, sub class, other class of same package.
They are not accessible to sub class and other class of other packages.
Since classes within the same package are allowed to access default members of a
class so that, it is also called as package level access specifier.
The visibility scope or accessibility of the access specifier in
tabular form
Other Sub Sub Other
Access Same
class(same class(same class(other class(other
specifier class
package) package) package) package)
Private Yes No No No No
Default Yes Yes Yes No No
Protected Yes Yes Yes Yes No
Public Yes Yes yes yes Yes
INHERITANCE
The process of creating a new class from an existing class is called as inheritance.
The new class is called as child class or sub class or derived class.
The existing class is called as parent class or super class or base class.
Inheritance defines parent-child relationship between classes.
The child class object inherits the properties of parent class as well as properties of its
own.
Syntax:-
Class <parent_class>
{
-------------
-------------
}
Class <child_class> extends <parent_class>
{
-------------
-------------….
Advantages of inheritance
Reusability of existing code.
Enhancement of base class.
Reduction of time and effort.
Increment of program reliability.
Types of inheritance
In OOP, following types of inheritance are used:-
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Single inheritance
When a child class inherits properties of an only one parent class, then it is known as
single inheritance.
It defines a simple linear relationship between a parent class and a child class.
Java uses extends keyword to inherit properties of a parent class.
Multiple inheritance
When a child class inherits properties of more than one parent classes then it is called
as multiple inheritance.
Java does not support multiple inheritance because the extends keyword cannot
extend more than one class at a time.
Java implements interface to achieve multiple inheritance.
Multilevel inheritance
When a child class is derived from a middle level parent class which is further derived
from another parent class then it is called multilevel inheritance.
Hierarchical inheritance
When more than one child classes inherits properties of a single parent class, then it
is called as hierarchical inheritance.
Hybrid inheritance
When two or more inheritance are used together in a single place, then it is known as
hybrid inheritance.
Hybrid inheritance is the combination of multilevel inheritance and multiple
inheritance.
Though multiple inheritance is not supported by java, hence hybrid inheritance is also
not supported by java.
FINAL KEYWORD
A final is a keyword used in Java to declare as a constant.
When data member of a class is declared as final, there values cannot be changed.
final int x=100;
x=200; //error
Because x is an integer constant.
When a class in java declared as final, it cannot be inherited.
STRING CLASS METHODS
COMMONLY USED LIBRARIES IN JAVA
In java, a library is a reusable software component containing various pre-defined
classes, methods used to reduce development time for the developer.
Library is also called as a package containing different classes, methods and
interfaces.
The following are various packages or libraries used in java:-
[Link]
[Link]
[Link]
[Link]
[Link], etc.
To use these packages in a program, java uses import keyword.
e.g. import [Link].*;
The [Link] library is imported in a Java program by default.
String class
A string is a set of characters written within double quotes (“ “).
Java provides a string class whose objects can hold unchanged string value, i.e. once
an object of a string is initialized it can’t be changed. This is called as string immutability.
In java a string object can be created and initialized directly in two ways:-
Syntax:-
String <object_name> = <value>;
OR,
String <object_name> = new String (“value”);
String class methods
Java provides a number of predefined methods to manipulate string objects known as
string class methods.
These methods can be used in java program by import the string class library in the
program.
The following are the various string methods of string class used in java:-
1. charAt(int Index)
2. concat(String str)
3. length()
4. toLowerCase()
5. toUpperCase()
6. trim()
7. substring(int startIndex)
8. substring(int startIndex, int endIndex)
charAt(int Index)
This method is used to return a specified character at a particular index of a string.
Example:-
class sample
{
public static void main(String args[])
{
String s = “SUNIL”;
[Link]([Link](2));
}
}
Output:-
concat(String str)
This method is used to concatenate/add a specific string at the end of the current
string object.
Example:-
class sample
{
public static void main(String args[])
{
String s = “SUNIL”;
[Link]([Link](“SAHU”));
}
}
Output:-
SUNILSAHU
Length()
This method is used to return the length of the current string.
In other words, it returns the number of characters in a string including space.
Syntax:-
<object_name>.length();
Example:-
class sample
{
public static void main(String args[])
{
String s = “RADHAKRISHNA”;
[Link]([Link]());
}
}
Output:-
12
toLowerCase()
This method is used to convert all the characters of a string into lowercase letter.
Example:-
class sample
{
public static void main(String args[])
{
String s = “SUNIL”;
[Link]([Link]());
}
}
Output:-
sunil
toUpperCase()
This method is used to convert all the characters of a string into uppercase letter.
Example:-
class sample
{
public static void main(String args[])
{
String s = “life”;
[Link]([Link]());
}
}
Output:-
LIFE
Trim()
This method is used to remove blank spaces from the beginning and ending of the
string.
It cannot remove blank spaces from the middle of the string.
Example:-
class sample
{
Public static void main(String args[])
{
String s = “ SUNIL ”;
[Link]([Link]());
String s1 = [Link]();
[Link]([Link]());
}
}
Output:-
7
5
Substring(int startIndex)
This method is used to return a substring from the current string starting from start
index character to the end of the string.
Example:-
class sample
{
public static void main(String args[])
{
String s = “SUNIL”;
[Link]([Link](2));
}
}
Output:-
NIL
Substring (int startIndex,int endIndex)
This method is used to return a substring from the current string starting from start
index character to the end index character of the string.>
Example:-
class sample
{
public static void main(String args[])
{
String s = “SUNIL”;
[Link]([Link](2,4));
}
}
Output:-
NI
MATH CLASS METHODS
To manipulate numbers and to perform various mathematical operations in a java program, java
provides a math class library.
The math class library contains various mathematical methods that are used to perform different
mathematical operations in java.
The general syntax of a math class method is :-
Math.<method_name>(argument);
The following are the various mathematical methods of math class used in java:-
1. Pow()
2. Round()
Pow()
This method is used to return the power of a number raised to an another number.
Example:-
class sample
{
public static void main(string args[])
{
[Link]([Link](2,3));
}
}
Output:-
Round()
This method is used to round up a value into its nearest integer value.
Example:-
class sample
{
public static void main(string args[])
{
[Link]([Link](7.4));
}
}
Output:-
DATABASE CONNECTIVITY, ODBC & JDBC
A database is a collection of related tables or files that stores huge amount of data in tabular format.
A database connection is a method used to connect an application program with database system.
Database connectivity means a connection and communication between application program and a
database system for accessing and manipulating data stored in the database.
A java application program can be connected with My SQL database using Netbeans IDE for
manipulation of data.
There are two methods used for database connectivity:-
1. ODBC
2. JDBC
ODBC
ODBC stands for Open Database Connectivity.
ODBC is an API (Application Programming Interface) used for accessing relational database.
It is a Microsoft’s API.
JDBC
JDBC stands for Java Database Connectivity.
It is platform independent API because it can be used for any platform.
JDBC API consist of a set of classes, interfaces and exceptions.
JDBC allows a programmer to connect with a database like My SQL.
It provides methods for querying, updating and accessing of data to and from a database.
It is used for RDBMS like MySQL, Oracle, etc.
JDBC has following tasks:-
1. Load the driver.
2. Establishes a connection between java application and database.
3. Create SQL statement and submit to server.
4. Process the request and provides the result.
JDBC DRIVER & TYPES OF JDBC DRIVER
JDBC driver is a software used to connect a java application with the database.
It allows a user to interact and manipulate with database.
It requires as an interface between application in client machine and database server.
Types of JDBC driver
1. JDBC-ODBC bridge driver
2. Native API driver
3. Network protocol driver
4. Thin driver
JDBC-ODBC bridge driver
It is a JDBC driver used to access relational database through ODBC driver.
It is also known as type-1 driver.
It converts JDBC call methods to ODBC call methods to connect to database.
It requires ODBC driver installation in the client machine.
It is a simple driver and easier to use and connect to the database.
Native API driver
It is a partially java driver.
It is also known as type-2 driver.
It required vendor database library installed in the client machine to access database.
It converts JDBC call methods into native calls of the database.
Network protocol driver
It uses a middleware application at server to connect to database.
The middleware software converts JDBC calls directly or indirectly into vendor specific database
protocol.
Thin driver
It is also known as type-4 driver.
The thin driver converts JDBC calls directly into vendor’s specific database protocol.
It does not required any middleware software for database connection.
e.g.- MySQL’s connector
Java Virtual Machine (JVM)
JVM is a run time environment which acts as an interpreter and translates
the byte code into object code (machine language). It is a platform-independent
execution environment that converts Java byte code into object code and
executes [Link] of Java wanted it to be a platform independent language. So the
concept of JVM was included in Java.
Source Code
The core program or text written in any computer language (like C, C++,
Java, etc.) is called source code. Usually it is a collection of computer instructions
written by using any human readable computer language. Source code files
have the extension class.
Object Code
The program in the form of machine instructions or binary instructions
(i.e., in computer readable form). It is generally produced by compiler/interpreter,
but in case of Java, it is produced by JVM.
Byte Code
In Java, when a source code is compiled, it doesn‘t directly convert into
object code, rather it converts into what, is known as byte code. So, a byte code
is machine instruction that the Java compiler (Javac) generates.
Javac –compiler that converts source code to byte code.
JVM- interpreter that converts byte code to machine language code.
Source Code ([Link]) Æ compile Æ Byte Code (program.
class)
Byte Code (program. class) Æ JVM Æ Machine Code
RAPID APPLICATION DEVELOPMENT (RAD) USING IDE
Rapid application development is abbreviated as RAD. It is an objectoriented
approach for software development, being introduced by James Martin
in the 1980s.
Following are two most popular RAD systems;
(i)NetBeans IDE is a cross-platform RAD tool for creating visual
desktop, mobile and web applications for Linux, Windows and Mac
OS X.
MyEclipse is a RAD environment, focusing on enterprise Java and
web application development.
Structure of Java Program
Structure of a java program is the standard format released by Language
developer to the Industry programmer. Sun Micro System has prescribed the
following structure for the java programmers for developing java application. ?
A package is a collection of classes, interfaces and sub-packages. A sub
package contains collection of classes, interfaces and sub-sub packages
etc. [Link].*; package is imported by default and this package is known
as default package.
Class is keyword used for developing user defined data type and every
java program must start with a concept of class.
―ClassName‖ represent a java valid variable name treated as a name of
the class each and every class name in java is treated as user-defined data
type.
Data member represents either instance or static they will be selected
based on the name of the class.
User-defined methods represents either instance or static they are meant
for performing the operations either once or each and every time.
Each and every java program starts execution from the main() method.
And hence main() method is known as program driver.
Since main() method of java is not returning any value and hence its return
type must be void.
Since main() method of java executes only once throughout the java
program execution and hence its nature must be static.
Since main() method must be accessed by every java programmer and
hence whose access specifier must be public.
Each and every main() method of java must take array of objects of String.
Block of statements represents set of executable statements which are in
term calling user-defined methods are containing business-logic.
The file naming conversion in the java programming is that which-ever
class is containing main() method, that class name must be given as a file
name with an extension .java.
Main ( ) Method
main ( ) method is starting execution block of a java program or any java
program start their execution from main method. If any class contain main( )
method known as main class.
Syntax:
public static void main(String args[])
{
……………..;
……………..;
}
Public
public is a keyword in a java language whenever if it is preceded by main()
method the scope is available anywhere in the java environment that means
main() method can be executed from anywhere. main() method must be accessed
by every java programmer and hence whose access specifier must be public.
Static
static is a keyword in java if it is preceded by any class properties for that
memory is allocated only once in the program. Static method are executed only
once in the program. main() method of java executes only once throughout the
java program execution and hence it declare must be static.
Void
void is a special datatype also known as no return type, whenever it is
preceded by main() method that will be never return any value to the operating
system. main() method of java is not returning any value and hence its return
type must be void.
String args[]
String args[] is a string array used to hold command line arguments in
the form of String values.