Java is a general-purpose; object oriented programming language developed by Sun Microsystems in 1991. James Gosling and his team members developed Java. It was designed for the development of software for electronic devices like TVs, VCRs. The important features of java are as follows:
1) Java is both a compiled and an interpreted language
2) platform independent and portable
3) Object oriented language
4) Robust and secure
5) Distributed
6) High performance
7) Multithreaded and Interactive
Java Developer’s kit (JDK) - It Contains the basic tools and libraries necessary for creating, testing, documenting and executing java programs. There are seven main programs in JDK such as.
1) Javac - The java compiler
2) Java - The Java interpreter
3) Javadoc – It generates documentation in HTML.
4) Appletviewer - The Java interpreter to execute Java applets
5) Jdb - The Java Debugger to find and fix bugs in Java programs
6) Javap - The Java disassembler displays the accessible functions and data in a compiled class. It also displays the meaning of byte codes.
7) Javah - It creates interface between Java and c routines.
Tools Available for Java programming
API - The Application programming interface enables Java programmers to develop varieties of applets and applications. It contains six packages.
java.applet - For applet programming
java.awt - The abstract window toolkit for designing GUI likes Button, checkbox, choice, menu panel etc.
java.io - File input/output handling
java.lang – It provides useful classes to handle object, thread, Exception, String, system, math, Float, integer etc.
java.net – It contains classes for network programming, and supports TCP/IP networking protocols
java.util - It contains miscellaneous classes like vector, stack, list, date, dictionary etc.
Web browser - Java environment requires Java-enabled web browser to support java applets. Some popular java-enabled browsers are:
- Hot java from java soft web site (http://java.sun.com)
- Netscape Navigator from Netscape home page (http://home.nescape.com)
- Internet Explorer from Microsoft’s web page (http://www.microsoft.com)
Java IDE
There are number of IDEs (Integrated Development Environment) available to support the productivity of software development.
Java programs are available in two flavors:
1) Applet - A java applet is a program that appears embedded in a web document and applet comes into effect when the browser browses the web page
2) Application - It is similar to all other kind of programs like in c and c++. An application is required to solve a particular problem.
Simple Java program
Our first Java program is a simple application to print a message on the screen.
//Hello Java Application//
Class HelloWorldApp
{
Public Static void main(String args[])
{
System.out.println(“Hello Java!”);
}
}
How to edit this program?
Any text editor can be used to write Java programs. In windows Notepad can be used to write and edit the program. After that save the Application in a file with the name HelloWorldApp.java
How to compile this program?
The Java compiler (javac) converts a Java Application into Java byte code. Now open the DOS shell and move to the directory where you have saved Java Program. Then enter the following command to compile.
Javac HelloWorldApp.java
After the successful compilation, Java byte code will be produced which will be automatically stored in the same directory but with file name having extension .class. For the running example, the class filename will be
HelloWorldApp .class
How to run this program?
To execute the Java Application, just type the following command
Java HelloWorldApp
Finally, you will get the output as "Hello java !"
Computer education courses and jobs information
Electronic and Science fast developing in these days. A man going to moon and man disease decides for using science and computers. So computer most useful instrument and much more wanted mission to the men in the daily life. Daily changes System works learning in one of the most important thing to the programmers.
Somaney intuitions are offering different program courses in Information technology division.
Computer education is most important to students. Computer touches necessity from the child hood. Now a day everything depends on computer, and internet useful. So know well computer hardware and software. These are easy the way to earn more amount from this occupation.
Different on line courses
1) c/c++
2) Computer certification
3) Computer networking
4) Desktop publishing
5) Micro software
6) Internet
7) Web designing
And some other courses in Hyderabad institutes.
a) Dot net
b) Sap
c) Java
d) Oracle
e) Unix
f) Linux
BPO (technical support and voice basis) and contact center division is dedicated to provide the best talent for MNCs and domestic organization specialized and contact center operations in India. Information technology division is primary focused on IT staffing.
IT training institute of in Mumbai.
On latest IT technologies to have total job security IIHT Indian instate of hard ware.
6 Months industrial training program for MCA/B.E/B.tech students IIHT limited has pioneered the growth of IT and IMS training in India.
Hardware and networking. Bangalore, the silicon valley of India, Indian instate of hardware technology.
IIJT was group of professionals whose excellence in computer based training is worth to vouch for. A group, who has made it, is one of the largest centers in India.
Select to good institute to Take best tanning and get job easy in any ware with good pay of salary. That is better to luck, find it ware it is. Don’t miss with the neglect more compaction of in these days. Go try and try search fortune where it is with door opens.
Introduction
Other Object Oriented Programming languages, such as C++, provide multiple inheritance (deriving a single new class from two or more base classes). But such type of inheritance leads in ambiguity when two base classes have the methods with the same name. Fortunately Java does not support multiple inheritance. But it can not overlook the benefit of multiple inheritance. Therefore in order to enjoy the benefits of multiple inheritance, Java provides interfaces. An interface forces the classes to implement certain methods with certain signatures. Actually an interface takes the abstract concept one step ahead.
Interfaces
Interfaces are used to define a behavior of a class. That is, using interface you can specify what a class must do, but not how it does. Once an interface is defined you can implement any number of classes from this interface. Also one class can implement any number of interfaces.
Syntax
An interface is defined like a class, but using a keyword interface rather than class as:
public interface InterfaceName{
// ….}
Here the access specifier is used either public or not used. An interface contains constant values (final variables) and methods declarations. The difference between interface and classes is that the methods in an interface are only declared and not implemented, that is, the methods do not have a body. In other words there can be no default implementation of any method specified within an interface.
Example
public interface BankEmployee{
static final int count = 10;void setdata();
void display();}
Since the methods of an interface have to be overridden by the classes that implement it, therefore they can not be declared final. The data members can be public, static or final. Here public ensures that it can be accessed in unrelated classes; static ensures that you can not create an object of the interface and final ensures that the value is not changed in the classes that implement the interface.
Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. The keyword implements is used in order to implement an interface by a class.
Syntax
The keyword implements is used in order to implement an interface by a class.
class ClassName implements InterfaceName{
// ….}
Each class that includes an interface must implement all of the methods.
Example
class Manager implements BankEmployee{
// ….void setdata(){
// ….
}void display(){
// ….
}
}
Extending Interfaces
Like classes, interfaces can also be extended. It means that an interface can be subinterfaced from other interfaces. The new subinterface will inherit all the members of the super interface.
Syntax
A new interface is inherited from the old interface using the keywords – extends, as:
interface SubInterfaceName implements SuperInterfaceName{// ….
}
Here one should remember that when a class implements that inherits another interface, it must provide implementation for all the methods defined within the interface chain.
Program ExtendInterfaceDemo.java illustrates this concept.
// Program – ExtendInterfaceDemo.javainterface first{
void show1();
}interface second extends first{
void show2();}
class third implements second{public void show1(){ // Implementing first interface method declaration
System.out.println("First interface method");
}public void show2(){ // Implementing second interface method declaration
System.out.println("Second interface method");}
}class ExtendInterfaceDemo{
public static void main(String args[]){
third t = new third();
t.show1();t.show2();
}
}
The output of this program is….First interface method
Second interface method
Grouping Interfaces (Multiple Inheritance)
As stated earlier, Java does not directly supports multiple inheritance. But being a pure Object Oriented Programming Language, it can not forget the lovely taste of inheritance. Java enjoys the benefits of multiple inheritance by grouping several interfaces, which are separated by commas, into one interface.
Syntax
interface InterfaceName1{// ….
}interface InterfaceName2{
// ….}
interface InterfaceName3 extends InterfaceName1, InterfaceName2{// ….
}
Remember that the subinterfaces are too interfaces therefore you can not define the methods declared in super interfaces. When we implement a class from a subinterface then it is the duty of this class to define all the methods of superinterfaces as well as subinterfaces.
Program GroupInterfaceDemo.java illustrates this.
// Program – GroupInterfaceDemo.javainterface first{
void show1();}
interface second{void show2();
}interface third{
void show3();}
class forth implements first, second, third{public void show1(){ // Implementing first interface method declaration
System.out.println("First interface method");}
public void show2(){ // Implementing second interface method declaration
System.out.println("Second interface method");}
public void show3(){ // Implementing third interface method declaration
System.out.println("Third interface method");}
}class GroupInterfaceDemo{
public static void main(String args[]){
forth f = new forth();f.show1();
f.show2();
f.show3();
}
}
The output of this program is….
First interface methodSecond interface method
Third interface method
Interfaces Verses Abstract Classes
- An interface looks like an abstract class, but there are some differences between these two as shown below:
- An abstract class is an incomplete class that requires further specialization. An interface is just a specialization or prescription for the behavior.
- A class can implements several interfaces at once, whereas a class can extend only one parent class.
- An abstract class is generally used where you want to initiate a hierarchy of ore specialized classes. On the other hand, an interface is used where you say – “I need to be able to call methods with these signatures in your classes.”
Packages
Like an ordered library provides books, a package provides organizes related classes into groups. In other words a package is a collection of classes and interfaces of similar nature. Two packages may have classes of same name. But within a package, each class has unique name.
Java provides two types of packages:
- Built-in Packages
- User-defined Packages
Before the discussion of these packages let us study the import statement.
The import Statement
Java Package, irrespective of built-in or user-defined, can be used in a program by using the import statement.
Syntax
The import statement is used as:
import PackageName.*;// importing an entire package PackageName
import PackageName.ClassName;// importing a class ClassName from package PackageName
Example
import java.awt.*;import java.awt.Button;
Here a dot separates an element. This convention shows that a hierarchy has been followed while creating these built-in packages. The first statement imports all the classes or interfaces of the awt package. The second statement imports Button class from the awt package.
After importing the above package, you can create an object of the Button class as:
Button b = new Button(“Yes”);
However if you don’t want to import a package then you create an object of the Button class as:
java.awt.Button b = new java.awt.Button(“Yes”);
Naturally the first statement looks better than second.
- Built-in Packages
- User-defined Packages
User-defined Packages
You can create your own packages in order to organize your related classes. Such packages are known as user-defined packages.
Syntax
A user-defined package is created by writing the first statement in a Java program as:
package PackageName;
Here package is the reserved word and PackageName is the name of the user-defined package. You can make only one package declaration in a source Java program. After the package declaration all the classes declared within that file will belong to the specified package.
Example
package mypackage;
public class FirstClass{ // ….
}
Save this source file as FirstClass.java. When you compile this file – FirstClass.java, its class file – FirstClass.class is stored in the same directory with the name of the package. Now if you want to use the classes of this package then go to one level higher and then import a class or all classes of this package - mypackage.
Let your mypackage has the following path:
C:\>jdk\mypackage
And your FirstClass.java and FirstClass.class files are stored in C:\>jdk\mypackage\ then before using the classes of mypackage you must go to one level higher, say:
C:\>jdk
And then import the classes of mypackage, as shown below:
import mypackage.*;
public class FirstDemo{
public static void main(String args[]){
FirstClass f = new FirstClass()
}
}
S
ave this file as FirstDemo.java in C:\jdk\ directory and then compile and execute this file. If you do not go up one level higher and try to import the classes of mypackage, say you store the file FirstDemo.java in directory C:\>jdk\mypackage\ then you would certainly encounter the following error message:
“can’t find class FirstClass”
Problem Definition :- Write a java code to find out the sum of all digits of a given number.
Solution :-
Step 1: Classes that are required in coding.
MainClass.java - contains main() method.
SumDigits.java - contains calculateSumOfDigits() method in which business logic is written.
Step 2: Variables required in MainClass.java class
readNumber of type integer - purpose is to read number from user during run time.
Variables required in SumDigits.java class
sum and quotient, both of type integer. Purpose of sum variable is to store sum of all digits and that of quotient is to store quotient when number is divided by 10. Why divide by 10, is explained later
Step 3: Method required in SumDigits.java class is calculateSumOfDigits() in which actual business logic is written.
Step 4: Actual code,
In MainClass.java, object sumDigits of a SumDigits class is created and by using object of Scanner class, required number is read.
In SumDigits.java class, number that is read during run time is passed through calculateSumOfDigits() method. This number is divided by 10 to get one less digit (of unit's place) every time. Then the modulus is done on same number by using % with 10 which gives remainder and this remainder is same as that the number in unit's place of original number. And, ultimately that remainder is added every time when modulus is done. Here, remainder is stored in number variable and added with sum number. And then qoutient which was obtained using divide by 10, is again assigned to number variable for further division.
Following is its execution.
Problem Definition:- Write a Java code to reverse any string. String should be input from the user at the runtime. Write different class for the business logic of reversing a string.
P.S. Here, Eclipse Ganymede is used for coding. And, Exception Handling and entering false strings are not covered this source code. This source code is meant for beginners of Java.
Solution:-
Step 1: Class names :
- MainClass.java - This class contains main() method from where method of reversing a string, which is defined in another class, is called.
- ReverseStringLogic.java - This class contains reverseString() method which contains business logic for reversing a string and its display.
Step 2: Variable declarations in each class :
- In MainClass.java, following variables are required.
- readString with data type as String and initialised to null .
String readString = null;
- In ReverseStringLogic.java, following are the variables which will be used.
1. lengthOfStringToReverse with data type as int, initialised to 0 (zero). Purpose of this variable is to store length of string.
int lengthOfStringToReverse = 0;
2. counter with data type as int, initialised to zero. It is just a counter variable to iterate through for loop.
int counter = 0;
Step 3: Now, the actual code
1. In above source code, there is one class named MainClass. In java, name of the class and name of the file must be same.
2. Inside MainClass, object named "reverse" is created and invoked using new keyword. This object is created of class ReverseStringLogic class , for calling reverseString() method from the ReverseStringLogic class.
3. In java, to read any text entered during run time, an object of Scanner class needs to be used. Here, scanLine is an object of Scanner class.
4. System.out.println() is used to display text , which will make it easier for any layman user to enter any appropriate text. Here, System is a java class that provides access to the system, out is an output stream that is connected to console, and println() is a method which displays any text that is written inside it, on console.
5. Using scanLine.next(), input that is entered during run time, is captured and stored in appropriate variable. Here, that variable is readString.
6. Using object of ReverseStringLogic class, "reverse", method reverseString() is called. This method takes argument as readString of type String. Here, string entered during run time is stored in readString variable and is passed through argument in method reverseString().
1. In above snapshot, ReverseStringLogic class is shown which contains reverseString() method, which contains one argument, stringToReverse of type String. As length() method is used for variables of data type String, we need to import package java.lang.String. Here , java.lang is a package, and String is a class.
2. length() method is used to calculate length of any string and length of string is stored in variable lengthOfStringToReverse, which is used in "for" loop while iteration.
3. "for" loop is used to iterate through the string with its variable as counter initialised to length of string i.e. lengthOfStringToReverse minus 1. "Minus 1" is done because in java, if string contains 8 characters then the range of storing these characters is from 0 to 7. Condition given as "counter >=0", as we have to reverse the string and finally, we have post decremented the counter by 1 "counter--".
4. Inside "for" loop, charAt() method is used, which contains one argument of type integer. charAt() method takes the character present at particular location. In above code, if value of counter variable is 5 then, it will take character present at 5th location from original string and is displayed using System.out.println(). The above for loop will run those many times depending on length of the string i.e. if length of string is 8 then for loop will run 8 times. Following is the ouput of the above source code.