Introductior
Java is an Object ~Oriented Programming language. In 1991, James Gosling took
the first step towards the development of Java programming language. Earlier Java
Was called Oak. Then the name Green was used .It was finally named Java in the
year 1995.Java was developed by SUN Microsystem, Most of its syntax has been
derived from C/C++.SUN released the I* JDKI.0 in 1995."Write Once, Run
Anywhere’ was the tagline for Java 1.0.Java is a platform —independent language.
It was secure and allowed network and file - access restrictions.JAVA released to
the market in three categories J2SE (JAVA 2 Standard Edition), J2EE (JAVA 2
Enterprise Edition) and J2ME (JAVA 2 Micro/Mobile Edition).
i, J2SE is basically used for developing client side applications/programs.
ii, JREE is used for developing server side applications/programs.
iil, J2ME is used for developing server side applications/programs.
Features of Java:
1. Simple
.. Object Oriented
. Robust
Secure
i)
Bw
. System Independence
. Portability
. High Performance
. Multithreaded
10.Distributed
11.Architecture Neutral
6
7. Interpreted
8
912.Dynamic
is a simple programming language. This is the design aim of java. When Java
was developed, they wanted it to be simple Java is a simple programming
Tanguage due to two reasons
4) The difficult concepts of C and C++ have been removed in java. For example
the concept of pointers has been completely eliminated from the Java.
b) Java developers maintained the same syntax of C and CH.
Objected iente
Java is an Object-Oriented Programming language. This means Java programs use
objects and classes. An object is anything that really exists in the world, It can be
distinguished from others. Class is used as a model for creating objects.
Robust:
Robust means strong, Java programs are strong .They don’t crash easily like a C or
C+ program. There are two reasons for this
a) Java has got excellent inbuilt exception handling features,
b) Memory Management features.
Secure:
Security problems can be eliminated by using Java on Internet,
System independence:
Java's byte code is not machine dependent. It can be run on any machine with any
processor and any operating system.
Portability :
Ifa program gives the same result on every machine then that program is called
Portable.Java programs are portable.This is the result of java’s system
independence nature.Interpreted:
Java programs are compiled to generate the byte code.This byte code can be
interpreted by the interpreter in JVM.If we take any other language only an
interpreter or a compiler is used to execute the program.
High Performance:
The problem with interpreter inside the JVM is that it is slow. Because of this,
Java programs used to run slow.To solve this problem Java developers have
introduced JIT(Just in time) compiler.{t enhances the speed of execution,
Multithreaded:
A thread represents an individual process to execute a group of statements, Jvm
uses several threads to execute different blocks of code.Creating multiple threads
is called mutithreaded.
Information is distributed on the various computers on a network.Using Java we
can write program that can be capture information and distribute it to the
clients.Java can handle TCP/IP and UDP Protocols.
Architecture Neutral:
When Java program compile it creats the java byte code.This byte code is
architecture neutral.
Dynami
Java is a Dynamic language.tava is capable of dynamically linking in new class
libraries,methods,and Objects.How Java Program Run:
Byte Code(abe.class)
Output(abe.obj)
Source Code(abe,java)
Java Interpreter
Java Compiler >}
Byte Code:
This code is actually platform independent. We can consider it as a machine code.
When we compile an anyname.Java file successfully. We can get this code. Itis in
the form of anyname.class file.
Virtual Machin
‘The name virtual suggests that this isnot an actual machine. Its just a program
‘hich simulates a machine on which java programs are supposed to run. Actually,
Wwe can suppose that is a hardware which runs programs in Bytecode.
How complie and run the java program:
Following steps we follow to compile and run the java program:
1. Open the notepad. Write the java code in this notepad file and save it with
the name abe,java in our destop.
2. Open the command prompt. Write the following commands
C>ed\
Ced Java
CNava>ed bin
CAljava\bin>javac abe javaNow goto your MYCOMPUTER right click on that and select
PROPERTIES.
Afier selecting properties you see a page of System .Go to left of page where
you see a link ADVANCED SYSTEM SETTING click on it.
+
5. you will see a box.
6. Click on ENVIRONMENT VARIABLE.
7. Click on New on User Variable.
8. Now Write PATH on Variable Name
9. Path Value - Paste the path of your javac that is
10.C:\Program Files\Java\jdk1.7.0_02\bin
click ok in all steps
12.now come to command prompt .Go to location where you save your file
13.1 saved program in Desktop .To change path you can use "ed" command
14.now type "javac test,java" this compile your program and class file is
created. Here test,java is file .You can use javac .File name will
be name of your file
15. You program is compiled now If there is any error than your can see error
message on screen.
16. To run your program now write "java " class name will be the
name of class which contain "main function body"/* Print Hello World*/
class HelloWorld
public static void main(String args[])
{
System.out println("Hello World");
}
}
Output:
Dava>java HelloWorld
Hello World
/* Enter a number and check whether it is an Armstrong number or not */
import java.util.*;
class ArmstrongNumber
( public static void main(String args[])
int n, sum = 0, temp, r;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check if it is an
armstrong number"
n= in.nextInt
temp =n;
while( temp != 0)
{
r= temp%10;
sum = sum + rrr;
temp = temp/10;if( sum )
System.out.printin("Entered number is an armstrong number.");
else
System.out.printIn("Entered number is not an armstrong
number,");
}
}
Output:
D:Vava>javac ArmstrongNumber.java
D:\ava>java ArmstrongNumber
Enter a number to check if it is an armstrong number
153,
Entered number is an armstrong number
/*Program to check the entered number is even or odd*/
import java.util. Scanner;
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.printIn("Enter an integer to check if it is odd or even");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if(x%2=0)
System.out.printIn(""You entered an even number.");
else
System.out.printIn("You entered an odd number.
}
}Oar
DAavarjavac OddOrEVen java
DMavarjava OddOrEven
Enter an integer to cheek ifit is odd or even
You entered an odd number,
DAava>
™ Java program to check palindrome */
import javanutil*;
class Palindrome
i
public static void main(String args{])
‘
String original, reverse
Scanner in = new Scanner(System.in);
System-out printin("Enter a string to check if isa palindrome");
original = in.nextLineQ:
int length
original length);
for (inti
revers
length = 1:i>=0; i+)
reverse + original.charAt(i);
if (original.equals(reverse))
System.out.printin("Entered string is a palindrome,
else
System.out.printin("Entered string is not a palindrome.
Output:D:\Vava>java Palindrome
Enter a string to check if it is a palindrome
ara
Entered string is a palindrome.
/* Java program to find factorial */
import java.util. Scanner;
class Factorial
{
Public static void main(String args(])
int n, c, fact = 1;
System.out println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n= in.nextInt(;
if(n<0)
System.out.printIn("Number should be non-negative.");
else
{
for(c=1;c<=n;ctt)
fact = fact*c;
System.out.println("Factorial of "tn+" is = "+fact);
D:MJava>javae Factorial,java
D\ava>java Factorial
Enter an integer to calculate it's factorial
Factorial of 5 is = 120