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

The bytecodes streams are stored in the method area of the JVM.The Java Runtime Environment (JRE) is a set of software tools for development of Java applications. including Windows OS and Linux. 2. JRE was originally developed by Sun Microsystems Inc. called "bytecode. the source language statements are compiled by the Java compiler into bytecode rather than into code that contains instructions . but can be downloaded separately. 5. When a JVM loads a class file. This binary format. After you've written a Java program.needed in Java development." A JVM is a software-based machine that runs Java programs.CLASS file before execution to improve performance.Bytecodes are the machine language of the Java virtual machine.In the Java programming language and environment. It combines the Java Virtual Machine (JVM).Bytecode:. or any other technique that was chosen by the designer of a particular JVM. it gets one stream of bytecodes for each method in the class. the Java code from a .JVM:. The bytecodes for a method are executed when that method is invoked during the course of running the program. 4. JRE is part of the Java Development Kit (JDK).Stands for "Java Virtual Machine. A Java virtual machine processes instructions similar to a physical processor.JAVA file must first be converted into instructions the JVM can understand. just-in-time compiling. It can be installed on several different operating systems. However. They can be executed by intepretation. platform core classes and supporting libraries." can be processed one instruction at a time or compiled into a .JIT compiler:..JRE:. 3. a wholly- owned subsidiary of Oracle Corporation. 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. JVMs allow Java apps to run on almost any computer.

Write Once and Run Anywhere(WORA). Mac/OS etc. Sun Solaris. Abstraction 6. Java code is compiled by the compiler and converted into bytecode.g. This bytecode is a platform- independent code because it can be run on multiple platforms i. operator overloading etc. Features OF Java There is given many features of java.that match a particular hardware platform's processor (for example. Windows. 2. They are also known as java buzzwords. Basic concepts of OOPs are: 1. Object 2.object-oriented:- Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behaviour.e. explicit pointers.. Encapsulation 3.Java code can be run on multiple platforms e.g. . Polymorphism 5. The bytecode is platform-independent code that can be sent to any platform and run on that platform. No need to remove unreferenced objects because there is Automatic Garbage Collection in java. Class 3. Java language is simple because: syntax is based on C++ (so easier for programmers to learn it after C++). Linux.Simple:.According to Sun. 1.Platform Independent:. Inheritance 4. removed many confusing and/or rarely-used features e. Object-oriented programming(OOPs) is a methodology that simplify software development and maintenance by providing some rules. an Intel Pentium microprocessor or an IBM System/390 processor).

We may carry the java bytecode to any platform. The main advantage of multi-threading is that it doesn't occupy memory for each thread. There is automatic garbage collection in java. it occupies 4 bytes of memory for both 32 and 64 bit architectures.A thread is like a separate program.C++ is platform dependent whereas JAVA is . Web applications etc. Architecture-neutral:. Portable:. Threads are important for multi-media. int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit architecture. There are lack of pointers that avoids security problem. Java uses strong memory management.Robust:.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 . All these points makes java robust. Secured:.but java is slightly different from c++ in following ways 1. executing concurrently. Multi-threaded:. There is exception handling and type checking mechanism in java. It shares a common memory area. Robust simply means strong. But in java. We can write Java programs that deal with many tasks at once by defining multiple threads.In C programming.c++ is designed to be compiled for a specific target means there is no portability for c++ program.

} 5. } save this file as Simple. 2. class Simple{ 2. 4.java To execute: java Simple Output: Hello Java . 4. platform independent.out.java To compile: javac Simple.println("Hello Java").C++ supports operator overloading but java don’t. Simple program in java Let's create the hello java program 1.C++ is compiler based programming language whereas JAVA is both comiler and intrertreter based programming language. 3. System. public static void main(String args[]){ 3.C++ supports pointer but JAVA don’t.

0d 8 byte .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.

When a Class extends another class it inherits all non-private members including fields and methods.salary).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.println("Programmer salary is:"+p. also known as Super class(Parent) and Sub class(child) in Java language. System. Inheritance provided mechanism that allowed a class to inherit property of another class. public static void main(String args[]){ Programmer p=new Programmer(). } class Programmer extends Employee{ int bonus=10000. . Inheritance in Java can be best understood in terms of Parent and Child relationship.out. Extend Keyword:.Inheritance in JAVA Inheritance is one of the key features of Object Oriented Programming. Syntax: class Subclass-name extends Superclass-name { //methods and fields } Example: class Employee{ float salary=40000.

bonus).0 Bonus of Programmer is:10000 Types of Inheritance in Java . System.println("Bonus of Programmer is:"+p.out. } } OUTPUT Programmer salary is:40000.

B and C are three classes.B{//suppose if it were Public Static void main(String args[]){ C obj=new C().out. java renders compile time error if you inherit 2 classes.} } class B{ void msg(){System. class A{ void msg(){System. The C class inherits A and B classes. multiple inheritance is not supported in java.msg(). there will be compile time error now. obj.println("Hello"). Consider a scenario where A.//Now which msg() method would be invoked? } } OUTPUT Compile time error . If A and B classes have same method and you call it from child class object.} } class C extends A. Since compile time errors are better than runtime errors.Q) Why multiple Inheritance is not supported in java? To reduce the complexity and simplify the language. So whether you have same method or different.out.println("Welcome"). there will be ambiguity to call method of A or B class.

a[2]=70. } } Output: 10 20 70 40 50 .i<a. a[4]=50.length.//initialization a[1]=20. //printing array for(int i=0.i++)//length is the property of array System.println(a[i]).//declaration and instantiation a[0]=10.Write a program in java using single-dimensional array class Testarray{ public static void main(String args[]){ int a[]=new int[5].out. a[3]=40.

//new line } } } Output: 268 6 8 10 .{3. } System.4}.{3.5}}. //adding and printing addition of 2 matrices for(int i=0. //creating another matrix to store the sum of two matrices int c[][]=new int[2][3].out.3.Write a program for addition of 2 matrices in java class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1.j<3.out.4. System.println().j++){ c[i][j]=a[i][j]+b[i][j].i++){ for(int j=0.4}.3. int b[][]={{1.5}}.print(c[i][j]+" ").i<2.4.

println("running safely...out. Write a simple example of abstract class. . } class Honda4 extends Bike{ void run(){System.  If a class is declared abstract.run()."). methods without body ( public void get(). obj.} public static void main(String args[]){ Bike obj = new Honda4().e.. it cannot be instantiated.  Abstract classes may or may not contain abstract methods. abstract class Bike{ abstract void run(). provide implementations to the abstract methods in it. you have to inherit it from another class.Abstract class A class which contains the abstract keyword in its declaration is known as abstract class. } } OUTPUT running safely. )  But. then the class mustbe declared abstract. i. if a class has at least one abstract method.  To use an abstract class.

System.Scanner. System. s = in.println("You entered float "+b).Write a program in java to take input from user.util.23 You entered float 1. import java. String s. System.println("You entered integer "+a).nextInt().println("Enter a string"). Scanner in = new Scanner(System. } } OUTPUT Enter a string Hello Java You entered string Hello Java Enter an integer 123 You entered integer 123 Enter a float 1.println("Enter a float").out.nextLine().out.in). class GetInputFromUser { public static void main(String args[]) { int a.out. System. b = in.out.nextFloat().out. float b. System.println("Enter an integer").23 .out.println("You entered string "+s). System. a = in.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.