JAVA TUTORIAL

JAVA DEFINTION:

* Java is a popular programming language,Created in 1995.

*It is owend by Oracle,and more than 3billion device run in java.

*Java is used to develop  mobile  application, web  application, Desktop application, Games and so on.

SYNTAX:

public class NAME
{
public static void main (String args[])
{
System.out.println(" ");
}

}

sample program:

we created file name sample.java.

public class sample
{
public static void main (String args[])
{
System.out.println("hello world!")

System.out.println("I am hero")

System.out.println("my favourite food Biriyani")

}

}

output:

hello world!

I am hero

my favourite food Biriyani



JAVA COMMENTS:

Java is used to the two types of comments ( // ) and (/* ------- */).

1. // I am learning java.

2./* Java is a popular programming language .It is Awesome! */.


JAVA VARIABLE:

Variable are containars for storing data values.

TYPES OF VARIABLE:

1.String - store text in double quotes "HELLO".

2.int -store integers (whole number),such as 123 or -123.

3.float-store floating point numbers,with decimal ,such as 19.99 0r -19.99.

4.char-strings are store single characters 'a' or 'z'.

5.boolean-store values in two states:True or False.

program:

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

String name = "John";     // String variable
int myNum = 5;            // Integer variable
float myFloatNum = 5.99f; // Float variable
char myLetter = 'D';      // Character variable
boolean myBool = true;    // Boolean variable

// Printing all variables

System.out.println(name);
System.out.println(myNum);
System.out.println(myFloatNum);
System.out.println(myLetter);
System.out.println(myBool);

}

}
output:

John
5
5.99
D
true



JAVA DATETYPES:

Java datatypes is two types primitive and non-primitive.

Primitive:

  • These are the basic built-in type type provide in java.
  • They store simple value directly in memory.
  • Primitive datatypes such as byte, short, int, long, float, double, boolean and char.

Non-Primitive:

  • These are created by the programmer are java libraries that can store collection of data and complex structure..
  • Also known as reference types.
  • Non-Primitive datatypes such as String, Arrays and classes.

program:

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

int mynum = 1000;    // 4 bytes, range: -2,147,483,648 to 2,147,483,647
byte mybyte = 100;     // 1 byte, range: -128 to 127
short myshort = 10000;    // 2 bytes, range: -32,768 to 32,767
long mylong = 5000001L;   // 8 bytes, range: -9 quintillion to +9 quintillion
float myfloat = 7.6f;            // 4 bytes, ~6 to 7 decimal digits precision
double mydouble = 733.88888888d; // 8 bytes, ~15 decimal digits precision

System.out.println(mynum);
System.out.println(mybyte);
System.out.println(myshort);
System.out.println(mylong);
System.out.println(myfloat);
System.out.println(mydouble);
}
}
output:

1000
100
10000
5000001
7.6
733.88888888888




JAVA OOPS CONCEPT:

Java oops(object-orient programming system) concepts are the core ides of java programming based on real-world objects.

They  helps in organizing code,making it reusable, modular, and easier to maintain.

The four main OOP concept in java:-

  1. Encapsulation.
  2. Inheritance.
  3. Polymorphism.
  4. Abstraction.

class/object:

A class is a blueprint for creating object.
It contains Variable(attributes) and methods (behaviours).

An object are instance of a class.
Object are created using the new keywords.


Program:

public class Laptop{
    String name="";
    String proc="";
    int ram=0;
    int price=0;
    public static void main(String[] args)
    {
    Laptop lap1 = new Laptop();
    Laptop lap2 = new Laptop();

        lap1.name="HP";
        lap1.proc="i5";
        lap1.ram=4;
        lap1.price=50000;

        lap2.name="Lenova";
        lap2.proc="i7";
        lap2.ram=6;
        lap2.price=80000;
System.out.println("lap1:"+lap1.price);
       System.out.println("lap2:"+lap2.price);
    }  
    }

output:
lap1:50000 lap2:80000



ENCAPSULATION:
  • Declare class attribute/variable as Private.
  • Provide public getter and setter methods to access and update the value of a Private variable.

Program:

class ClassA{
    private String name;   //private data
    private int age;

    //getter
    public String getName(){
        return name;
    }
   
    //setter
    public void setName(String name){
        this.name = name;
    }

    public int getAge(){
        return age;
    }
    public void setAge(int age){
        if (age >0){
            this.age = age;
        }
    }
}

public class Student{
    public static void main(String[] args) {
        ClassA c =new ClassA();
        c.setName("Mani");
        c.setAge(20);

        System.out.println("Name:" + c.getName());
         System.out.println("Age:" + c.getAge());
    }
   
}

output:

PS C:\Manijava> javac Student.java PS C:\Manijava> java Student.java Name:Mani Age:20 PS C:\Manijava>



INHERITANCE:

  • One class inherits the prorperties and methods of another class using extends -->key
  • Promote reusabilty

Five main type of inheritance:

  1. Single Inheritance.
  2. Multilevel inheritance.
  3. Hierarchical Inheritance.
  4. Multiple Inheritance.(not directly supported in java with classes)
  5. Hybrid Inheritance.

1.Single Inheritance.

---->One class inherits another.

program:

class Animal{
    void eat() {
        System.out.println("Eating...");
        }
}

class Dog extends Animal{
    void bark(){
        System.out.println("Barking...");
    }
}

public class single {
    public static void main(String[] args){
        Dog d = new Dog();
        d.eat(); //from parent
        d.bark(); //from child
    }

}

output:

PS C:\Manijava> javac single.java PS C:\Manijava> java single.java Eating... Barking...



2.Multilevel Inheritance.

---->A class inherits from another class, which again inherits from another. 

program:

class Grandparent {
    void house() {
        System.out.println("Grandparent has a house");
    }
}
class Parent extends Grandparent {
    void car() {
        System.out.println("Parent has a car");
    }
}
class Child extends Parent {
    void bike() {
        System.out.println("Child has a bike");
    }
}
public class multilevel {
    public static void main(String[] args) {
        Child c = new Child();
        c.house(); // from Grandparent
        c.car();   // from Parent
        c.bike();  // from Child
    }
}

Output:

PS C:\Manijava> javac multilevel.java PS C:\Manijava> java multilevel Grandparent has a house Parent has a car Child has a bike PS C:\Manijava>




3.Hierarchical Inheritance.

---->Multiple classes inherit from a single parent.


4.Multiple Inheritance.(not directly supported in java with classes)

---->Java does not allow multiple inhertance using classes.

---->But java supports multiple inhertance using interfaces.

5.Hybrid Inheritance.

---->Java does not support hybrid inheritance with classes,but we can achieve it using interfaces.

POLYMORPHISM:

  • Polymorphism means "many forms".

Two types of Polymorphism:

1.Complie-time(method overloading).

2.Run-time(method overriding).

1.Complie-time(method overloading).

Same method name , but different parameter lists.

Decided by complietime Polymorphism(early binding).

program:

class mathoperation {
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
}

public class operation {
    public static void main(String[] args) {
        mathoperation m = new mathoperation();
        System.out.println(m.add(2, 3));       // int version → 5
        System.out.println(m.add(2.5, 3.5));   // double version → 6.0
    }
}

output:

PS C:\Manijava> javac operation.java

PS C:\Manijava> java operation

5

6.0


2.Run-time(method overriding).

  • Child class provides a new implemention of a parent method.
  • Decided at runtime Polymorphism(late binding).

Program:

class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class overriding {
    public static void main(String[] args) {
        Animal a = new Dog();  // parent reference, child object
        a.sound();             // calls Dog’s sound()
    }

}

output:

PS C:\Manijava> javac overriding.java PS C:\Manijava> java overriding Dog barks


ABSTRACTION:
  • Hinding implementation details and showing only essitential feature.
  • Achived using abstract class or interface.
  • Abstract class cannot create object directly.
  • Abstract method has no body,must be implemented by child.
program:

abstract class vehicle{
    abstract void speed();
}
class Bike extends vehicle{
    void speed()
    {
        System.out.println("50km.hr");
    }
    }
class car extends vehicle{
        @Override
        void speed() {
        System.out.println("100km.hr");
    }
    }
public class abs {
    public static void main(String[] args) {
        vehicle v1 = new Bike();
        vehicle v2 = new car();
        v1.speed();
        v2.speed();
    }
    }
output:
PS C:\Manijava> javac abs.java PS C:\Manijava> java abs 50km.hr 100km.hr



Comments

Popular posts from this blog

JSP TUTORIAL

ORACLE DBA