Class and object

Class

  • Class is a structure
  • Binding the data with its related and corresponding functions.
  • Class is the base for encapsulation.
  • Class is a user defined data type in java.
  • Class will act as the base for encapsulation and implement the concept of encapsulation through objects.
  • Any java applications looks like collection of classes but where as c- application looks like collection of functions.

  1. public class Example{

  2.          //variable declaration
  3.           int id;

  4.       // methods
  5.         public int getId() {
  6.           return id;
  7.          }

  8.     public void setId(int id) {
  9.         this.id = id;
  10.     }

  11. }
  12. ;

Object

  • Object is nothing but instance (dynamic memory allocation) of a class.
  • The dynamic memory allocated at run time for the members [non-static variables] of the class is known as object.

What is a Reference variable?

  • Reference variable is a variable which would be representing the address of the object.
  • Reference will act as a pointer and handler to the object.
  • Since reference variable always points an object.
  • In practice we call the reference variable also as an object.
  • We can create an object by using new operator.
  • If we want to call any method inside the class we need object
               Syntax: A obj= new A();

Example program:

      public class A {
         int a;
     public  void Print(){
        System.out.println("value of a="+a);
    }
    public static void main(String[] args) {
   A obj=new A();
  obj.print();
    }
}

Output: value of a=0
What Object Contains?
  • Object of any class contains only data.
  • Apart from the data object of a class would not contains anything else.
  • Object of a class would not contain any functionalities or logic.
  • Thus object of a class would be representing only data and not represent logic.

What is state of the Object?

  • The data present inside object of a class at that point of time is known as state of the object

What is behavior of the object? 

  • The functionalities associated with the object => Behavior of the object.
     The state of the object changes from time-to-time depending up on the functionaities that are executed on that object but whereas behavior of the object would not change.



Naming conventions for declaring a class:

  • Class name should be relevant.
  • Use UpperCamelCase for class names. //StudentDemoClass
  • For Methods names follow camelCase(); //getName();
  • Declare variables lowercase first letter. // int rollNumber
  • To declare final static variables means which will acts like constants
    MIN_WIDTH=10;
    MAX_AGE=18;

Check armstrong number or not


  1. package com.instanceofjavaTutorial; 
  2. import java.util.Scanner;

  3. public class ArmstrongNumber{
  4.  
  5. public static void main(String args[])
  6.  {
  7.  
  8.           int n, sum = 0, temp, r;
  9.  
  10.           Scanner in = new Scanner(System.in);
  11.  
  12.           System.out.println("Enter a number to check if it is an Armstrong number or not");     
  13.           n = in.nextInt();
  14.  
  15.           temp = n;
  16.           while( temp != 0 )
  17.           {
  18.  
  19.              r = temp%10;
  20.              sum = sum + r*r*r;
  21.              temp = temp/10; 
  22.  
  23.           }
  24.  
  25.           if ( n == sum )
  26.              System.out.println(n+"is an Armstrong number.");
  27.           else
  28.              System.out.println(n+"  is not an Armstrong number.");  
  29.        
  30.        }
  31. }


Output:

  1. Enter a number to check if it is an Armstrong number or not
  2. 153
  3. 153is an Armstrong number.



Constructor Overloading

  • Concept of defining multiple constructors within the same class by changing the data types of the parameters is known as constructor overloading.
package com.instanceofjavaforus;

public class ConstructorOverloading {

int a,b,c;

ConstructorOverloading(){
    this(1);
}

ConstructorOverloading(int a){
    this(a,2);
}

ConstructorOverloading(int a, int b){
    this(a,b,3);
}

ConstructorOverloading(int a, int b,int c){
this.a=1;
this.b=2;
this.c=3;
System.out.println(a+""+b+""+c);
}
   
public static void main(String args[]){
   
    ConstructorOverloading obj=new ConstructorOverloading();
   
   }
   
}


OutPut:
1 2 3

Command line arguments

  • The group of strings what we are passing as an argument to the main method from the console or command line are known as command line arguments.
  • Every command line argument would be accepted by the JVM in the form of a String object.
  • JVM will always executes main method by passing object of an array of strings.
  • JVM would never execute main method by passing null
  • JVM always executes main as a thread. 

 

Program on command line arguments:

package com.instanceofjavaforus;

public class CommandLineArguents {

    public static void main(String args[]){
      
        System.out.println("Your first argument is: "+args[0]);
    }
  
}

  • >javac CommandLineArguent.java

    >java CommandLineArguents

    OutPut:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at com.instanceofjavaforus.CommandLineArguents.main(CommandLineArguents.java:8)
  • >javac CommandLineArguent.java

    >java CommandLineArguents  instance

    OutPut:
    instance

Enhanced For loop

  • Using enhanced for loops we are just simplify the regular for loop and operation of for loop.
  • Enhanced for loops are applicable to any object that maintains group of elements and supports indexes.
  • Enhanced for loops are used where ever it is required to access all the elements from the beginning till ending automatically from array or from any object representing group of elements along with index.

Enhanced for loop to iterate Array:

 package com.instanceofjavaforus;

public class EnhancedForloop {

    public static void main(String args[]){
      
        int x[]={1,2,3,4,5,6};
      
        for(int i:x){
            System.out.println(i);
        }
      
    char ch[]={'a','b','c','d','e'};
  
    for(char i:ch){
        System.out.println(i);
    }
  
    }
}

output:

1
2
3
4
5
6
a
b
c
d
e

Enhanced for loop to iterate ArrayList:


package com.instanceofjavaforus;

import java.util.ArrayList;
import java.util.Collection;

public class EnhancedForLoopList {

    public static void main(String args[]){
       
    Collection<String> nameList = new ArrayList<String>();
    nameList.add("Ramu");
    nameList.add("Geetha");
    nameList.add("Aarya");
   nameList.add("Ajay");
    for (String name : nameList) {
      System.out.println(name);
    }
    }
}

Output:

Ramu
Geetha
Aarya
Ajay

Difference between arraylist and vector

     
  • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
  • ArrayList was introduced in java version1.2, as part of java collections framework.
  • Synchronization and Thread-Safe:
  • Vector is  synchronized. Synchronization and thread safe means at a time only one thread can access the code .In Vector class all the methods are synchronized .That's why the Vector object is already synchronized when it is created .
  • ArrayList is not synchronized.

How To Make ArrayList synchronized:

  • We have direct method in collections class to make Arrraylist as Synchronized.
  • List li=new ArrayList();
  • Collections.synchrinizedlist(li);

Automatic Increase in Capacity:
  • A Vector defaults to doubling size of its array.
  • ArrayList increases it's size by 50%.
Performance:
  • Vector is slower than ArrayaList.
  • ArrayList is faster than Vector.




Select Menu