To Java Programming: Christ College - Pune Presented by David Thomas
To Java Programming: Christ College - Pune Presented by David Thomas
To Java Programming: Christ College - Pune Presented by David Thomas
to Java
Programming
CHRIST COLLEGE - PUNE
PRESENTED BY DAVID THOMAS
Features of Java
1.You are not allowed to write code outside class in Java.
2. Platform Independent.
3. No Pointers like C/C++.
4. unlike C/C++ Java Code are supposed to catch some specific exceptions(checked type)
otherwise compilation fails.
5. Automatic Memory management.
6. Data Structure Implementation API just use them - Collection Framework.
7. Java is complete Object Oriented language, still give support to write Pure Object Oriented code.
8. Open Source Frameworks are available in Java suits best for Enterprise application Development.
9. Android is the platform build upon Java so suits for mobile products also.
10. Java Docs/tutorials and Java SDK code is also available on internet so you can learn and know
inside working also of Java......
Application of Java
According to Sun Microsoft , 3 billion devices run Java. There are many devices where
Java is currently being used. Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as irctc.co.in, Amazon, Google Map etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
Types of Java Applications
There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application
Standalone applications are also known as desktop applications or window-based applications. These
are traditional software that we need to install on every machine. Examples of standalone application
are Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web
applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called enterprise
application. It has advantages of the high-level security, load balancing, and clustering. In Java, EJB is
used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application. Currently, Android
and Java ME are used for creating mobile applications.
Java is an object oriented language because it provides the features to implement an object oriented model.
These features includes Abstraction, encapsulation, inheritance and polymorphism.
OOPS is about developing an application around its data, i.e. objects which provides the access to their
properties and the possible operations in their own way.
Abstraction :One of the most fundamental concept of OOPs is Abstraction. Abstraction is a process where
you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when
you login to your Amazon account online, you enter your user_id and password and press login, what happens
when you press login, how the input data sent to amazon server, how it gets verified is all abstracted away
from the you.
Another example of abstraction: A car in itself is a well-defined object, which is composed of several other
smaller objects like a gearing system, steering mechanism, engine, which are again have their own
subsystems. But for humans car is a one single object, which can be managed by the help of its subsystems,
even if their inner details are unknown
Encapsulation
This post provides the theoretical explanation of Encapsulation with real-life examples. For detailed
explanation on this topic with java programs refer encapsulation in java with example.Encapsulation is:
• Binding the data with the code that manipulates it. It keeps the data and the code safe from external
interference
Inheritance
Inheritance is the mechanism by which an object acquires the some/all properties of another object. It
supports the concept of hierarchical classification.
Polymorphism
Polymorphism means to process objects differently based on their data type.
In other words it means, one method with multiple implementation, for a certain class of action. And
which implementation to be used is decided at runtime depending upon the situation (i.e., data type of
the object)This can be implemented by designing a generic interface, which provides generic methods
for a certain class of action and there can be multiple classes, which provides the implementation of
these generic methods.
Structure of a Java Program
package student;
import java.util.Date;
import java.applet.*;
SAMPLE JAVA PROGRAM
Simple.java
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
1. class CommandLineExample
2. {
3. public static void main(String args[])
4. {
5. System.out.println("Your first argument is: "+args[0]);
6. }
7. }
compile c:\Program File\Java\bin> javac CommandLineExample.java
run by c:\Program File\Java\bin> java CommandLineExample Christ
Output: Your first argument is: Christ
Class Definition
A Java program may contain several class definitions, classes are an essential part of any Java program. It defines the
information about the user-defined classes in a program.
A class is a collection of variables and methods that operate on the fields. Every program in Java will have at least one class
with the main method.
The main method is from where the execution actually starts and follows the order specified for the following statements.
Let’s take a look at a sample program to understand how it is structured.
When the main method is declared public, it means that it can be used outside of this class as well.
• The word static means that we want to access a method without making its objects. As we call the main method without
creating any objects.
• The word void indicates that it does not return any value. The main is declared as void because it does not return any value.
String[] args
It is an array where each element is a string, which is named as args. If you run the Java code through a console, you can
pass the input parameter. The main() takes it as an input.
System.out.println in Java
Java System.out.println() is used to print an argument that is passed to it. The statement can be
broken into 3 parts which can be understood separately as:
2.out: This is an instance of PrintStream type, which is a public and static member field of the
System class.
3.println(): As all instances of PrintStream class have a public method println(), hence we can invoke
the same on out as well. This is an upgraded version of print(). It prints any argument passed to it
and adds a new line to the output. We can assume that System.out represents the Standard Output
Stream.
Constructors
Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that
class.
Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the
same name as the class. A class can have more than one constructor.
Following is an example of a constructor −
Example
public class Puppy
{ public Puppy()
{
}
public Puppy(String name) { // This constructor has one parameter, name. }}
Java also supports Singleton Classes where you would be able to create only one instance of a class.
Creating an Object
As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new
keyword is used to create new objects.
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
Passed Name is :tommy
Accessing Instance Variables and Methods
Instance variables and methods are accessed via created objects. To
access an instance variable, following is the fully qualified path −
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/*
Now you can call a class method as follows */ObjectReference.MethodName();
public class Puppy {
int puppyAge;
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
public void setAge( int age ) {
puppyAge = age;
}
public int getAge( ) {
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args) {
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
Name chosen is :tommy
Puppy's age is :2
Variable Value :2
They are Employee and EmployeeTest.
First open notepad and add the following code. Remember this is the Employee class and the class is a public class. Now, save this source file with the name
Employee.java.
The Employee
import class has four instance variables - name, age, designation and salary. The class has one explicitly defined constructor, which takes a parameter.
java.io.*;
public class Employee {
String name; int age; String designation; double salary;
// This is the constructor of the class Employee
public Employee(String name)
{ this.name = name; }
// Assign the age of the Employee to the variable age.
public void empAge(int empAge)
{ age = empAge; }
/* Assign the designation to the variable designation.*/
public void empDesignation(String empDesig) { designation = empDesig; }
/* Assign the salary to the variable salary.*/
public void empSalary(double empSalary) { salary = empSalary; }
/* Print the Employee details */
public void printEmployee() { System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}
Local Variables
•Local variables are declared in methods, constructors, or blocks.
•Local variables are created when the method, constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or block.
•Access modifiers cannot be used for local variables.
•Local variables are visible only within the declared method, constructor, or block.
•There is no default value for local variables, so local variables should be declared and an initial value
should be assigned before the first use.
public class Test
{ public void pupAge()
{ int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
} public static void main(String args[])
{ Test test = new Test();
test.pupAge();
}
}
This will produce the following result −
Output
Puppy age is: 7