0% found this document useful (0 votes)
1K views20 pages

Web Technology UNIT-1

This document provides an introduction to web technology and development. It discusses the history of the World Wide Web and protocols like HTTP that govern the web. It also covers writing web projects, targeting users, and connecting to the internet. Core Java concepts like data types, variables, classes and exceptions are introduced.

Uploaded by

Yash Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views20 pages

Web Technology UNIT-1

This document provides an introduction to web technology and development. It discusses the history of the World Wide Web and protocols like HTTP that govern the web. It also covers writing web projects, targeting users, and connecting to the internet. Core Java concepts like data types, variables, classes and exceptions are introduced.

Uploaded by

Yash Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Web Technology (KCS602)

UNIT-4
Introduction: Introduction and Web Development Strategies, History of Web and Internet,
Protocols Governing Web, Writing Web Projects, Connecting to Internet, Introduction to
Internet services and tools, Introduction to client-server computing. Core Java:
Introduction, Operator, Data type, Variable, Arrays, Methods & Classes, Inheritance,
Package and Interface, Exception Handling, Multithread programming, I/O, Java Applet,
String handling, Event handling, Introduction to AWT, AWT controls, Layout managers.
Introduction and Web Development Strategies

World Wide Web


i. The World Wide Web is a system of interlinked hypertext documents accessed via the
Internet. Web is a huge collection of pages of information linked to each other around
the globe.
ii. The World Wide Web (WWW) is a global information medium which users can read
and write via computer connected to the internet.
iii. The Web, or World Wide Web, is basically a system of Internet servers that support
specially formatted documents. The documents are formatted in a markup language
called HTML (Hypertext Markup Language) that supports links to other documents, as
well as graphics, audio, and video files.
iv. World Wide Web (WWW) is collection of text pages, digital photographs, music
files, videos, and animations you can access over the Internet.

 History of WWW

a. WWW is created by Sir Tim Berners Lee in 1989 at CERN in Geneva.


b. In 1990, the first text only browsers were setup and CERN scientist could access
hypertext files and other information at CERN. HTML was based on a subset of the
standard generalized markup language (SGML). To transfer HTML document to remote
sites a new protocol was devised called HTTP (Hyper Text Transfer Protocol).
c. In the fall of 1991, conference goes around the world started hearing about the
promise but sparks still were not flying.
d. In 1993, there are only about 50 websites worldwide. A browser that allowed user to
take advantage of the web’s graphical capabilities was developed at the National
center for Super Computing application (NCSA). NCSA called the browser Mosaic.
What is The Internet?
The Internet is a massive network of networks, a networking infrastructure. It connects
millions of computers together globally, forming a network in which any computer can
communicate with any other computer as long as they are both connected to the Internet.
Information that travels over the Internet does so via a variety of languages known as
protocols.
So we can says that Internet is network of computer which connect to together and any
computer communicate with any other computer.

Different between Internet and WWW


The Web is a Portion of The Internet. The Web is just one of the ways that information
can be disseminated over the Internet. The Internet, not the Web, is also used for email,
which relies on SMTP, Usenet news groups, instant messaging and FTP. So the Web is just
a portion of the Internet.
Protocols Governing Web
Protocol: A protocol is a set of rules that is used to communicate applications to each other. A
protocol is the interface required for communicating the different applications.
a. HTTP
b. TCP/IP
c. FTP
d. SMTP
e. TELNET
 Hyper Text Transfer Protocol (HTTP)
HTTP is a communication protocol. It defines mechanism for communication between browser
and the web server. It is also called request and response protocol because the communication
between browser and server takes place in request and response pairs.
HTTP Request
HTTP request comprises of lines which contains:
 Request line
 Header Fields
 Message body
Key Points
 The first line i.e. the Request line specifies the request method i.e. Get or Post.
 The second line specifies the header which indicates the domain name of the server from
where index.htm is retrieved.
HTTP Response
Like HTTP request, HTTP response also has certain structure. HTTP response contains:
 Status line
 Headers
 Message body
 Hyper Text Transfer Protocol Secure (HTTPS)
1) HTTPS is an abbreviation of Hypertext Transfer Protocol Secure.
2) It is a secure extension or version of HTTP.
3) This protocol is mainly used for providing security to the data sent between a website and
the web browser.
4) It is widely used on the internet and used for secure communications. This protocol uses
the 443 port number for communicating the data.
5) This protocol is also called HTTP over SSL because the HTTPS communication
protocols are encrypted using the SSL (Secure Socket Layer).
6) By default, it is supported by various web browsers.
7) Those websites which need login credentials should use the HTTPS protocol for sending
the data.
8) It allows users to create a secured encrypted connection and helps them to protect their
information from being stolen.
Advantages of HTTPS
Following are the advantages or benefits of a Hypertext Transfer Protocol Secure (HTTPS):
o The main advantage of HTTPS is that it provides high security to users.
o Data and information are protected. So, it ensures data protection.
o SSL technology in HTTPS protects the data from third-party or hackers. And this
technology builds trust for the users who are using it.
o It helps users by performing banking transactions.
Disadvantages of HTTPS
Following are the disadvantages or limitations of a Hypertext Transfer Protocol Secure
(HTTPS):
o The big disadvantage of HTTPS is that users need to purchase the SSL certificate.
o The speed of accessing the website is slow because there are various complexities in
communication.
o Users need to update all their internal links.

 Internet Protocol (IP), which provides unreliable, connectionless packet


delivery for the Internet.
IP is connectionless because it treats each packet of information independently. It
is unreliable because it does not guarantee delivery, meaning, it does not require
acknowledgments from the sending host, the receiving host, or intermediate hosts.
IP provides the interface to the network interface level protocols. The physical
connections of a network transfer information in a frame with a header and data.
The header contains the source address and the destination address. IP uses an
Internet datagram that contains information similar to the physical frame. The
datagram also has a header containing Internet Protocol addresses of both source
and destination of the data.
Difference between HTTP and HTTPS
HTTP HTTPS

1. It is an abbreviation of Hypertext Transfer 1. It is an abbreviation of Hypertext Transfer


Protocol Protocol Secure.

2. This protocol operates at the application layer. 2. This protocol operates at the transport layer.

3. The data which is transferred in HTTP is plain 3. The data which is transferred in HTTPS is
text. encrypted, i.e., ciphertext.

4. By default, this protocol operates on port 4. By default, this protocol operates on port
number 80. number 443.

5. The URL (Uniform Resource Locator) of 5. The URL (Uniform Resource Locator) of
HTTP start with http:// HTTPS start with https://

6. This protocol does not need any certificate. 6. But, this protocol requires an SSL (Secure
Socket Layer) certificate.

7. Encryption technique is absent in HTTP. 7. Encryption technique is available or present in


HTTPS.

8. The speed of HTTP is fast as compared to 8. The speed of HTTPS is slow as compared to
HTTPS. HTTP.

9. It is un-secure. 9. It is highly secure.


10. Examples of HTTP websites are Educational 10. Examples of HTTPS websites are shopping
Sites, Internet Forums, etc. websites, banking websites, etc.

Website: A website is simply a collection of interlinked web pages.


Classification: A. Corporate Website B. Individual website

A. Corporate Website:
i. In this, there is certain no. of persons, who develop their website for a particular
organization.
ii. The corporate website are formed when group of people have common interest
and objective.
iii. The purpose of this website is to convey the information of organization to all over
the world.
B. Individual website: It is just like profile management system. In this type of website an
individual wants to develop website for h-projection, career growth etc.
Web Applications:
a. Simple office software (word processors, online spreadsheets, and presentation tools).
b. More advanced applications such as project management, computer-aided
design, video editing and point-of-sale.
Writing web Projects and Target Users:
A. Write a project mission statement: Write the specific mission statement that you
want to do.
B. Identify Objectives:
i. Specific
ii. Measurable
iii. Attainable
iv. Realistic
v. Time limited
C. Identify your target users: The matter of a website will be determined by the users
whom you want to visit the site. This is totally depend upon
i. Market research
ii. Focus group
iii. Understanding intranet audiences
D. Determine the scope: By supporting documents and client’s approval.
E. Budget:
i. Assumption for budgets.
ii. Budget categories.
iii. Determine hidden costs and tools.
F. Planning issues:
i. Discuss client’s existing information system.
ii. Project team and developing infrastructure.
iii. Where the website will place.
ARPANET:
 In 1969, a project was funded by the Advanced Research Project Agency (ARPA), an
arm of the U.S. Department of Defence. ARPA established a packet-switching network of
computers linked by point-to-point leased lines called Advanced Research Project
agency Network (ARPANET) that provided a basis for early research into networking.
 The conventions developed by ARPA is specify how individual computers could
communicate across that network became TCP/IP.
 As networking possibilities grew to include other types of links and devices, ARPA
adapted TCP/IP to the demands of the new technology. As involvement in TCP/IP grew,
the scope of ARPANET expanded until it became the backbone of an internet- work
today referred to as the internet.
ISP:
 The Internet Service Provider (ISP) gives you the telephone access and software you
need to connect to the internet along with some technical help.
 Many ISPs also include an electronic mail account, host customers web pages and
offer services a company that do business on the Internet.
 There are aprox. 200 ISPs in India like VSNL, MTNL, ERNET (Education and
Research Community Network) etc.

 Introduction to Java:
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working
version. This language was initially called “Oak” but was renamed “Java” in 1995.
 Java Applets and Applications
Java can be used to create two types of programs: applications and applets. An application is a
program that runs on your computer, under the operating system of that computer. That is, an
application created by Java is more or less like one created using C or C++. When used to create
applications, Java is not much different from any other computer language.
An applet is an application designed to be transmitted over the Internet and executed by a Java-
compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded
across the network, just like an image, sound file, or video clip. The important difference is that
an applet is an intelligent program, not just an animation or media file. In other words, an applet
is a program that can react to user input and dynamically change—not just run the same
animation or sound over and over.
JDK (Java Development Kit).The Java Development Kit (JDK) is a software development
environment which is used to develop Java applications and applets. It physically exists. It
contains JRE + development tools. The JDK contains a private Java Virtual Machine (JVM) and
a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java Application.
Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java
Code or applications. It converts Java bytecode into machines language. JVM is a part of Java
Runtime Environment (JRE). In other programming languages, the compiler produces machine
code for a particular system. However, Java compiler produces code for a Virtual Machine
known as Java Virtual Machine.
Here is how JVM works
First, Java code is compiled into bytecode. This bytecode gets interpreted on different machines
Between host system and Java source, Bytecode is an intermediary language.
JVM in Java is responsible for allocating memory space.

JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory area,
execution engine etc.
1) Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java
program, it is loaded first by the classloader. There are three built-in classloaders in Java.
1. Bootstrap ClassLoader: This is the first classloader which is the super class of
Extension classloader. It loads the rt.jar file which contains all class files of Java
Standard Edition like java.lang package classes, java.net package classes, java.util
package classes, java.io package classes, java.sql package classes etc.
2. Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader
of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: This is the child classloader of Extension classloader.
It loads the classfiles from classpath. By default, classpath is set to current directory. You
can change the classpath using "-cp" or "-classpath" switch. It is also known as
Application classloader.
2) Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.
3) Heap
It is the runtime data area in which objects are allocated.
4) Stack
Java Stack stores frames. It holds local variables and partial results, and plays a part in method
invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method
invocation completes.
5) Program Counter Register
PC (program counter) register contains the address of the Java virtual machine instruction
currently being executed.
6) Native Method Stack
It contains all the native methods used in the application.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter: Read bytecode stream then execute the instructions.
3. Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts
of the byte code that have similar functionality at the same time, and hence reduces the
amount of time needed for compilation. Here, the term "compiler" refers to a translator
from the instruction set of a Java virtual machine (JVM) to the instruction set of a
specific CPU.
8) Java Native Interface
Java Native Interface (JNI) is a framework which provides an interface to communicate with
another application written in another language like C, C++, Assembly etc. Java uses JNI
framework to send output to the Console or interact with OS libraries.
Software Code Compilation & Execution process
In order to write and execute a software program, you need the following
1) Editor – To type your program into, a notepad could be used for this
2) Compiler – To convert your high language program into native machine code
3) Linker – To combine different program files reference in your main program together.
4) Loader – To load the files from your secondary storage device like Hard Disk, Flash Drive,
CD into RAM for execution. The loading is automatically done when you execute your code.
5) Execution – Actual execution of the code which is handled by your OS & processor.
With this background, refer the following video & learn the JVM internal working and
architecture of JVM (Java Virtual Machine).
Bytecode
Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time
system, which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is
an interpreter for bytecode.
JVM: Translating a Java program into bytecode helps makes it much easier to run a program in a
wide variety of environments. The reason is straightforward: only the JVM needs to be
implemented for each platform. Once the run-time package exists for a given system, any Java
program can run on it.

The Java Buzzwords


1. Simple
2. Secure
3. Portable
4. Object-oriented
5. Robust
6. Multithreaded
7. Architecture-neutral
8. Interpreted
9. High performance
10. Distributed
11. Dynamic
Simple
Assuming that you have some programming experience, you will not find Java hard to master.
Java inherits the C/C++ syntax and many of the object-oriented features of C++, most
programmers have little trouble learning Java.
Secure
When you use a Java-compatible Web browser, you can safely download Java applets
without fear of viral infection or malicious intent. Java achieves this protection by
confining a Java program to the Java execution environment and not allowing it access to
other parts of the computer.
Portable
Java has had a profound effect on the Internet. The reason for this is quite simple: Java
expands the universe of objects that can move about freely in cyberspace. In a network
very broad categories of objects are transmitted between the server and your personal
computer.
Object-oriented
Java manages to strike a balance between the purists’s “everything is an object”
paradigm. The object model in Java is simple and easy to extend, while simple types,
such as integers, are kept as high-performance non-objects.
Robust
To better understand how Java is robust, consider two of the main reasons for program failure:
memory management mistakes and mishandled exceptional conditions (that is, run-time errors).
Java virtually eliminates these problems by managing memory allocation and de-allocation for
you. (In fact, de-allocation is completely automatic, because Java provides garbage collection for
unused objects.). In a well-written Java program, all run-time errors can—and should—be
managed by your program
Multithreaded
Java supports multithreaded programming, which allows you to write programs that do
many things simultaneously. The Java run-time system comes with an elegant yet
sophisticated solution for multi-process synchronization that enables you to construct
smoothly running interactive systems.
Architecture-neutral
The Java designers made several hard decisions in the Java language and the Java Virtual
Machine in an attempt to alter this situation. Their goal was “write once; run anywhere,
any time, forever.” To a great extent, this goal was accomplished.
Interpreted and High performance
Java was engineered for interpretation, the Java bytecode was carefully designed so that it
would be easy to translate directly into native machine code for very high performance by
using a just-in-time compiler. Java run-time systems that provide this feature lose none of
the benefits of the platform-independent code. “High-performance cross-platform” is no
longer an oxymoron.
Distributed
The original version of Java (Oak) included features for intra-address space messaging.
This allowed objects on two different computers to execute procedures remotely. Java
revived these interfaces in a package called Remote Method Invocation (RMI).
Dynamic
It the robustness of the applet environment, in which small fragments of bytecode may be
dynamically updated on a running system.
 Operator
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:
 Arithmetic Operators
 Relational Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
The Arithmetic Operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in
algebra. The following table lists the arithmetic operators:
Assume integer variable A holds 10 and variable B holds 20, then:
Show Examples
SR.NO Operator and Example

1 + ( Addition )
Adds values on either side of the operator
Example: A + B will give 30

2 - ( Subtraction )
Subtracts right hand operand from left hand operand
Example: A - B will give -10

3 * ( Multiplication )
Multiplies values on either side of the operator
Example: A * B will give 200

4 / (Division)
Divides left hand operand by right hand operand
Example: B / A will give 2

5 % (Modulus)
Divides left hand operand by right hand operand and returns remainder
Example: B % A will give 0

6 ++ (Increment)
Increases the value of operand by 1
Example: B++ gives 21

7 -- ( Decrement )
Decreases the value of operand by 1
Example: B-- gives 19

The Relational Operators:


There are following relational operators supported by Java language
Assume variable A holds 10 and variable B holds 20, then:
Show Examples
SR.NO Operator and Description

1 == (equal to)
Checks if the values of two operands are equal or not, if yes then condition
becomes true.
Example: (A == B) is not true.

2 != (not equal to)


Checks if the values of two operands are equal or not, if values are not equal then
condition becomes true.
Example: (A != B) is true.

3 > (greater than)


Checks if the value of left operand is greater than the value of right operand, if yes
then condition becomes true.
Example: (A > B) is not true.

4 < (less than)


Checks if the value of left operand is less than the value of right operand, if yes then
condition becomes true.
Example: (A < B) is true.

5 >= (greater than or equal to)


Checks if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true.
Example (A >= B) is not true.

6 <= (less than or equal to)


Checks if the value of left operand is less than or equal to the value of right
operand, if yes then condition becomes true.
example(A <= B) is true.

The Bitwise Operators:


Java defines several bitwise operators, which can be applied to the integer types, long, int, short,
char, and byte.
Show Examples
SR.NO Operator and Description

1 & (bitwise and)


Binary AND Operator copies a bit to the result if it exists in both operands.
Example: (A & B) will give 12 which is 0000 1100

2 | (bitwise or)
Binary OR Operator copies a bit if it exists in either operand.
Example: (A | B) will give 61 which is 0011 1101

3 ^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in one operand but not both.
Example: (A ^ B) will give 49 which is 0011 0001

4 ~ (bitwise compliment)
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
Example: (~A ) will give -61 which is 1100 0011 in 2's complement form due to a
signed binary number.
5 << (left shift)
Binary Left Shift Operator. The left operands value is moved left by the number of
bits specified by the right operand
Example: A << 2 will give 240 which is 1111 0000

6 >> (right shift)


Binary Right Shift Operator. The left operands value is moved right by the number
of bits specified by the right operand.
Example: A >> 2 will give 15 which is 1111

7 >>> (zero fill right shift)


Shift right zero fill operator. The left operands value is moved right by the number of
bits specified by the right operand and shifted values are filled up with zeros.
Example: A >>>2 will give 15 which is 0000 1111

The Logical Operators:


The following table lists the logical operators:
Assume Boolean variables A holds true and variable B holds false, then:
Show Examples
Operator Description

1 && (logical and)


Called Logical AND operator. If both the operands are non-zero, then the
condition becomes true.
Example (A && B) is false.

2 || (logical or)
Called Logical OR Operator. If any of the two operands are non-zero, then the
condition becomes true.
Example (A || B) is true.

3 ! (logical not)
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a
condition is true then Logical NOT operator will make false.
Example !(A && B) is true.

The Assignment Operators:


There are following assignment operators supported by Java language:
Show Examples
SR.NO Operator and Description

1 =
Simple assignment operator, Assigns values from right side operands to left side
operand.
Example: C = A + B will assign value of A + B into C

2 +=
Add AND assignment operator, It adds right operand to the left operand and assign
the result to left operand.
Example: C += A is equivalent to C = C + A

3 -=
Subtract AND assignment operator, It subtracts right operand from the left operand
and assign the result to left operand.
Example:C -= A is equivalent to C = C - A
4 *=
Multiply AND assignment operator, It multiplies right operand with the left operand
and assign the result to left operand.
Example: C *= A is equivalent to C = C * A

5 /=
Divide AND assignment operator, It divides left operand with the right operand and
assign the result to left operand
ExampleC /= A is equivalent to C = C / A

6 %=
Modulus AND assignment operator, It takes modulus using two operands and
assign the result to left operand.
Example: C %= A is equivalent to C = C % A

7 <<=
Left shift AND assignment operator.
ExampleC <<= 2 is same as C = C << 2

8 >>=
Right shift AND assignment operator
Example C >>= 2 is same as C = C >> 2

9 &=
Bitwise AND assignment operator.
Example: C &= 2 is same as C = C & 2

10 ^=
bitwise exclusive OR and assignment operator.
Example: C ^= 2 is same as C = C ^ 2

11 |=
bitwise inclusive OR and assignment operator.
Example: C |= 2 is same as C = C | 2

Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which
value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false

Data Types
Java defines eight simple (or elemental) types of data: byte, short, int, long,
char, float, double, and boolean. These can be put in four groups:
1. Integers This group includes byte, short, int, and long, which are for
whole valued signed numbers.
2. Floating-point numbers This group includes float and double, which
represent numbers with fractional precision.
3. Characters This group includes char, which represents symbols in a
character set, like letters and numbers.
4. Boolean This group includes boolean, which is a special type for
representing true/false values.
byte:
 Byte data type is an 8-bit signed two's complement integer.
 Minimum value is -128 (-2^7)
 Maximum value is 127 (inclusive)(2^7 -1)
 Default value is 0
short:
 Short data type is a 16-bit signed two's complement integer.
 Minimum value is -32,768 (-2^15)
 Maximum value is 32,767 (inclusive) (2^15 -1)
 Default value is 0
int:
 Int data type is a 32-bit signed two's complement integer.
 Minimum value is - 2,147,483,648.(-2^31)
 Maximum value is 2,147,483,647(inclusive).(2^31 -1)
 Default value is 0
long:
 Long data type is a 64-bit signed two's complement integer.
 Minimum value is -9,223,372,036,854,775,808.(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
 This type is used when a wider range than int is needed.
 Default value is 0L.
float:
 Float data type is a single-precision 32-bit IEEE 754 floating point.
 Float is mainly used to save memory in large arrays of floating point numbers.
 Default value is 0.0f.
 Float data type is never used for precise values such as currency.
 Example: float f1 = 234.5f
double:
 double data type is a double-precision 64-bit IEEE 754 floating point.
 This data type is generally used as the default data type for decimal values, generally the
default choice.
 Double data type should never be used for precise values such as currency.
 Default value is 0.0d.
 Example: double d1 = 123.4
boolean:
 boolean data type represents one bit of information.
 There are only two possible values: true and false.
 This data type is used for simple flags that track true/false conditions.
 Default value is false.
 Example: boolean one = true
char:
 char data type is a single 16-bit Unicode character.
 Minimum value is '\u0000' (or 0).
 Maximum value is '\uffff' (or 65,535 inclusive).
 Char data type is used to store any character.
Variables
The variable is the basic unit of storage in a Java program. A variable is defined by
the combination of an identifier, a type, and an optional initializer. In addition, all
variables have a scope, which defines their visibility, and a lifetime.
Declaring a Variable
In Java, all variables must be declared before they can be used. The basic form of
a variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
Arrays
An array is a group of like-typed variables that are referred to by a common name.
Arrays of any type can be created and may have one or more dimensions. A specific
element in an array is accessed by its index. Arrays offer a convenient means of grouping
related information.
One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed variables. To create an array,
you first must create an array variable of the desired type. The general form of a one
dimensional array declaration is
type var-name[ ];
Here, type declares the base type of the array. The base type determines the data type of
each element that comprises the array. How to allocate memory for arrays? The general
form of new as it applies to one-dimensional arrays appears as follows:
array-var = new type[size];
Here, type specifies the type of data being allocated, size specifies the number of
elements in the array, and array-var is the array variable that is linked to the array.
Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. declare a multidimensional
array variable, specify each additional index using another set of square brackets. For
example, the following declares a two-dimensional array variable called twoD.
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();}}}
This program generates the following output:
01234
56789
10 11 12 13 14
15 16 17 18 19
 Classes
It is the logical construct upon which the entire Java language is built because it
defines the shape and nature of an object. As such, the class forms the basis for
object-oriented programming in Java. Any concept you wish to implement in a
Java program must be encapsulated within a class.
Perhaps the most important thing to understand about a class is that it defines a
new data type. Once defined, this new type can be used to create objects of that
type.
Thus, a class is a template for an object, and an object is an instance of a class.
Because an object is an instance of a class, you will often see the two words object
and instance used interchangeably. A class is declared by use of the class keyword.
The general form of a class definition is shown here
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
The data, or variables, defined within a class are called instance variables. The
code is contained within methods. Collectively, the methods and variables defined
within a class are called members of the class.
Methods
This is the general form of a method:
type name(parameter-list)
{
// body of method
}
Here, type specifies the type of data returned by the method. This can be any valid
type, including class types that you create. If the method does not return a value, its
return type must be void. The name of the method is specified by name. This can
be any legal identifier other than those already used by other items within the
current scope. The parameter-list is a sequence of type and identifier pairs
separated by commas. Parameters are essentially variables that receive the value of
the arguments passed to the method when it is called. If the method has no
parameters, then the parameter list will be empty.

Method Overloading

If a class has multiple methods by same name but different parameters, it is known
as Method Overloading.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any
number of arguments, if you write the method such as a(int,int) for two parameters,
and b(int,int,int) for three parameters then it may be difficult for you as well as
other programmers to understand the behaviour of the method because its name
differs. So, we perform method overloading to figure out the program quickly
Advantage of method overloading
Method overloading increases the readability of the program.
We have created two overloaded methods, first sum method performs addition of
two numbers and second sum method performs addition of three numbers.
class Calculation{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
public static void main(String args[]) {
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Output: 30
40
Method Overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in java.
In other words, If subclass provides the specific implementation of the method that
has been provided by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
 Method overriding is used to provide specific implementation of a method
that is already provided by its super class.

Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).
Example
class Vehicle{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle{
void run(){
System.out.println("Bike is running safely");
}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}
Output: Bike is running safely

There are many differences between method overloading and method overriding in
java. A list of differences between method overloading and method overriding are
given below:

No. Method Overloading Method Overriding


Method overriding is used to provide
Method overloading is used to increase the the specific implementation of the
1)
readability of the program. method that is already provided by its
super class.
Method overriding occurs in two
2) Method overloading is performed within class. classes that have IS-A (inheritance)
relationship.
In case of method overloading, parameter must be In case of method overriding,
3)
different. parameter must be same.
Method overloading is the example of compile time Method overriding is the example of
4)
polymorphism. run time polymorphism.
In java, method overloading can't be performed by
changing return type of the method only. Return type Return type must be same or
5)
can be same or different in method overloading. But covariant in method overriding.
you must have to change the parameter.
Declaring Objects
Obtaining objects of a class is a two-step process.
1. You must declare a variable of the class type. This variable does not define
an object. Instead, it is simply a variable that can refer to an object.
2. You must acquire an actual, physical copy of the object and assign it to that
variable. You can do this using the new operator. The new operator
dynamically allocates (that is, allocates at run time) memory for an object
and returns a reference to it.
This reference is, more or less, the address in memory of the object allocated
by new. This reference is then stored in the variable.
This statement combines the two steps just described. It can be rewritten like this
to show each step more clearly:
Box mybox; // declare reference to object of Box class
mybox = new Box(); // allocate a Box object
Can write like this
Box mybox == new Box();

Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties
and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child
relationship.
Why use inheritance in java
 Method Overriding (so runtime polymorphism can be achieved).
 Code Reusability.
 Code Sharing
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from
an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new
class is called a subclass.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:
Programmer salary is: 40000.0
Bonus of programmer is: 10000

In the above example, Programmer object can access the field of own class as well
as of Employee class i.e. code reusability.

Note: Multiple inheritances is not supported in java through class.

You might also like