Sie sind auf Seite 1von 31

Basics of

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.

No memory is allocated at the time Sufficient memory space will be


of declaration allocated for all the variables of class
at the time of declaration.

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.

• Primitive data types


• Primitive data types are those whose variables allows us to store only one
value but they never allows us to store multiple values of same type. This
is a data type whose variable can hold maximum one value at a time.
Example
• int a; // valid
• a=10; // valid
• a=10, 20, 30; // invalid
Derived data types
Derived data types are those whose variables allow us to store multiple
values of same type. But they never allows to store multiple values of
different types. These are the data type whose variable can hold more
than one value of similar type. In general derived data type can be achieve
using array.
Example
• int a[] = {10,20,30}; // valid
• int b[] = {100, 'A', "ABC"}; // invalid

User defined data types


• User defined data types are those which are developed by programmers
by making use of appropriate features of the language.
• User defined data types related variables allows us to store multiple
values either of same type or different type or both. This is a data type
whose variable can hold more than one value of dissimilar type, in java it
is achieved using class concept.
• Note: In java both derived and user defined data type combined name as
reference data type.
Example
• Student s = new Student();
Data Types
In java we have eight data type which are
organized in four groups. They are
• Integer category data types
• Character category data types
• Float category data types
• Boolean category data types
Integer category data types
These category data types are used for storing integer data in
the main memory of computer by allocating sufficient amount
of memory space.
Integer category data types are divided into four types which
are given in following table

Data Type Size Range

1 Byte 1 + 127 to -128


2 Short 2 + 32767 to -32768
3 Int 4 + x to - (x+1)
4 Long 8 + y to - (y+1)
Character category data types
A character is an identifier which is enclosed within single quotes. In java
to represent character data, we use a data type called char. This data type
takes two byte since it follows Unicode character set.

Data Type Size(Byte) Range

Char 2 232767 to -32768

Why Java take 2 byte of memory for store character ?

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.

Why Boolean data types take zero byte of memory ?


Boolean data type takes zero bytes of main memory
space because Boolean data type of java implemented
by Sun Micro System with a concept of flip - flop. A flip
- flop is a general purpose register which stores one bit
of information (one true and zero false).
Cont..
Data Type Default Value Default size

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte


Variable Declaration Rules in Java
Variable is an identifier which holds data or another one variable is an
identifier whose value can be changed at the execution time of program.
Variable is an identifier which can be used to identify input data in a
program.
Cont..
Rules to declare a Variable
• Every variable name should start with either alphabets
or underscore ( _ ) or dollar ( $ ) symbol.
• No space are allowed in the variable declarations.
• Except underscore ( _ ) no special symbol are allowed
in the middle of variable declaration
• Variable name always should exist in the left hand side
of assignment operators.
• Maximum length of variable is 64 characters.
• No keywords should access variable name.
Operators in Java
Operators in Java
Operator is a special symbol that tells the
compiler to perform specific mathematical or
logical Operation. Java supports following lists of
operators.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Ternary or Conditional Operators
Structure of Java Program
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.
Main() Method in Java
• 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 of main() method:
Syntax
public static void main(String args[])
{
.......
.......
}

Das könnte Ihnen auch gefallen