Beruflich Dokumente
Kultur Dokumente
Java
Categories of Java
Java divided into three categories
• J2SE (Java 2 Standard Edition)
• J2EE (Java 2 Enterprise Edition)
• J2ME (Java 2 Micro or Mobile Edition)
• J2SE
• J2SE is used for developing client side applications.
• J2EE
• J2EE is used for developing server side applications.
• J2ME
• J2ME is used for developing mobile or wireless application by
making use of a predefined protocol called WAP(wireless Access /
Application protocol).
Basic Points of Java
Java is a platform independent, more powerful, secure,
high performance, multithreaded programming language.
Here we discuss some points related to Java.
• Byte Code
Byte code is the set of optimized instructions generated
during compilation phase and it is more powerful than
ordinary pointer code.
• JRE
The Java Runtime Environment (JRE) is part of the Java
Development Kit (JDK). It contains a set of libraries and
tools for developing Java application. The Java Runtime
Environment provides the minimum requirements for
executing a Java application.
Cont..
• JVM
JVM is set of programs developed by sun Micro System and
supplied as a part of the JDK for reading line by line line of byte
code and it converts into a native understanding form of operating
system. The Java language is one of the compiled and interpreted
programming language.
• Garbage Collector
The Garbage Collector is the system Java program which runs in the
background along with a regular Java program to collect un-
Referenced (unused) memory space for improving the performance
of our applications.
• Note: Java programming does not support destructor concept in
place of destructor, we have garbage collector program.
Cont..
• API
An API (Application Programming Interface) is a
collection of packages, a package is the collection
of classes, interfaces and sub-packages. A sub-
package is a collection of classes, Interfaces and
sub sub packages etc.
Java programming contains user friendly syntax
so that we can develop effective applications. in
other words if any language is providing user
friendly syntax, we can develop error free
applications.
Features of Java
First Java Program
• Requirements for java Program
For executing any java program we need given
things.
Install the JDK version 1.8 if you don't have
installed it.
Set path of the jdk/bin directory.
Create the java program
Compile and run the java program
Cont..
Steps For compiling and executing the java program
Java is very simple programming language first we write a java program
and save it with program class name.
In below program we create a java program with "First" name so we save
this program with "First.java" file name. We can save our java program
anywhere in our system or computer.
class First
{
public static void main(String[] args)
{
System.out.println("Hello Java");
System.out.println("My First Java Program");
}
}
Compile and Execute as discussed is lecture 1
Process
During the program execution internally following steps
will be occurs.
• Class loader subsystem loads or transfer the specified class
into main memory(RAM) from secondary memory(hard
disk)
• JVM takes the loaded class
• JVM looks for main method because each and every java
program start executing from main() method.
• Since main() method of java is static in nature, JVM call the
main() method with respect to loaded class (Example: First
as First.main(--))
• Note: A java program can contain any number of main
method but JVM start execution from that main() method
which is taking array of object of String class.
Object and class in Java
• Object is the physical as well as logical entity where as class is the
only logical entity.
• Class: Class is a blue print which is containing only list of variables
and method and no memory is allocated for them. A class is a group
of objects that has common properties. (A class is a set of objects
that share a common structure and a common behavior)
• A class in java contains:
• Data Member
• Method
• Constructor
• Block
• Class and Interface
Cont..
• Object is a instance of class, object has state
and behaviors. (a visible or tangible thing of
relative stable form; A thing that may be
apprehended intellectually; A thing to which
thought or action is directed)
• An Object in java has three characteristics:
• State
• Behavior
• Identity
Cont..
• State: The state of an object encompasses all of the (static) properties of the
object plus the current (dynamic) values of each of these properties
• A property is an inherent or distinctive characteristic, trait, quality, or feature that
contribute to making an object uniquely that object
• Examples
• Properties ( Attributes ) :
– Elevators travel up or down
– Vending machines accept coins
– Clocks indicate the current time
• Values
– Current floor
– Number of coins deposited
– The number of minutes since the last hour
• Behavior: Behavior is how an object acts and reacts, in terms of state changes and
interactions with other objects.
• An operation is some action that one object performs upon another in order to
elicit a reaction.
• We will use the word method to describe object behavior in java.
• Invoking a method causes the behavior to take place.
Cont..
• Types of Methods
• There are 4 basic types of methods:
• Modifier (sometimes called a mutator)
• Changes the value associated with an attribute of the object
• E.g. A method like Change_Car_Color
• Accessor
• Returns the value associated with an attribute of the object
• E.g. A method like Price_of_Car
• Constructor
• Called once when the object is created (before any other method will be invoked)
• E.g. Car(Mustang)
• Destructor
• Called when the object is destroyed
• E.g.~Car( )
• Identity: Identity is the property of an object that distinguishes it from all other
objects.
• The failure to recognize the difference between the name of the object and the
object itself is the source of many errors in object-oriented (OO) programming.
Difference between Class and Object in
Java
Class Object
Class is a container which collection Object is a instance of class
of variables and methods.
One class definition should exist only For one class multiple objects can be
once in the program. created.
Simple Example of Object and Class
In this example, we have created a Employee class that have two data members
eid and ename. We are creating the object of the Employee class by new keyword
and printing the objects value.
class Employee
{
int eid; // data member (or instance variable)
String ename; // data member (or instance variable)
eid=101;
ename=“Ahmed";
public static void main(String args[]) {
Employee e=new Employee(); // Creating an object of class Employee
System.out.println("Employee ID: "+e.eid);
System.out.println("Name: "+e.ename);
}
}
Data Type in Java
Datatype is a spacial keyword used to allocate sufficient memory space for
the data, in other words Data type is used for representing the data in
main memory (RAM) of the computer.
In general every programming language is containing three categories of
data types. They are
• Fundamental or primitive data types
• Derived data types
• User defined data types.
Java support more than 18 international languages so java take 2 byte for
characters, because for 18 international language 1 byte of memory is not
sufficient for storing all characters and symbols present in 18 languages. Java
supports Unicode but c support ascii code. In ascii code only English language
are present, so for storing all English letter and symbols 1 byte is sufficient.
Unicode character set is one which contains all the characters which are
available in 18 international languages and it contains 65536 characters
Float category data types
Float category data type are used for representing
float values. This category contains two data types,
they are in the given table
Number of
Data Type Size Range
decimal places
+2147483647
Float 4 to - 8
2147483648
Double 8 + 9.223*1018 16
Boolean category data types
Boolean category data type is used for representing or
storing logical values is true or false. In java
programming to represent Boolean values or logical
values, we use a data type called Boolean.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte