Q. What is java?

Java is general purpose ,high level ,device independent programming
language. Developed in 1995 by James Gosling of Sun Microsystems specifically
for network-heavy environments such as internet and enterprise intranets), it
is a major part of the information infrastructure being developed all over the
world. Like the C++ language (on which it is based) Java is object oriented:
meaning its programs are built with 'modules' of code which can be employed
in building new programs without rewriting the same code.

History of Java
In the early 90s, extending the power of network computing to the activities of
everyday life was a radical vision. In 1991, a small group of Sun engineers
called the "Green Team" believed that the next wave in computing was the
union of digital consumer devices and computers. Led by James Gosling, the
team worked around the clock and created the programming language that
would revolutionize our world – Java.
The Green Team demonstrated their new language with an interactive,
handheld home-entertainment controller that was originally targeted at the
digital cable television industry. Unfortunately, the concept was much too
advanced for the them at the time. But it was just right for the Internet, which
was just starting to take off. In 1995, the team announced that the Netscape
Navigator Internet browser would incorporate Java technology.
Today, Java not only permeates the Internet, but also is the invisible force
behind many of the applications and devices that power our day-to-day lives.
From mobile phones to handheld devices, games and navigation systems to e-
business solutions, Java is everywhere!

Terms related to Java
1.JDK:- The Java Development Kit (JDK) is a software development
environment used for developing Java applications and applets. It includes the
Java Runtime Environment (JRE), an interpreter/loader (java), a compiler
(javac), an archiver (jar), a documentation generator (javadoc) and other tools
needed in Java development.

2.JRE:- The Java Runtime Environment (JRE) is a set of software tools for
development of Java applications. It combines the Java Virtual Machine (JVM),
platform core classes and supporting libraries.

JRE is part of the Java Development Kit (JDK), but can be downloaded
separately. JRE was originally developed by Sun Microsystems Inc., a wholly-
owned subsidiary of Oracle Corporation.

3.JVM:- Stands for "Java Virtual Machine." A JVM is a software-based
machine that runs Java programs. It can be installed on several
different operating systems, including Windows OS and Linux. JVMs allow
Java apps to run on almost any computer.
A Java virtual machine processes instructions similar to a physical processor.
However, the Java code from a .JAVA file must first be converted into
instructions the JVM can understand. This binary format, called "bytecode,"
can be processed one instruction at a time or compiled into a .CLASS file before
execution to improve performance.

4.Bytecode:- Bytecodes are the machine language of the Java virtual
machine. When a JVM loads a class file, it gets one stream of bytecodes for
each method in the class. The bytecodes streams are stored in the method
area of the JVM. The bytecodes for a method are executed when that method
is invoked during the course of running the program. They can be executed by
intepretation, just-in-time compiling, or any other technique that was chosen
by the designer of a particular JVM.

5.JIT compiler:- In the Java programming language and environment, a
just-in-time (JIT) compiler is a program that turns Java bytecode (a program
that contains instructions that must be interpreted) into instructions that can
be sent directly to the processor. After you've written a Java program, the
source language statements are compiled by the
Java compiler into bytecode rather than into code that contains instructions
that match a particular hardware platform's processor (for example, an
Intel Pentium microprocessor or an IBM System/390 processor). The bytecode
is platform-independent code that can be sent to any platform and run on that

Features OF Java
There is given many features of java. They are also known as java buzzwords.

1.Simple:- According to Sun, Java language is simple because: syntax is
based on C++ (so easier for programmers to learn it after C++). removed
many confusing and/or rarely-used features e.g., explicit pointers, operator
overloading etc. No need to remove unreferenced objects because there is
Automatic Garbage Collection in java.

Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behaviour.
Object-oriented programming(OOPs) is a methodology that simplify
software development and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation

3.Platform Independent:- Java code can be run on multiple
platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc. Java code is compiled
by the compiler and converted into bytecode. This bytecode is a platform-
independent code because it can be run on multiple platforms i.e. Write Once
and Run Anywhere(WORA).
Robust:- Robust simply means strong. Java uses strong memory
management. There are lack of pointers that avoids security problem. There is
automatic garbage collection in java. There is exception handling and type
checking mechanism in java. All these points makes java robust.

Architecture-neutral:- In C programming, int data type occupies 2
bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit
architecture. But in java, it occupies 4 bytes of memory for both 32 and 64 bit

Portable:- We may carry the java bytecode to any platform.

Multi-threaded:- A thread is like a separate program, executing
concurrently. We can write Java programs that deal with many tasks at once
by defining multiple threads. The main advantage of multi-threading is that it
doesn't occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications etc.

Secured:- Java is secured because:
o No explicit pointer
o Java Programs run inside virtual machine sandbox

C++ vs JAVA
Both c++ and java are object oriented programming language ,but java is
slightly different from c++ in following ways

1.c++ is designed to be compiled for a specific target means there is no
portability for c++ program.C++ is platform dependent whereas JAVA is
platform independent.

2.C++ is compiler based programming language whereas JAVA is both comiler
and intrertreter based programming language.

3.C++ supports pointer but JAVA don’t.

4.C++ supports operator overloading but java don’t.

Simple program in java

Let's create the hello java program

1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }

save this file as Simple.java

To compile: javac Simple.java
To execute: java Simple

Output: Hello Java
Data types in Java

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
Inheritance in JAVA

Inheritance is one of the key features of Object Oriented Programming.
Inheritance provided mechanism that allowed a class to inherit property of
another class. When a Class extends another class it inherits all non-private
members including fields and methods. Inheritance in Java can be best
understood in terms of Parent and Child relationship, also known as Super
class(Parent) and Sub class(child) in Java language.

Extend Keyword:- The extends keyword indicates that you are making a
new class that derives from an existing class. The meaning of "extends" is to
increase the functionality.

class Subclass-name extends Superclass-name
//methods and fields

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);



Programmer salary is:40000.0
Bonus of Programmer is:10000

Types of Inheritance in Java
Q) Why multiple Inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.

Consider a scenario where A, B and C are three classes. The C class inherits A
and B classes. If A and B classes have same method and you call it from child
class object, there will be ambiguity to call method of A or B class.

Since compile time errors are better than runtime errors, java renders compile
time error if you inherit 2 classes. So whether you have same method or
different, there will be compile time error now.

class A{

void msg(){System.out.println("Hello");}


class B{

void msg(){System.out.println("Welcome");}


class C extends A,B{//suppose if it were

Public Static void main(String args[]){

C obj=new C();

obj.msg();//Now which msg() method would be invoked?



Compile time error
Write a program in java using single-dimensional array

class Testarray{
public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation

//printing array
for(int i=0;i<a.length;i++)//length is the property of array


Write a program for addition of 2 matrices in java

class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];

//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
System.out.print(c[i][j]+" ");
System.out.println();//new line

Output: 268
6 8 10
Abstract class

A class which contains the abstract keyword in its declaration is known as
abstract class.

 Abstract classes may or may not contain abstract methods, i.e., methods
without body ( public void get(); )

 But, if a class has at least one abstract method, then the class mustbe
declared abstract.

 If a class is declared abstract, it cannot be instantiated.

 To use an abstract class, you have to inherit it from another class,
provide implementations to the abstract methods in it.

Write a simple example of abstract class.

abstract class Bike{
abstract void run();

class Honda4 extends Bike{
void run(){System.out.println("running safely..");}

public static void main(String args[]){
Bike obj = new Honda4();

running safely..
Write a program in java to take input from user.

import java.util.Scanner;

class GetInputFromUser
public static void main(String args[])
int a;
float b;
String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);

System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);

System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);

Enter a string
Hello Java
You entered string Hello Java

Enter an integer
You entered integer 123
Enter a float
You entered float 1.23

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times