Javafile
Javafile
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995 and
later acquired by Oracle Corporation. It is a simple programming language. Java
makes writing, compiling, and debugging programming easy. It helps to create
reusable code and modular programs. Java is a class-based, object-oriented
programming language and is designed to have as few implementation dependencies
as possible. A general-purpose programming language made for developers to write
once run anywhere that is compiled Java code can run on all platforms that support
Java. Java applications are compiled to byte code that can run on any Java Virtual
Machine. The syntax of Java is similar to c/c++.
In 1997, Sun Microsystems approached the ISO standards body and later formalized
Java, but it soon withdrew from the process. At one time, Sun made most of its Java
implementations available without charge, despite their proprietary software status.
Sun generated revenue from Java through the selling of licenses for specialized
products such as the Java Enterprise System.
Characteristics of Java
1. Object-Oriented
2. Portable
3. Platform independent
4. Secured
5. Robust
6. Architecture neutral
7. Interpreted
8. High Performance
9. Multithreaded
10. Distributed
11. Dynamic
12. Simple
Structure
Documentation Section
Package Statement
Import Statements
Interface Statement
Class Definition
Main Method Class
o Main Method Definition
Advantages
Straightforward Java –
It is anything but difficult to program, compose, gather, investigate, and
learn than elective programming dialects. Java might be a more modest sum
convoluted than C++; therefore, Java utilizes programmed memory portion
and trash assortment.
Item Oriented –
It grants you to make standard projects and reusable code.
Stage Independent Java code –
It runs on any machine that needn’t bother with any unique programming to
be introduced, however, the JVM should be available on the machine.
Java is a disseminated language –
Java is a dispersed language as it gives an instrument for dividing
information and projects between numerous PCs that improve the
presentation and proficiency of the framework. The RMI(Remote Method
Invocation) is something that bolsters the dispersed handling in Java.
Secure Java –
It has no unequivocal pointer. Besides this, it is a security administrator that
characterizes the entrance of classes.
Memory distribution –
In Java, memory is part into two sections one is stored and another is stack.
At whatever point we pronounce a variable JVM gives memory from one or
the other stack or pile space. It assists with remaining the information and
reestablish it without any problem.
Multithreaded –
It is the potential for a program to perform numerous assignments
simultaneously. at long last time showed up to become familiar with the
ideas of Multithreading in Java.
Disadvantages
class Main {
public static void main(String[] args) {
import java.util.Scanner;
class AreaOfCircle
double r= s.nextDouble();
double area=(22*r*r)/7 ;
import java.io.*;
class Addition {
}
4.) Program to show the concept of conditional operator
public classConditionalOperatorExample
class Main {
System.out.println(!(5 == 3));
import java.io.*;
import java.util.Scanner;
class GFG {
if (num % 2 == 0) {
}else {
}}}
else
import java.util.Scanner;
class Student_Division
{
Scanner input = new Scanner(System.in);
int m1 = input.nextInt();
int m2 = input.nextInt();
int m3 = input.nextInt();
int m4 = input.nextInt();
int m5 = input.nextInt();
System.out.println("Total :"+tot);
System.out.println("Percentage :"+per);
if(per>=60)
System.out.println("First Division.");
System.out.println("Second Division.");
}
else if(per>=40 && per<=49)
System.out.println("Third Division.");
else{
System.out.println("Fail.");
}}
int day = 5;
String dayString;
switch (day) {
case 1:
dayString = "Monday";
break;
case 2:
dayString = "Tuesday";
break;
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
}System.out.println(dayString);
}
class FibonacciExample1{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
} }}
int num = 6;
long factorial = 1;
// factorial = factorial * i;
factorial *= i;
class GFG {
{
int N = 10;
int sum = 0;
sum += i;
System.out.println();
int l=8;
int b=8;
double area=l*b;
}}
import java.util.Scanner;
int num=sc.nextInt();
}
}
String firstName;
String lastName;
int age;
public Student(){
firstName = "Rocky";
lastName = "Bhai";
age = 20; }
System.out.println(myStudent.firstName);
System.out.println(myStudent.lastName);
System.out.println(myStudent.age);
} }
17.) Program to show the concept of parametrize
constructor
import java.io.*;
class Geek {
String name;
int id;
{ this.name = name;
this.id = id;
}class GFG {
}
18.) Program to show the concept of copy Constructor
fprice = fPrice;
fname = fName;
Fruit(Fruit fruit)
fprice = fruit.fprice;
fname = fruit.fname;
double showPrice()
{
return fprice;
String showName()
return fname;
}
19.) Program to show the concept of constructor
overloading
public class Student {
int id;
String name;
Student(){
id = i;
name = n;
//object creation
"+student.name);
class main {
objB.print();
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
23.) Program to use of interface
interface Polygon {
void getArea(int length, int breadth);
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
24.) Program to use of multiple interface
interface Inf1 {
void sayHello1();
}
interface Inf2 {
void sayHello2();
}
interface Inf3 {
void sayHello3();
}
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occ
urs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
28.) Program to illustrate the use of packages
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
t.start();
String str = t.getName();
System.out.println(str);
}
}
t1.start();
System.out.println("after starting thread isAlive: "+t1.isAlive());
}
}
class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
class main
{
public static void main(String args[]) {
char ch = 'A';
} }}
37.) Program to illustrate the use of
PushBackInputStream class
import java.io.*;
public class InputStreamExample {
public static void main(String[] args)throws Exception{
String srg = "1##2#34###12";
byte ary[] = srg.getBytes();
ByteArrayInputStream array = new ByteArrayInputStream(ary);
PushbackInputStream push = new PushbackInputStream(array);
int i;
while( (i = push.read())!= -1) {
if(i == '#') {
int j;
if( (j = push.read()) == '#'){
System.out.print("**");
}else {
push.unread(j);
System.out.print((char)i);
}
}else {
System.out.print((char)i);
} }
} }