Computer Scientists are Pretty Pessimistic

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 28 August 2016

Final Keyword

final variable :-

When a variable declared as final it's value never be changed next time .

final method :-

When a method declared as final it can be overridden .

final class :-

To prevent a class from being inherited the class need to declared as final.

finally block :-

A finally block is a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block . The finally block will execute whether or not an exception is thrown . If an exception is thrown. the finally block will execute even if no catch statement matches the exception .

finalize method :-

When an object is no longer used, it is called "garbage" . The function of the "garbage collector" is to determine objects which are no longer used and reclaim the memory occupied by the object .

Why Abstract Class ?

Sometimes there may be a need to crate a super class that only defines a generalized form that will be shared by all of its sub classes , leaving it to each sub class to fill in the details . Such a class determines the mature of the methods that the sub classes must implement. Abstract class provides a way to solve this type of situation.

Restrictions of abstract class :-

* Constructor never be declared as abstract .

* An object can't be created of abstract class since an abstract  class is not fully defined .

* Can't declare an abstract static methods .

Interfaces in JAVA

Interfaces :-

* Using interface you can specify what a class must do.

* Variables in interface always final and static by default.

* All methods and variables are implicitly public.

* One or more classes can implement an interface.

* A class implements more than one interface.

Some Restriction of Interface :-

* You can not instantiate an interface. (you won't create an object)

* An interface doesn't contain any constructors.

* All of the methods in an interface are abstract.

* An interface can't contain instance fields. The only fields that can appear in an interface must be declared both static and final.

* An interface is not extended by a class, it is implemented by a class.

* An interface can extend multiple interfaces.

Exception Handling in JAVA

# An exception is an abnormal condition that arises in a code sequence at run time.

Java exception handling is managed via five keywords : -
                       Try, Catch, Throw, Throws and Finally

Try : -
       Program statements that you want to monitor for exceptions are contained within a try block.

Catch : -
      Catched an exception.

Throw : -
     To manually throw an exception use the keyword throw.

Throws : -
     Any exception that is thrown out of a method must be specified as such by a throws clause.

Finally : -
     Any code that absolutely must be executed after a try block completes is put in a finally block.

# Once an exception is thrown program control transfers out of the try block into the catch block.

                                        
                    java.lang.Arithmetic Exception: divide by zero
                    at exc2.main(exc2.java.18)

Wednesday, 22 June 2016

What is Automatic Type Conversion and Automatic Type Promotion ?

Automatic Type Conversion:

Automatic conversion between compatible type is called type conversion. When one type of data is assigned to another type of variable an automatic type conversion will take place if the following two conditions are met,

1. The two types are compatible.

2. The destination type is larger than the source type.


Automatic Type Promotion:

While evaluating expressions Java automatically promotes all byte, short and char values to int and in case of the presence of a float or double they are promoted to float or double respectively. This is called automatic type promotion. 

Access Specifiers In Java

There are four types of access specifiers in java : -

1. Anything declared public can be accessed from anywhere.

2. Anything declared private can not be seen outside of its class.

3. Anything declared as default access (package private) can be accessed from anywhere in the same package.

4. Anything declared as protected can be accessed from anywhere in the same package plus from only its sub classes outside the package. 

Is Java compiled language or interpreted language or both ? Give justification to your answer.


Generally a programming language is compiled or interpreted. But Java is both compiled and interpreted. Because firstly a Java source code converted into byte code. This byte code is machine independent. After that this byte code converted into machine code by Java interpreter. So, Java is called both compiled and interpreted language. And also java is known two stage programming language.



public static void main ( String args[] )

public : main () method must be declared as public since it must be called by code outside of its class when the program is started.

static : The keyword static allows main () method to be called without having to instantiate a particular instance of the class. This is necessary since main () method is called by the JVM before any objects are made.

main : All java application begin execution by calling main () method. So, we need a main method must in our code.

String args[] : In main () method there is only one parameter which is an array of instances of the class String. Objects of type String store character Strings. In this case args receive any command line arguments present when the program is executed. 

Sunday, 10 April 2016

What is Byte Code ?

Byte Code : : :

                Java byte code is the language to which java source is compiled and java virtual machine understands. Unlike compiled languages that have to be specifically compiled for each different type of computers, a java program only needs to be converted to byte code once, after which it can run on any platform for which a java virtual machine exists.
               Byte code is the compiled format for java programs. Once a java program has been converted to byte code, it can be transferred across a network and executed by java virtual machine. Bytecode files generally have a .class extension

Why JAVA is platform independent ?

When we compiled a java source code first of all it converted into Byte Code which extension is ".class". This byte code executed by JVM. JVM is Java Virtual Machine , in every operating system like windows, linux, mac there have JVM, based on their system. So, any JAVA source code can be run on any platform with the help of JVM. So, Java is platform independent.

Constructors ! ! !

## Constructor is block of code which is executed at the time of object creation. But other than getting called, constructor is entirely different than methods and has some specific properties like name of constructor must be same as name of class. Constructor also can not have any return type.

Example::

public class Hello{
        String s ="WORLD";
        Hello() //constructor
         {
               System.out.println(s);
         }
        public static void main(String args[])
         {
                Hello object = new Hello(); //Hello() called constructor
         }
}

What is Class in JAVA ?

## Class is a collection of objects of similar type. Objects are variables of the type class. Once a class is defined, we can create any number of objects belonging to that class.

Example: grapes,bananas and orange are the member of class  fruit.

N.B: Classes are user define data types.

Thursday, 24 March 2016

Short Description of JDK , JVM , JRE

JDK:
    The Java Development Kit (JDK) is a software development environment used for developing java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in java development.

JRE:
   The Java Runtime Environment (JRE) is a set of software tools for development of java applications. It combines the Java Virtual Machine (JVM), platform core classes and supporting libraries. JRE is the part of the JDK.

JVM:
   The Java Virtual Machine (JVM) is an abstraction layer between a java application and the underlying platform. As the name implies, the JVM acts as a virtual machine or processor. To the bytecodes comprising the program, they are communicating with a physical machine however they are actually interacting with JVM.



VISUALIZATION


Tuesday, 22 March 2016

Three Principles of Object Oriented Programming (JAVA)

Three principle of OOP 

                                1. Encapsulation
                                2. Inheritance
                                3. Polymorphism


Encapsulation:
          Encapsulation in JAVA is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class, therefore it is also known as data hiding.

Example:

public class EncapTest{
               private String name;
               public void setName(String name)
               {
                          this.name=name;
               }
                public String getName()
               {
                           return name;
               }
}
public class Main{
         public static void main(String args[]){
                           EncapTest ob = new EncapTest();
                           ob.setName("IMTIYAZ");
                           System.out.println(ob.getName()):
        }
}


Inheritance:
           Inheritance is the process by which one object acquires the properties of another object by use of inheritance, an object would need only to define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.

Example:

public class calculation{
              int l;
              public void add(int h, int r)
              {
                       l = h+r;
                       System.out.println(l);
              }
}
public class my_calculation extends calculation{
              public void sub(int x,int y){
                       l = x-y;
                       System.out.println(l);
             }
}
public class InheritTest{
            public static void main(String args[]){
                    int a=20,b=10;
                    my_calculation ob = new my_calculation();
                    ob.add(a,b);
                    ob.sub(a,b);
           }
}



Polymorphism:
             Polymorphism is a feature that allows one interface to be used for a general class of actions. the specific action is determined by the exact nature of the situation.

Example:

public class bike{
            void run(){
                    System.out.println("Running");
            }
}
public class splender extends bike{
            void run(){
                    System.out.println("Running safely with 60km");
            }
}
public class Test{
            public static void main(String args[]){
                              bike b = new splender(); //upcasting
                              b.run():
            }
}

Output : : Running safely with 60km