intro of java
intro of java
Introduction of Java.
C lang developed by Dennies Ritchie in 1972. It developed at AT&T Bell
Laboratories, New Jursey. USA. C lang was focus on POP.
Initially this langauge is called 'Oak' Language. Because Java lang was
developing like 'Oak' tree.(named after the Oak trees outside Gosling office).
In 1995, It rename 'Java'. why put 'java' name because, Java is Very
Popular Hot Drink in USA. You can learn this language like hot drinks.
Addition.java
import java.io.*; //karan BufferedReader cls ya package madhye aahe.
class addition
{
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\n Enter the two number --->");
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
int c=a+b;
System.out.println("\n Addition ="+c);
}
}
-----------------------------------------------------------------------------
sub.java
import java.io.*; //karan BF cls ya package madhye aahe.
class sub
{
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\n Enter the two number --->");
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
int c=a-b;
System.out.println("\n Subtraction ="+c);
}
}
-----------------------------------------------------------------------------------
-----------
WAP accept student details and display
import java.io.*;
class student
{
public static void main(String args[]) throws IOException
{
System.out.println("Student Rollno="+rno);
System.out.println("Student Name ="+snm);
System.out.println("Percentage ="+per);
}
}
-----------------------------------------------------------------------------------
--------
WAP accept radius and find area of circle
import java.io.*;
class circle
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the Radius--->");
int r= Integer.parseInt(br.readLine());
double area=3.14*r*r;
System.out.println("\n Area of Circle= "+area);
}
}
-----------------------------------------------------------------------------------
-----
Homeworks
1) WAP accept employ details and display
2) WAP find circumference of circle
3) WAP to find volume of cylinder
4) WAP to find area of tringle.
-----------------------------------------------------------------------------------
------
i) Simple
ii) Object Oriented
iii) Distrubuted
iv) Robust
v) Secure
vi) Architecture Neutral
vii) Portable
viii) Interpreted
ix) Multithreaded
x) Dynamic
xi) High Performance.
i) Simple
Java is simple lang compared to c, and C++,Java has omitted complex and
confusing features of C,C++,
like preprocesser, operator overloading, multiple inheritance,
pointers,template etc.
If programmer is familier with C and C++, then he can learn java easily.
hence java is simple lang.
iii) Distributed.
java program can be distributed and accessed over the network, Java
Supports protocol like TCP/IP,
UTP and FTP. java application can access objects using URL's and invoke
remote methods
using RMI. java supported distributive concept.
iv) Robust.
Major causes of failures are runtime errors and memory mismanagements.
java is strongly
supported typed language. type checking is carried out at both
compile and runtime.
Java uses automatic garbage collection, which manage memory by
preventing memory leaks.
v) Secure.
java used in network enviornments, hence security is an important
concorn. Java enforces
security by restricting access to resouces to trusted program only.
By eliminating pointers, java ensures the user cannot access and
modify data in memory.
Java support four access specifier, public, private, protected, and
default. hence it secure data.
viii) Interpreted
The Java compiler compiles the java source code into java bytecode,
which is executable for the java virtual machine.
The bytecode is then interpreted by a java interpreter which converts
the bytecode into platform specific code and executes its.
ix) MultiThreaded
Two or more thread (small program) can be run simultanously/concurrently
in a system is called multithreading.. Java support Multithreading concept.
Because CPU cannot seat idle. It's Utilies CPU Idle time and perform
some clean up action and general system maintenance.
x) Dynamic
Java is designed to adapt to an ever changing enviornment. During the
execution of a program. java can dynamically load required classes
thereby reducing overheads and imporving performance.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
import java.io.*; //karan BF cls ya package madhye aahe.
class addition
{
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter the two number --->");
int a=Integer.parseInt(br.readLine()); //String che conversion he
integer madhye karnyasathi Integer.parseInt() use keele jate
int b=Integer.parseInt(br.readLine());
int c=a+b;
System.out.println("\n Addition ="+c);
}
}
-----------------------------------------------------------------------------------
-----
import java.io.*; //karan BF cls ya package madhye aahe.
class sub
{
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter the two number --->");
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
int c=a-b;
System.out.println("\n Subtraction ="+c);
}
}
-----------------------------------------------------------------------------------
------
//WAP to accept n numbers in array and display
import java.io.*;
class arr1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int A[]=new int[5]; //array syntax datatype arrnam[]=new
datatype[size];
System.out.println("Student Rollno="+rno);
System.out.println("Student Name ="+snm);
System.out.println("Percentage ="+per);
}
}
-----------------------------------------------------------------------------------
---------------------------------------
Comparison or difference between C/C++ and Java
-----------------------------------------------------------------------------------
-------------------------------------------------------------
C/C++
| Java
-----------------------------------------------------------------------------------
-------------------------------------------------------------
1) hybrid between procedural / Object Oriented lang | 1) Java is Pure
Object Oriented Language.
Combination of POPs and OOPs |
-----------------------------------------------------------------------------------
-------------------------------------------------------------
2) Can Have Stand alone methods | 2)
All Methods are part of class
-----------------------------------------------------------------------------------
-------------------------------------------------------------
3) Platform Depedence |
3) Platform Independance
-----------------------------------------------------------------------------------
-------------------------------------------------------------
4) Architecture Specific
| 4) Architecture Neutral
-----------------------------------------------------------------------------------
-------------------------------------------------------------
5) Does support Multiple Inheritance | 5)
Doesnot Support Multiple Inheritance
-----------------------------------------------------------------------------------
-------------------------------------------------------------
6) Does support Pointer |
6) Does not Support Pointers.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
7) It support Default Arguments |
7) Doesnot support Default Arguments.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
8) Does support template, operator overloading | 8) Doesnot
Support template, operating overloading, enum sturcture etc
-----------------------------------------------------------------------------------
-------------------------------------------------------------
9) Only virtual function are dynamic bounds. | 9) All
methods are dynamic bounds.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
10) Templates as parameterized types | 10) No
Parameterized types.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
11) Array references translate to pointer arithmetic | 11) Array reference
are not translated pointer arithmetic.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
12) No Bound checking in array | 12)
Bound Checking in array.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
13) String are null ternminated chars arrays. | 13) String
are objects.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
14) NO Automatic Garbage Collector | 14) Automatic
Garbage Collector
-----------------------------------------------------------------------------------
-------------------------------------------------------------
15) Compiled language | 15)
Compiler and interpreter language.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
16) Doesnot support interface | 16)
It support Interface.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
17) Doesnot Support Multithreading | 17) Does
Support Multithreading
-----------------------------------------------------------------------------------
------------------------------------------------------------
18) Does support Preprocessor | 18)
Doesnot support Preprocessor.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
19) Support SystemBased Application | 19) Support
SystemBased & Web Based Application
-----------------------------------------------------------------------------------
-------------------------------------------------------------
20) It support structure & enum. | 20) It
Doesnot support structure & enum.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
21) Main function can have return a value | 21) Main Methods
cannot have a return value.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
Java Enviroments.
Java is not just a programming lang, it is a complete technology.
The Java Technology contains
i) A programming language.
ii) A developement environments
iii) An Application Environments
iv) A deployment Environments
v) Integration Environments.
---------------------------------------------------------------------------------
Java Tools.
i) javac : The Java Compiler, It converts java source code into
bytecode (classfile).
ii) java : java interpreter, Convert bytecode into machine code.
iii) javadoc : java documentation (API). The java commenting tool,
Creates HTML help file from java source code.
iv) jdb : The java debugger, Steps through programs as they
executed,
sets breakpoints, and monitor behavior.
v) javap : Disassembling classes, It show lists of methods and
class members.
e.g javap java.io.BufferedReader
vi) javah : Header File Generator, Used when combining java with
C/C++ programs.
vii) jar : Java Archieve File(JAR) manager, Combines and
compresses multiple files and directories into one.
viii) appletviewer : The java applet launcher.. it is used for run applet
programming.
-----------------------------------------------------------------------------------
-----------------------------------------
Java Editors ..
A java program is a simple text which can be typed using any standard editor like,
notepad, textpad, eclipse, gedit, vi editor, Textedit, NetBeans, Editplus,
JCreator, JBoss, Notepad++,
JDeveloper, DrJava, Greenfoot, JGrasp, JEdit, BlueJ, intellJ etc ....
The IDE's provide their own editors, these editor provide additional
features such as
keyword, highlighting, automatic indentation etc.
-----------------------------------------------------------------------------------
----------------------------------------------
//simple program
class demo
{
public static void main(String args[])
{
System.out.println(" Good Morning...");
}
}
String args[] : String args[] array. accept input from commandline arguments.
-----------------------------------------------------------------------------------
------------------------
System.out.println()
System is class and out is object. This methods displays the given string on
the output device.
It also displays a new line characters after the string.
System.out is a predefine object which represent the output stream.
-----------------------------------------------------------------------------------
// WAP to accept book details and display.
import java.util.*;
class book
{
Scanner s1=new Scanner(System.in);
int bno;
String bnm;
double price;
void accept()
{
System.out.println("\n Enter the book no, book name and price :---
>");
bno= s1.nextInt();
bnm=s1.next();
price=s1.nextDouble();
}
void display()
{
System.out.println("\n BookNo="+bno+" \t Book Name="+bnm+"\t Book
Price="+price);
}
public static void main(String args[])
{
book b1=new book(); //obj create kela.
book b2=new book();
b1.accept(); b2.accept(); //calling
b1.display(); b2.display();
}
}
-----------------------------------------------------------------------------------
Command line Arguments.
Recall the defination of the main method, here main is defined as a method which
accept an array of strings as commond line arguements. These are arguments
passed to the main function when it start executing. Input values can be passed
to main method from command line and accessed in the code.
Syntax
java programname value1 value2
value3 ........................ valueN
}
}
-----------------------------------------------------------------------
WAP to accept 'n' numbers from command line arguments and find sum.
class args3
{
public static void main(String args[])
{
int n=args.length;
int sum=0;
for(int i=0; i<n; i++)
{
sum=sum+Integer.parseInt(args[i]);
}
System.out.println("\n Addition ="+sum);
}
}
----------------------------------------------
//WAP to accept 'n' numbers from command line arguments and find maximum
number from its.
class args4
{
public static void main(String A[])
{
int n=A.length;
int max=0;
for(int i=0; i<n; i++)
{
if(max< Integer.parseInt(A[i]))
{
max=Integer.parseInt(A[i]);
}
}
System.out.println("\n Maximum elements ="+max);
}
}
------------------------------------------------------------------------------
//WAP to accept number from command line arguments and find factorial
class args5
{
public static void main(String A[])
{
int n=Integer.parseInt(A[0]);
int f=1;
System.out.print("\n Factorial of ="+n+" is--->");
while(n>1)
{
f=f*n;
n--;
}
System.out.print(f);
}
}
--------------------------------------------------------------------------------
//WAP to accept 'n' numbers from command line arguments and count positive and
negative numbers.
class args6
{
public static void main(String A[])
{
int n=A.length;
int p=0, ng=0;
for(int i=0; i<n; i++)
{
int x= Integer.parseInt(A[i]);
if(x>0)
p++;
else
ng++;
}
System.out.println("\n No of positive number ="+p+"\n No of negative
number="+ng);
}
}
-------------------------------------------------------
WAP to accept n numbers from commandline arguments and count even and odd numbers.
-----------------------------------------------------------------------------------
----------------------------------
//WAP to accept n numbers in array and display
import java.io.*;
class arr1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int A[]=new int[5]; //array syntax datatype arrnam[]=new
datatype[size];
-----------------------------------------------------------------------------------
----
//Find Volume of cylinder
import java.util.*;
class cylinder
{
public static void main(String args[])
{
float pi=3.14f;
Scanner s1 = new Scanner(System.in);
System.out.println("\n Enter the radius and height :->");
float r= s1.nextFloat();
double h = s1.nextDouble();
double dhano=pi*r*r*h;
System.out.println("\n Volume of Cylinder ="+dhano);
}
}
-----------------------------------------------------------------------------------
---------
//WAP to accept Book Details and display
import java.util.*;
class book
{
public static void main(String args[])
{
Scanner s1 = new Scanner(System.in);
System.out.println("\n Enter the Bookid ,Bookname, and Price :->");
int bid = s1.nextInt();
String bnm = s1.next();
double price=s1.nextDouble();
System.out.println("\n Book id="+bid+"\t Book Name="+bnm+"\t
Price="+price);
}
}
-----------------------------------------------------------------------------------
-------------------------
//WAP to accept n numbers from command line args and count no of even or odd
number
class argssum
{
public static void main(String N[])
{
int odd=0,even=0;
for(int i=0; i < N.length ; i++)
{
int n =Integer.parseInt(N[i]);
if(n%2==0)
even++;
else
odd++;
}
System.out.println("\n Even no -->"+even+"\t Odd number --->"+odd);
}
}
-----------------------------------------------------------------------------------
-----
//WAP to print factor of number by using commandline args...
class argfactor
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
System.out.println("\n Factor of "+n+ " are ---->");