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

1.public class NAME
  • Public --> Access modifier. It means the class can be accessed from anywhere in the program.
  • Class --> Keyword used to create a class in java.
  • name -->Class name. You can replace NAME with any vaild class name such as Hello, Student, etc..
2.public static void main (String args[])

This is the main method, where Java strats executing the program.

Public:
  • The method can be accessed by the JVM (Java Virtual Machine).
Static
  • Allows the method to be called without creating an object of the class.
void
  • The method does not return any value.
main
  • Specail method name recognized by the JVM as the entry point of the program
String args[]
  • Stores command-line arguments passed when running the program.
  • args is an array of string objects.

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

Used to print otput on the console

System
  • A built_in java class
out
  • Standard output steam (console).
println(" ");
  • Prints the text and moves the cursor to the next line.

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! */.


Difference between JDK, JRE, and JVM :


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 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 Type Casting:
Type casting means converting one data types into another. For example, turning an int into double.

In Java, ther eare two types of casting:
  • Widening Casting ( Automatic ) --> Converting a smallar type to larger type size.

    byte -> short -> char -> int -> long -> float -> double
  • Narrowing Casting ( manual ) ---> Converting a larger type to smaller type size.

    double -> float -> long -> int -> char -> short -> byte

Java operators:

1.Arithmetic Operators:

Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example
+ Addition Adds two values together x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

program:

public class arithmeticoperators {

    public static void main(String[] args) {
        int x = 10;
        int y = 3;
        int z = 8;

        System.out.println(x + y); // add 13
        System.out.println(x - y); // sub 7
        System.out.println(x * y); // multi 30
        System.out.println(x / y); // div 3
        System.out.println(x % y); // mod 1

        ++z;
        System.out.println(z); // inc 9
        --z;
        System.out.println(z); // dec 8
    }
}

output:

PS C:\Manijava> javac arithmeticoperators.java PS C:\Manijava> java arithmeticoperators.java 13 7 30 3 1 9 8


2.Assignment Operators:

Assignment operators are used to assign values to variables.

The reason is that assignment operators change the value of the variable itself. After each statement, X gets a new value.

Operator Example Same As Description
= x = 5 x = 5 Assign value to variable
+= x += 3 x = x + 3 Add and assign
-= x -= 3 x = x - 3 Subtract and assign
*= x *= 3 x = x * 3 Multiply and assign
/= x /= 3 x = x / 3 Divide and assign
%= x %= 3 x = x % 3 Modulus and assign
&= x &= 3 x = x & 3 Bitwise AND and assign
|= x |= 3 x = x | 3 Bitwise OR and assign
^= x ^= 3 x = x ^ 3 Bitwise XOR and assign
>>= x >>= 3 x = x >> 3 Right Shift and assign
<<= x <<= 3 x = x << 3 Left Shift and assign
>>>= x >>>= 3 x = x >>> 3 Unsigned Right Shift and assign

Program:

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

        int x = 10;

        System.out.println("Initial value: " + x);

        x += 5; // x = x + 5
        System.out.println("x += 5 : " + x);

        x -= 3; // x = x - 3
        System.out.println("x -= 3 : " + x);

        x *= 2; // x = x * 2
        System.out.println("x *= 2 : " + x);

        x /= 4; // x = x / 4
        System.out.println("x /= 4 : " + x);

        x %= 3; // x = x % 3
        System.out.println("x %= 3 : " + x);
    }
}

Output:

PS C:\Manijava> javac assignment.java PS C:\Manijava> java assignment.java Initial value: 10 x += 5 : 15 x -= 3 : 12 x *= 2 : 24 x /= 4 : 6 x %= 3 : 0

JAVA OOPS CONCEPT:

Java oops(object-orient programming system) concepts are the core ideas 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



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



Error in java:
java error commonly grouped into 3 categories
  1. Syntax error (Compile-time error). ---> You assembled the bike incorrectly
  2. Run-time error. ---> The bike breaks while riding.
  3. Logical error. ---> The bike works, but you go to the wrong destination.
1.Syntax error (Compile-time error).
    Mistakes in java grammar or syntax.
    For example: forgot the Semicolon (;) , Mismatch types and Undeclared variable. 

example:
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello World")
    }
}
 
ERROR:
       ' ; ' expected.

2.Runtime error:
       The code complies successfully , but crashes while running.
        For example:
example:


Comments

Popular posts from this blog

ORACLE DBA

JSP TUTORIAL