Friday, September 25, 2015

Java Keywords (Return, Super, This)


Return: This keyword is a keyword. This can be use with methods, loops, and statements. The behavior of return is different with different scenarios.

1)   Methods: Return keyword is always used with method when the return type is not void. Return type of methods could be anything. The return value must be as the return type declared with method signature.

package com.demo;

public class DemoClass {

       public String getName() {

              String name = "Try Catch";

              return name;
       }

}


2)   Loops: In context of loops, return keyword used when we want to exit from loop. Here is no need to declare return value.

package com.demo;

public class DemoClass {

       public void getName() {

              while (true) {

                     System.out.println("Try Catch");

                     return;
              }
       }
}



Super: Super is a keyword which is use to access the members of super or parent class. To use super keyword, inheritance must be there. This keyword could use with following statements:

package com.demo;

public class SuperClass {

       String name = "Instance varibale of SuperClass";
      
       public SuperClass() {
              System.out.println("Super class constructor...");
       }
      
       void getName(){
              System.out.println("Method of Super class...");
       }

}


1)    Instance Variables: Super keyword use to access or call the instance variable of parent or super class into child or base class.


2)    Constructor: Using super keyword, we can call the constructor of super class. You will see that, while calling super class constructor there is () brackets. This is because; constructor is also like a normal method. So to tell compiler that, we are calling constructor not other members of super class, we have to use these brackets.

3)    Methods: Methods of super call can be access using super keyword.

package com.demo;

public class BaseClass extends SuperClass {

       String name = "Try Catch";

       public BaseClass() {
              super();
       }

       public static void main(String[] args) {

              BaseClass base = new BaseClass();

              base.getName();
              base.showMethod();
             
       }

       void getName() {
              System.out.println(super.name);
       }

       void showMethod() {

              super.getName();
       }

}


This: This keyword is to use to call all the members of same class rather than super class. Rest behavior is always same as super keyword.

package com.demo;

public class BaseClass extends SuperClass {

       String name = "Try Catch";

       public BaseClass() {
              System.out.println("Basse class constructor...");
       }

       public static void main(String[] args) {

              BaseClass base = new BaseClass();
              base.getName();
              base.showMethod();

       }

       void getName() {

              System.out.println(this.name);
       }

       void showMethod() {

              this.getName();
       }


}


Thursday, September 24, 2015

Java Keywords

(interface, abstract, import, implements)
http://www.try2catch.in

Interface: Interface is same as like a class. It is also contains methods and variables. But, behavior of the member is different. To declare a class as interface we just need to use interface keyword in place of class keyword. In case of use of the members of interface, you need to use implements keyword to get all the members in class. We could never make any object of an interface. This keyword is only use with a class to which we want to declare as interface. All the members are public.

1)   Variables: Variable of an interface is always public static and final.

2)   Methods: Methods of an interface are always abstract. In other words, they have only declaration not implementation.

package com.demo;

public interface InterfaceDemo {

       public static final String name = "Try Catch";

       public String showName();

}
  


Abstract: Abstract can be a class or method. It contains all normal member of a concrete class. To declare a class or method as abstract, we need to use abstract keyword. If a class is declared as abstract, it is not mandate that it should have at least one abstract method. To use any member of abstract class, we need to use extends keyword. But if a method is declared as abstract then the class must be an abstract class. Abstract method should not be private. Abstract class may have non abstract methods. It can have private members also. Abstract keyword can be use with class and method.



package com.demo;

public abstract class AbstractDemo {

       private String name = "Try Catch";

        abstract String showName();

       private void getName() {

              System.out.println(name);
       }

}

Implements: This keyword works same as extend keyword but to inherit the properties of an interface.

package com.demo;

public abstract class AbstractDemo implements InterfaceDemo {

@Override
public String showName() {
       // TODO Auto-generated method stub
       return null;
}


}

Import: To inherit the properties of any class, we need to give the full path of the class directory in form of packages.

package com.demo;

import com.demo.*;
public abstract class AbstractDemo  {


}


Tuesday, September 22, 2015

Architecture of JVM


1)   Class Loader
2)   Method Area
3)   Heap
4)   Stack
5)   PC Registers
6)   Native Method Stack
7)   Interpreter
8)   JIT




Let’s learn these concepts one by one.

Class Loader: It will load the class on request.

Method Area: Inside a Java virtual machine instance, information about loaded types is stored in a logical area of memory called the method area. When the Java virtual machine loads a type, it uses a class loader to locate the appropriate class file. The class loader reads in the class file, a linear stream of binary data and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area. Memory for class (static) variables declared in the class is also taken from the method area.

Heap: It is to store java objects. Size of heap is depends on JVM. It can be increase or decrease by developer also. All objects in heap are accessible by all threads. Whenever size of heap is full, JVM will throw an exception of OutofMemoryException.

Stack: It is to store the Methods and the structure of class like, variables, constructor, local variables etc. When the stack is full, JVM will throw StackOverflowException.


PC Registers: It contains address of JVM activity, which is currently being executed.

Native Method Stack: It stores all the native methods information.

Interpreter: It will read the java code and convert into the machine language supported by OS.

JIT: Just in Time compiles the machine language code into byte code.

Below is the architecture of JVM Memory Allocation.

Sunday, September 20, 2015

Data Types in Java







Saturday, September 19, 2015

What is Java Method?


In real word, there are many features in television. Using them, we can personalize our television. One of them is volume. We can change the volume as per our desire.

We give some inputs and television takes that and processes something and set volume level.




In Java world, setting volume is a method, level of volume is member of method and updated volume level is return type.

Method is a set of codes use to perform a task resemblance to its name and returns a value. Method name also should relate to class name.

We can define a method as below:

{Modifier} {Return type} {methodName}({Parameters p}){
         


}

Thursday, September 17, 2015

Java Structure




JDK: It is software to develop and run java programs. It includes JRE and development tools like javac, java and many more. JDK is same as like structure of the multiplex includes wall, ticketing system, security system etc.

JRE: JRE is software which is use to run java programs or provides runtime environment to java programs. JRE is a part of JDK but, you can download and installed it separately also. JRE is same as like your running movie. Because if you have projector to run a movie. Then, you can see movie anywhere anytime.

JVM: JVM is a platform which provides us an environment to develop and run the java programs. It is platform dependent. This exists in JRE.



We have learned about these three but still, there are lots of things which are need to learn. We will learn them when we develop a java program.

Tuesday, September 15, 2015

What is Super Class, Base Class and String Class

Super Class

Super or parent class is a class which properties are inherited by some other class. Object class is a super class, which is inherited by all the class by default. Exists in java.lang package. To inherit the properties of super class we need to use extends keyword like below:

class Lcd extends Television{

}

In real world, we can take example of Television. The basic television has minimum a screen, remote, speaker etc. There are so many types of televisions like LED, LCD, Smart, 3D etc. All are these are television because they inherit the property of television.



Base Class


Sub or child class is a class which inherits the properties of Super class. Like LED, LCD etc are child class of television and television is super class of LED and LCD.

class Lcd extends Television{

}

String Class

String class is a mutable class. Which is use to store any type of value or sequence of characters as String object. String uses char array to store string values. Value of string always in between inverted commas (“”).




class LCD extends Television{

String channelName = “MTV” ;

}

Now, we have learned all the basics of a java program. Let’s develop a program.

class Television{

int numOfChannels = 10; //instance variable

String channelName = “Mtv”; //instance variable

//Class method
public static void main(String[] args){

System.out.println(“Number of channels ”+ numOfChannels);
System.out.println(“Name of channels ”+ channelName);

}

}


Monday, September 14, 2015

Object Oriented Features

OOPs Concept

Inheritance: When one object acquires all the properties and behaviors of parent object i.e. known as inheritance. It provides code re usability.

Inheritance having two types (Inheritance) IS-A and (Composition) HAS-A:

IS-A: This relationship is easy to identify. In real world, car is a vehicle. Car extends all the properties of vehicle and implements vehicle’s behavior. In java world, where ever extends and implements keyword is used with class name, defines IS-A relationship.

This relationship is uni-directional. Means, Car is vehicle but a vehicle never be a car.

HAS-A: In real world, car has AC, stereo etc from car accessories. In java world, when any instance variable use in some other class known as HAS-A relationship.

Poly morph-ism: It is very simple to understand, one name but many forms. Like in real world, we have bicycles. But, there are so many types of them. Only having little changes and there functionality is totally changed just because of some of having gear, light, GPS etc. In java world, overloading and overriding are example of poly morph-ism.

Encapsulation: This is known as binding or wrapper data together into a single unit. The best and simple example is capsule, having different type of medicine into a single capsule. In java world, bean class is best example of encapsulation. All the members are in a single class and all are private.


Abstraction: Hiding internal details and showing functionality is known as abstraction. Like we have car we always drive but don’t know how it starts. In java world, we use abstract classes and interface to implement abstraction.

So till now, we have learned basic features, elements and flow and java program. Now you are able to make a simple program implementing all OOPS concepts.

Sunday, September 13, 2015

What is main method?

Main Method

So, question is that why we need main method. Let’s take a real life example.

Suppose, you bought a movie ticket and entered into Multiplex. Now, How would you know which is your hall to see your movie. You just see your movie tickets and match all the details from the details showing in front of halls. Like movie name, timings and hall number and you entered into that hall and enjoying your movie.

         Same like this. A java program also needs some identification to run it. To                  achieve this, JVM has a default or pre-define structure or signature for this. This is below.


public static void main(String[] args){

}

As we have already learn about all the word which are use to define the main method. All are keywords and we know behaviors and purpose of each keyword. Except main, main is nothing but just a unique name to identify the method.

But now question is this, why we have public static void? Why we don’t have other modifiers or some other return types.

Let’s study that one by one. As we already learn that JVM need some identification point from where it will run a java program. So when the JVM was developed, that time the developer defined that this is the only method from where a Java program should starts.

We can make some of changes, but we will learn it later.

Okay, we agree that this is the default signature. But, why only public static void? Why not private String main() or something else.

Let’s discuss one by one.

public: This is a simple answer, if in case developer make it private and we know the behavior of private keyword. Then, how JVM will access the main method? That’s why it is public.

static: When we know that only JVM will going to call main method, then there is no need to make an object of the class. JVM will call main method by using class name.

void: There is no need to return anything by main method. Because, there is no other method will call after complete processing of main method. If, it will return something, then who will go to use that value? This is why main method does not return any value.

main: main is just a pre-define name which is we have to use to define main method.


String[] args: It is an String[] type of argument and args is a reference of String[] type, which is passed into main method. We will know more about this later.
https://www.youtube.com/edit?o=U&video_id=l2k6CzJ7NYc