Introduction to Java

Introduction to Java-

Introduction to Java  language-

Java is  one of most popular programming language in the world.
  • Java was developed by 'Sun MicroSystem' in '1991' and later acquired by the 'Oracle'.
  • Java was developed by 'James gosling' and 'Patrick Naughton'.
  • The first publicly available version of Java is Java 1.0 version was released in 1995.
  • Oracle acquired Sun MicroSystem in 2010. 

Main functions of Java-

Java is platform independent language. Platform independent means, the code written in Java can run on any operating System.
Such as- Windows, Unix, Mac etc.

Object Oriented Language-

Object oriented program is a way of organizing programs as a collection of objects. Each of which represent instance of class.

Four main concept of oop-
  • Inheritance
  • Abstraction
  • polymorphism
  • Incapsulation.

Simple-

Java is a very Simple programming language because it was based on C and C++ Syntax to make it easy for programmers.

Robust-

Robust means reliable.

Portable-

Portable means the Java code i.e. written on one machine can run on another machine.

C++  v/s Java

Q- Where Java is used ?
Ans- Java is used for 
  • Mobile Application.
  • Desktop application
  • Web application 
  • Games
  • Data base connection.

Programming phases in Java-  

Programming phases in Java are as follows-
  • Writing of Program:- Writing of Program is a course done by the Java Programmer like you and me .
  • Compilation of Program:- It is done by Javac Compiler Javac is the primary Java compiler included in Java Development kit (JDK).
  • Run the program:- In third phase , Java virtual machine (JVM) execute the byte code generated by the compiler. This is called program run phase.
  • Java basic Terminology:- Java virtual machine (JVM) is a engine that provide run time environment  to drive the Java code or Application.
  • JVM converts Java byte code into machine language.
  • JVM is a part of java run time enviornment.
  • In other programming language the compiler produces machine code for a particular system however Java compiler produces code for Java virtual machine.
  • JVM is responsible for allocating memory space.

Byte code:-  Javac compiler of JDK, compiles the java source code into byte So it can execute by JVM.

  • The byte code saved in .class extension file by compiler.
  • Java byte code is a language by which java source code is compile and the java virtual machine understand.
  • Unlike compiled languages that have to be  specificly Compiler for each different types of computer a Java program only needs to be converted to byte code only once after which it can run on any platform for which JVM exists for.
  • Byte code is the compiled format Java program.

    • Java development Kit (JDK)-  The JDK is Software development environment used for developing Java application .It include Java run time environment (JRE) an interpreter, a Compiler (Java c) a document generator in Java development.
    • In order to create , compiler and run the java program. you need to install JDK on your computer System.

    Java Run time Environment-

The Java run time  environment is a set of software tools for development of java application.It takes the java code combines it with the necessary library and start the JVM to execute it. The JRE contains library and software that your program need to r
JRE= JVM + library files.
JDK= JRE + Development tools.


Keywords in Java - 

Keywords and reserved words are the words in a language process or represent some predefined actions Because of this programmers can't use keywords as name of variables, methods ,classes or any other identification.

A list of Java keywords or reserved words are given below:-
  1. abstract:-  Used to declare abstract class A.C. can provide the implementation of interface.
  2. boolean:- Holds boolean variable true or false only.
  3. byte:- Holds a variable that can take 8-bitdata value.

 
20. implements:- Keyword is used to implement an interface.
21. import:- This keyword makes classes and interfaces available and accessible to the current source code.
22. instance:- Java keyword is used to test whether the object is an instance of the specified class or implement an interface.
 
35. static:-Java static keyword is used to indicate that a variable or method is a class method. The Static keyword in Java is used for memory management maily.
36. strict fp (Floating point):- Restrict the floating point calculation to ensure portability.

Constants in Java:- Constants in Java are fixed values those are not changed during the execution of program.
OR
A constant is a variable which can't  have it's value changed ofter declaration.It used 'final' keyword.
Syntax:- Modifier final datatyes variable name = value;
Example:- public final double p=3.14;

Types of constant:-  Java Supports several tupes of constants.
(i) Integer constant
(ii) Real constant
(iii) Single constant
(iv) Backslash character constant.

Variables:-  A variable is a collection container which holds the value which while the Java program is executed. A variable is assigned with a data type.
OR
A variable is a name given to a memory location. It is the basic unit of Storage in a program.
  • The value stored in a variable can be changed during the execution of the program.
  • All the operations done of the variable effects that memory location.
  • In Java, variables most be declared before use.
Declaration of variable:-
data-type       variable-name = value;

Ex:- 
        int age = 20;

Types of variable:- There are three types of variables in Java:-

  1. Local variables
  2. Instance variables
  3. Static variables.
Data type in Java:-  In programming language data types simply indicates what kind of data we are going to use in our program.

Data types are divided into two groups:- 
(i) Primitive data types   (ii) Non- primitive data types.

Primitive data types:-
(i) byte   (ii) short  (iii) int   (iv) boolean 
(v) char (vi) long  (vii) float (viii) double.

Non- primitive data types:-
(i) string  (ii)  array  (iii) classes.

Program : sum of two numbers

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

int num1 = 5;
int num2 = 15;
int sum;
sum = num1 + num2;
System.out.println("Sum of number" +sum);
}
}
Java operators:- Operators are used to perform operations on variable and values. The value is called an operand, while the operation (to be performs between the two operands) is defined by an operator.

Operand        Operator           operand
     100                   +                         50

Ex-
            int x = 100 + 50;

Decision making-   
                                Decision making in programming is some situation where we want a certain block of code to be executed where some condition is full filled.

A programming language uses control statements to control the flow of execution of program based on certain condition.
Decision making Statements:- 
  1. if statement
  2. if-else statement
  3. switch statement
  4. break statement
  5. continue statement
  6. jump statement
If statement:-  If statement is used to decide whether a certain statement  or block of statement will be executed or not i.e. if a certain is true then a block of statement is executed otherwise not.
Syntax:-
if (condition)
{
   // Statement to execute if condition is true
}

Ex:-
        class Demo
        {
           public static void main(String args[])
         {
           int i =10;
          if(i>9)
          {
             System.out.println("Ruhi");
         }
         }
         }

(ii) If-else statement :-   If -else statement in Java is also used to control the program flow based on some condition.

In this statement if block will be executed when the condition is true otherwise if not executed then else part will be executed.
Syntax:-
                if(condition)
                {
                    // executes this block if condition is true
                }
                    else
                {
                    // executes this block when if condition is false
                }
Ex:-

                    class Demo
               {
                    public static void main(String args[])
                {
                        int age = 18;
                        if (age < 18)
                {
                    System.out.println("you can't vote");
                }
                    else
                 {
                    System.out.println("you can vote");
                }
            }
        }

switch statement :-  The Java switch statement executes one statement from multiple conditions
OR
The switch statement test the equality of a variable against multiple values. It provides an easy way to dispatch execution to different part of code based on the value of expression basically the expression can be byte , short, char , and int data types.
Syntax:-
                switch (expression)
                {
                 case value 1 :
                    // code to be executed;
                 break;    //optional
                
                 case value 2:
                    //code to be executed;
                 break;   // optional
                 --      ---        ----
                ---      ---        -----
                 default :
                // code to be executed if all cases are 
            }
Important rules for switch statements:-
  • The case values must not be duplicates values.
  • The value for a case must be the same data type as the variable in the switch.
  • The value for a case must be a constant or a literal , variables are not allowed.
  • The break statement is optional and can appear any where inside the switch block.
Ex:-
                public class Test
            {
                public static void main(String args[])
            {
                    int day = 5;
                    string dayname;
                    
                    switch (day)
            {  
                case 1:
                System.out.println("Monday");
                break;
            
                case 2:
                System.out.println("Tuesday");
                break;

                case 3:
                System.out.println("Wednesday");
                break;

                case 4:
                System.out.println("Thursday");
                break;

                base 5;
                System.out.println("Friday");
                break;
                
                base 6;
                System.out.println("Saturday");
                break;
            }
        }

Figure of switch statement
Loop control in Java :- A loop statement allows you to execute a statement or a group of statement multiple time. Programming language provides various control structures that allows for more complicated execution paths.

OR
Looping in programming language is a feature which facilities the execution of a set of instruction/function repeatedly  while some condition evaluate to true.
Java programming language provides the following types of loop to handle the looping requirement.
  1. for loop
  2. while loop
  3. do-while loop
(i) for loop :-  A for loop is a repetition control structure that allows you to efficiently write a loop that need to be executed a specific number of times. 
Note that:- 
                    A  for loop is useful when you know, how many times a task is to repeated.

Syntax:-
                for(initialization ; boolean expression ; update)
            {
                // for loop statement(s);
            }

Ex:- 
            public class Test
        {
            public static void main(String args[])
        {
            for (int x = 10; x < 20; x = x+1)
        {
            System.out.println("value of x= " +x);
        }
    }
}
 
(ii) while loop:-   A while loop statement in java programming language repeatedly executes a target statement as long as given condition is true.                  
Syntax:-
                  while(boolean-expression)
                 {
                        statement;
                        //body of loop
                  }
Here, statement may be a single statement or a block of statements. The condition may be any expression and true is any non zero value .When executing: If the boolean expression result is true , then the actions inside the loop will be executed. This will be continue as long as the expression result is true , when the condition becomes false, program control passes to the line immediately following the loop.

Syntax:-
                   while(condition)
                {
                    conditional code;
                }
Here key point of the while loop is that the loop might not even run. when the expression is tested and the result is false , the loop body will be skipped and the first statement after the while loop will be executed.

Ex-
                   public class Test
                {
                    public static void main(String args[])
                {
                        int x = 10;
                    while(x<10)
                {
                    System.out.println("value of x =" +x);
                    x++;
                    System.out.print("ln");
                }
            }
        }
Result value of :-
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19

Flow diagram of while loop:-
Difference between while loop or do-while loop.

Here key point of the while loop is that the loop might not ever run. when the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

(iii) do-while loop :-
                                    A do-while loop is similar to while loop except that a do while loop is guarrantied to execute at least one-time.
Syntax:-
                do
            {
                Statement;
                // body of loop
            }
                while condition(boolean expression);
Notice that the boolean expression appears at the end of the loop, so the statements in the loop execute once before the boolean is tested. If the boolean expression is true, the control jumps back up to do statements and the statements in the loop execute again. This process until the boolean expression is false.

Example:-

                    public class Test
                {
                    public static void main(String args[])
                {
                    int x = 10;
                        
                    do
                {
                    System.out.print("value of x=" +x);
                    x++;
                    System.out.print("ln);
                }
                    while(x<20);
            }
        }
Result value of:-
                    x=10
                    x=11
                    x=12
                    x=13
                    x=14
                    x=15
                    x=16
                    x=17
                    x=18
                    x=19

Loop Control Statements:-

Loop control statements changes execution from its normal sequence when execution leaves a scope all automatic objects that were created in that scope are destroyed.

Java supports the following control statements:-

(i) break statement
(ii) continue statement.

Break statement in Java:-

Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

The Break statement in Java programming language has the following two usage :-
  • when the break statement is encountered inside a loop , the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • It can be used to terminate a case in switch statement.
Syntax:-
                The Syntax of a break is a single statement inside any loop.
                break;    

Return:-

Java return keyword is used to complete the execution of a method. The return followed by the appropriate value that is returned to the caller. This value depends on the method return type like int method always return an integer value.

Important points to remember:-

  • It is used to exit from the method.
  • It is not allowed to use return keyword in void method (void display() not possible)
  • The value passed with return keyword must match with return type of the method.

Introduction to classes and objects in Java:-

Java is an object oriented programming language.
classes and objects are basic concept of object oriented programming which is based on around the real life entities.

class:-

class is a blueprint or template for creating different objects which defines classes properties and behaviour . A class is used to defined blueprint or prototype from which objects are created. It represent the set of properties or methods that are common to all objects of one type.
OR
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:-
  • Feilds
  • Methods
  • Constructor
  • blocks
  • Interfaces
Syntax for declaration of class:-

                access_modifier class <class_name>
            {
                feilds;
                methods;
            }

Object:-

An object in Java is the physical as well as a logical entity.
OR
An entity that has state and behaviour is known as an object.
OR
An object is an instance of a class.
OR
An object is a real world entity.

An Object has three characteristics:-

State:- 

Represent the data (value) of an object.

Behaviour:-

Represent the functionality of an object such as - deposite, withdraw etc.
Syntax:-
                class-name object-name

Creating object in Java:-

Using "new" keyword is the most basic way to create an object. This is the most common way to create an object in Java. By using this method we can call any constructor we want to call.

Syntax:- 
                class-name  object-name = new class-name();
Ex:-
            public class New keyword
        {
            String name = "Ruhi";
            public static void main(String args[])
         {
            NEW obj = new NEW();
            System.out.println(obj.name);
        }
     }

Constructor in Java:-

  • A  constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of class is created. At the time of calling constructor ,memory for the object is allocated in memory area.
  • Every time an object is created by using new keyword, at least one constructor is called everytime.
           class Test // creating a class Test
       {
            Data members
            member function
        }
            Test() // constructor is created.

Rules for creating Java constructor:-

  • Constructor name must be same as the class name.
  • Constructor should not return any value, not even void.

Why constructor is called constructor:-

It is called constructor because it construct the value at the time of object creation. It is not necessary to write a constructor for a class. It is because Java compiler create a in your class.

            public class Demo //creating a class Demo
        {
            int x; //set the initial value x for the class attribute
            public Demo()
        {
            x=5;
        }
             public static void(String args[])
        {
            Demo obj = new Demo;
            System.out.println(obj.x);
       }
     }

Types of constructor:-

There are three types of constructor in Java-
  1. Default constructor
  2. Parameterized constructor
  3. Non-parameterized constructor.

1. Default constructor:-

It you don't implement any constructor in your class. The Java compiler insert a default constructor in your code. This constructor is known as default constructor.

You would not find a default constructor in your source code (.Java file) as it would be inserted code during compilation and exist in (.class file)

.Java file
            public class my class
        {
            public static void main(String args[])
        {
            my class obj = new my class();
      }
   }

                                                               Compile
.class file

            public class My class
        {
            my class()// Default
        {
            public static void main(String args[])
        {
            my class obj = new my class();
        }
     }
  }

2. Parameterized constructor:-

The constructor that can take parameters or argument are called parameterized constructor.
                        or
The constructor ,which is created by the programmer with the help of one or more parameters with different types of data types is called parameterized constructor.
Syntax:-
public class Sample{
   Int i;
   public sample(int i){
      this.i = i;
   }
}
 Ex:-
  public class Test {
   String val;
   Test(String val){
      this.val = val;
   }
   public static void main(String args[]){  
      Test obj = new Test("test");
      System.out.println(obj.val);
   }
}

 3. Non-parameterized constructor:-

A constructor ,without any parameter is called non-parameterized constructor.

          class Bike  //class is created
        {
            Bike () //non-argument constructor
         { 
            System.out.println("Bike is created");
        }
            // Main method
            public static void main(String args[])
        {
            //calling a non-parameterized or default construction
          Bike b = new Bike();
        }
     }
  }
           

Next Unit- Arrays and Strings






























 








                                                                     

Post a Comment

Previous Post Next Post