0% found this document useful (0 votes)
15 views

Javafile

Java is an object-oriented programming language that is designed to have as few implementation dependencies as possible and allow applications to run on any device. The document discusses the history and characteristics of Java, provides examples of Java programs, and covers Java concepts like conditionals, loops, and methods.

Uploaded by

Janvi Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Javafile

Java is an object-oriented programming language that is designed to have as few implementation dependencies as possible and allow applications to run on any device. The document discusses the history and characteristics of Java, provides examples of Java programs, and covers Java concepts like conditionals, loops, and methods.

Uploaded by

Janvi Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

INTRODUCTION OF JAVA

Java is a class-based, object-oriented programming language that is designed to have


as few implementation dependencies as possible. It is intended to let application
developers write once, and run anywhere (WORA), meaning that compiled Java code
can run on all platforms that support Java without the need for recompilation. Java
was first released in 1995 and is widely used for developing applications for desktop,
web, and mobile devices. Java is known for its simplicity, robustness, and security
features, making it a popular choice for enterprise-level applications.

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++.

History: Java’s history is very interesting. It is a programming language created in


1991. James Gosling, Mike Sheridan, and Patrick Naughton, a team of Sun engineers
known as the Green team initiated the Java language in 1991. Sun Microsystems
released its first public implementation in 1996 as Java 1.0. It provides no-cost -run-
times on popular platforms. Java1.0 compiler was re-written in Java by Arthur Van
Hoff to strictly comply with its specifications. With the arrival of Java 2, new versions
had multiple configurations built for different types of platforms.

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

A Java program involves the following sections:

 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

Execution Java language –


It is a more slow language when contrasted with different dialects as it is a
memory burning-through language.
Look and Feel –
The default look of GUI applications written in Java utilizing the Swing
toolbox is very not quite the same as local applications.
Memory Management –
In Java, Memory is overseen through trash pickup, at whatever point the
refuse gatherer runs, it influences the exhibition of the apparatus. This is
frequently in light of the fact that all different strings inside the require to be
halted to allow the junk authority string to figure.
Java requires huge memory space –
Java requires a critical or significant measure of memory space when
contrasted with different dialects like C and C++. During the execution of
trash assortment, the memory productivity and the exhibition of the
framework might be unfavorably influenced.
Verbose and Complex codes –
Java codes are verbose, implying that there are numerous words in them
and there are numerous long and complex sentences that are hard to peruse
and comprehend. This can decrease the meaningfulness of the code.
1.) Write a program to print a string

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

String first = "Java";


String second = "Python";
String third = "JavaScript";
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
}

2.) Write a program to calculate area of circle using scanner class

import java.util.Scanner;

class AreaOfCircle

public static void main(String args[])

Scanner s= new Scanner(System.in);

System.out.println("Enter the radius:");

double r= s.nextDouble();
double area=(22*r*r)/7 ;

System.out.println("Area of Circle is: " + area);

3.) Write a program to calculate arithmetic expression through


scanner.

import java.io.*;

class Addition {

public static void main(String[] args)

int num1 = 10, num2 = 20, sum = 0;

System.out.println("num1 = " + num1);

System.out.println("num2 = " + num2);

sum = num1 + num2;

System.out.println("The sum = " + sum);

}
4.) Program to show the concept of conditional operator

public classConditionalOperatorExample

public static void main(String args[])

int x=5, y=4, z=7;

System.out.println(x>y && x>z || y<z);

System.out.println((x<z || y>z) && x<y);

5.) Program to show the concept of logical operator.

class Main {

public static void main(String[] args) {

System.out.println((5 > 3) && (8 > 5));

System.out.println((5 > 3) && (8 < 5));

System.out.println((5 < 3) || (8 > 5));


System.out.println((5 > 3) || (8 < 5));

System.out.println((5 < 3) || (8 < 5));

System.out.println(!(5 == 3));

System.out.println(!(5 > 3));

6.) Program to show the concept of Relational


opearators

public class relatiop {

public static void main(String[] args) ;

int num1 = 12, num2 = 4;

System.out.println("num1 == num2 = " + (num1 == num2) );

System.out.println("num1 != num2 = " + (num1 != num2) );

System.out.println("num1 > num2 = " + (num1 > num2) );

System.out.println("num1 < num2 = " + (num1 < num2) );

System.out.println("num1 >= num2 = " + (num1 >= num2) );

System.out.println("num1 <= num2 = " + (num1 <= num2) );


}}

7.) Program to find whether a number is even or odd

import java.io.*;

import java.util.Scanner;

class GFG {

public static void main(String[] args)

int num = 10;

if (num % 2 == 0) {

System.out.println("Entered Number is Even");

}else {

System.out.println("Entered Number is Odd");

}}}

8.) Program to find greatest of three numbers

public class Largest {


public static void main(String[] args) {

double n1 = -4.5, n2 = 3.9, n3 = 2.5;

if( n1 >= n2 && n1 >= n3)

System.out.println(n1 + " is the largest number.");

else if (n2 >= n1 && n2 >= n3)

System.out.println(n2 + " is the largest number.");

else

System.out.println(n3 + " is the largest number.");

9.) write a program to calculate percentage and


division of a student

import java.util.Scanner;

class Student_Division

public static void main(String[] args)

{
Scanner input = new Scanner(System.in);

System.out.println("Enter The Five Subject Marks :");

int m1 = input.nextInt();

int m2 = input.nextInt();

int m3 = input.nextInt();

int m4 = input.nextInt();

int m5 = input.nextInt();

int tot = m1+m2+m3+m4+m5;

float per = tot/5;

System.out.println("Total :"+tot);

System.out.println("Percentage :"+per);

if(per>=60)

System.out.println("First Division.");

else if(per>=50 && per<=59)

System.out.println("Second Division.");

}
else if(per>=40 && per<=49)

System.out.println("Third Division.");

else{

System.out.println("Fail.");

}}

10.) write a program to use of Switch Statement


public class GFG {

public static void main(String[] args)

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:

dayString = "Invalid day";

}System.out.println(dayString);
}

11.) Write a program to print Fibonacci series.

class FibonacciExample1{

public static void main(String args[])

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;

} }}

12.) program to find Factorial of a number


public class Factorial {

public static void main(String[] args) {

int num = 6;

long factorial = 1;

for(int i = 1; i <= num; ++i)

// factorial = factorial * i;

factorial *= i;

System.out.printf("Factorial of %d = %d", num, factorial);

13.) Write a program to calculate Sum of first n


numbers .
import java.io.*;

class GFG {

public static void main(String[] args)

{
int N = 10;

int sum = 0;

System.out.print("First " + N + " Numbers = ");

for (int i = 1; i <= N; i++) {

System.out.print(i + " ");

sum += i;

System.out.println();

System.out.println("Sum of first " + N

+ " Natural Number = " + sum);}}

14.) Write a program calculate area of rectangle using


classes
class AreaOfRectangle

public static void main(String args[])

int l=8;

int b=8;
double area=l*b;

System.out.println("Area of Rectangle is: " + area);

}}

15.) write a program to print a table

import java.util.Scanner;

public class TableExample

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter number: ");

int num=sc.nextInt();

for(int i=1; i <= 10; i++)

System.out.println(num+" * "+i+" = "+num*i);

}
}

16.) Program to show the concept of Default


Constructor

public class Student {

String firstName;

String lastName;

int age;

public Student(){

firstName = "Rocky";

lastName = "Bhai";

age = 20; }

public static void main(String args[]) {

Student myStudent = new Student();

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;

Geek(String name, int id)

{ this.name = name;

this.id = id;

}class GFG {

public static void main(String[] args)

Geek geek1 = new Geek("Ninja Hattori", 112);

System.out.println("GeekName :" + geek1.name

+ " and GeekId :" + geek1.id);

}
18.) Program to show the concept of copy Constructor

public class Fruit

private double fprice;

private String fname;

Fruit(double fPrice, String fName)

fprice = fPrice;

fname = fName;

Fruit(Fruit fruit)

System.out.println("\nAfter invoking the Copy Constructor:\n");

fprice = fruit.fprice;

fname = fruit.fname;

double showPrice()

{
return fprice;

String showName()

return fname;

public static void main(String args[])

Fruit f1 = new Fruit(399, "Ruby Roman Grapes");

System.out.println("Name of the first fruit: "+ f1.showName());

System.out.println("Price of the first fruit: "+ f1.showPrice());

//passing the parameters to the copy constructor

Fruit f2 = new Fruit(f1);

System.out.println("Name of the second fruit: "+ f2.showName());

System.out.println("Price of the second fruit: "+ f2.showPrice());

}
19.) Program to show the concept of constructor
overloading
public class Student {

//instance variables of the class

int id;

String name;

Student(){

System.out.println("this a default constructor");

Student(int i, String n){

id = i;

name = n;

public static void main(String[] args) {

//object creation

Student s = new Student();

System.out.println("\nDefault Constructor values: \n");

System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);

System.out.println("\nParameterized Constructor values: \n");


Student student = new Student(10, "David");

System.out.println("Student Id : "+student.id + "\nStudent Name :

"+student.name);

20.) Program to show the concept of Single Level


inheritance

class main {

public void display() {

System.out.println("I am a method from class A");

// B is inheriting display method of A

class B extends main {

public void print() {

System.out.println("I am a method from class B");


}

public static void main(String[] args) {

B objB = new B();

objB.display(); // Reusing the method of A named display

objB.print();

21.) Program to show the concept of Hierarchical


inheritance

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

22.) Program to how the concept of overriding

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

// implement the Polygon interface


class Rectangle implements Polygon {

// implementation of abstract method


public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length *
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();
}

class Sample implements Inf1, Inf2, Inf3 {


public void sayHello1() {
System.out.println("Hello World1");
}
public void sayHello2() {
System.out.println("Hello World2");
}
public void sayHello3() {
System.out.println("Hello World3");
}
}
public class Main {
public static void main(String[] args) {
Sample S = new Sample();
S.sayHello1();
S.sayHello2();
S.sayHello3();
}
}

25.) Program to print a pattern *


**
***
****

public class RightTrianglePattern


{
public static void main(String args[])
}
int i, j, row=4;
for(i=0; i<row; i++)
{
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

26.) Program to use of Exception handling

public class JavaExceptionExample{


public static void main(String args[]){
try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}

27.) Program with multiple catch exceptions

public static void main(String args[]){


try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}
public class MultipleCatchBlock1 {

public static void main(String[] args) {

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

29.) Program to illustrate the use of thread

public class MyThread1


{
public static void main(String argvs[])
{
Thread t= new Thread("My first thread");

t.start();
String str = t.getName();
System.out.println(str);
}
}

30.) Program to create thread using runnable interface

public class Multi3 implements Runnable{


public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor
Thread(Runnable r)
t1.start();
}
}

31.) Program to use of Stack Trace

public class StackTraceExample


{
public static void main(String args[])
{
demo();
}
static void demo()
{
demo1();
}
static void demo1()
{
demo2();
}
static void demo2()
{
demo3();
}
static void demo3()
{
Thread.dumpStack();
}
}

32.) Program to illustrate the use of final keyword


final class Day {
// Constant variables to store the number of minutes and hours in a
day
static final int MINUTES_PER_DAY = 1440;
static final int HOURS_PER_DAY = 24;

// Method to display the number of hours and minutes in a day


static void displayHoursAndMinutes() {
System.out.println("Number of hours in a day: " +
HOURS_PER_DAY);
System.out.println("Number of minutes in a day: " +
MINUTES_PER_DAY);
}
}
public class Main {
// Main method
public static void main(String[] args) {
// Call the displayHoursAndMinutes method
Day.displayHoursAndMinutes();
}
}

33.) Program to illustrate the use of IsAlive method


public class JavaIsAliveExp extends Thread
{
public void run()
{
try
{
Thread.sleep(300);
System.out.println("is run() method isAlive "+Thread.currentT
hread().isAlive());
}
catch (InterruptedException ie) {
}
}
public static void main(String[] args)
{
JavaIsAliveExp t1 = new JavaIsAliveExp();
System.out.println("before starting thread isAlive: "+t1.isAlive());

t1.start();
System.out.println("after starting thread isAlive: "+t1.isAlive());
}
}

34.) Program to show the concept of Multilevel


inheritance

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

35.) Program to print a pattern A


BC
DEF
GHIJ
KLMNO

class main
{
public static void main(String args[]) {
char ch = 'A';

for (int i = 0; i < 5; i++) {


for (int j = 0; j <= i; j++) {
System.out.print(ch++);
}
System.out.println();
}
}}

36.) Program to print a pattern 1


31
531
7531
9 7 5 31

public class main


{
public static void main(String args[]) {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
System.out.print(j + " ");
}
System.out.println()

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

} }

You might also like