Saturday, 12 December 2015

How does System.out.print() work in Java

Java Tutorial
How does System.out.print() work in Java?
To understand System.out.print() we need to understand the methods in Java. In Java there are two type of methods:
1.       static method
2.       Instance method (or non-static method)
Static methods can be called by class name. for example:
class staticMethodClass
{
        //User defined static method
        public static void staticMethod()
{
        System.out.println(“Inside static method”);
}
//main method. Its n entry point for java class. You can read more about main method here.
public static void main(String args[])
{
        //calling static method using class name classname.methodname();
        staticMethodClass. staticMethod();
}
}


o/p Inside static method

To call a non-static method we need to create an object first and then we can invoke the non-static method inside static method. We will discuss the scope of static and non-static methods later.
For ex:
class nonStaticMethodClass
{
        //User defined non-static method
        public void nonStaticMethod()
{
        System.out.println(“Inside non-static method”);
}
//main method. Its n entry point for java class. You can read more about main method here.
public static void main(String args[])
{
        //object creation
        nonStaticMethodClass object1=new nonStaticMethodClass();
        //calling non static method through object
        object1. nonStaticMethod ();
}
}


o/p Inside non-static method

Now I am coming to the discussion of how System.out.print() works in java.
Here print() is a method and it comes under PrintStream. Now question will arise that if print is a method of PrintStream class then why can’t we create an object of PrintStream and call print method just like below:

PrintStream object=new PrintStream();
Object.print();
  
Ans is we can’t create a direct object of PrintStream class. An alternative is given to us i.e System.out
Here System is class name and out is a static variable in System class. When we call out, a PrintStream  object will be created and through which we can call print() as System.out.println()

Below code snippet will explain you in better way how out is being declared in System class and how print() in PrintStream class
//the System class belongs to java.lang package
class System {
  public static final PrintStream out;
  //...
}

//the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}


Friday, 11 December 2015

Comments in Java

Java Tutorial
First steps to Java Programming
Comments in Java:
First of all we need to understand what is the use of comments in java. Suppose you are joining a company as a developer and you got an existing project, now if you are going through the code, It will take hell lot of time to understand the coding done by someone else.  But if that person has followed the stander procedure then he defiantly has kept some comments so that anyone can understand the code in better way.
There are 3 ways to keep comments in Java:

1.    Single Line comment

// single line comment
Here we can keep comments in single line


2.     Multi Line comment

/* Multiple line comment. Here we can keep n no of lines of comment.
This is 2nd line.
This is 3rd line */


3.     Java documentation comment

/** Java documentation comment */
These comments are used to provide the description of every feature in a java programming.
This description use to create a .html file called API (Application programming Interface) document.

               

 Command to generate the .html file is:

 javadoc className.java

Thursday, 10 December 2015

Key features of JVM (Java Virtual Machine)

Java Tutorial:

Key Features of JVM (Java Virtual Machine):

Q1. Which part of JVM allocates the memory for Java Program?
When we execute a java program, Class loader of JVM will allocate the necessary memory.

Q2. How Java is platform independent?
In any other language only complier or interpreter is being used to execute a program, but in java 1st code will be converted to byte-code by Java compiler and from .java to .class (.class is nothing but byte code) file will be created. Once .class file created, Interpreter will convert this .class file to machine language, and machine language will be executed by microprocessor, and provides the result back.
As this .class file is portable, so we can take this .class file to any other machine. If we will run this, we will get same answer.

Q3. What is garbage collection is Java? Can we explicitly call garbage collection?
Garbage collection is a process which is already implemented in java. Here if an object or variable is not being used, then It will be automatically removed by Garbage collector of JVM.
Yes we can explicitly call garbage collector by using System.gc()
Note: Garbage collection in java performed on the basis of many algorithms but most frequent one is Mark and Sweep.    

Q4. What is JIT (Just-In-Time) complier?
JIT (Just-in-Time) compiler is a program that converts byte code into instructions that can be sent directly to the processor. In another words, JIT compiler is a program which convert java byte code in a format that can be easily processed by processor.

Q5. In JVM which is use to convert byte code into Machine language Interpreter or JIT compiler?
In general either Interpreter or JIT compiler used to convert byte code into machine code, but in Java both are being used. For more clarification we will see below instructions:
Assume below code is a byte code instruction.
                                   Print Rajnish
                                   Print Rajnish Again
                                   For i=1 to i=10
                                          Print Rajnish Once Again
Now first interpreter will execute 1st instruction and Print Rajnish will be converted to machine code,( to convert this suppose Interpreter took 2 nanoseconds) and this will be given to microprocessor.
Again Interpreter comes back in memory and reads 2nd instruction Print Rajnish Again, It will again take 2 nanoseconds to convert 2nd instruction into machine code, and then 2nd instruction will be given to microprocessor to execute.
Again Interpreter comes back and reads 3rd instruction which is a loop, so it will be repeated 10 times.
Interpreter will read Print Rajnish Once Again and will convert it again into machine code in 2 nanoseconds. Total 10 repetitions are there so 10*2=20 nanoseconds for 3rd Instruction which is not time efficient. So JIT comes into picture.
JIT will convert Print Rajnish Once Again into machine code in 2 nanoseconds and will place it in memory. Let’s say placing the result into memory will take 2 nanoseconds so total 4 nanoseconds and now processor will take the result from memory and execute it 10 times.
Here we saw how interpreter taking 20 nanoseconds for a task where JIT taking only 4 nanoseconds for the same task.
Note that 1st and 2nd instruction won’t be assigned to JIT because again it will take 4 nanoseconds for each task (converting bytecode to machine code 2 nanoseconds and placing result in memory 2 nanoseconds) where Interpreter will take only 2 nanoseconds for each tasks.

So for above example we can easily see how Interpreter and JIT is being used simultaneously in JVM to speed up the execution.




Thursday, 3 December 2015

Java Object Oriented Programming concept

Java Tutorial
Java Object Oriented Programming concept:
If you are new to Object Oriented Programming then you need to learn few basic concepts before We starts. I will try to correlate these concepts with real world and simultaneously I will provide the java syntax for the same.   
First thing, We need to make up our mind that, here in java everything works with object. As java is a pure Object oriented programming language, we required at least a class and object to invoke the variables and methods of that class.
Before we starts with Java programming we need to understand the below basic concepts:


What is Class: A class is a Blueprint or instruction of how to provide a specific service.
Java Syntax:
class className{
------------------
}  
What is Object: Object is a thing that practically exists. For example every human, Trees, fruits, Animals are Object.
Java Syntax:
className objectName=new className();
Note that Class name and object name can be anything. Its upto you, what you want to give class name or object name.
What is Inheritance: Inheritance is a concept of inheriting (taking) the property of parent class.
Real life example is some people use to work with left hand as one of the parent is lefty. So that person has inherited the property from his parent.
Java Syntax:
class A{
-------------
}
class B extends A{
--------------
}

What is Polymorphism: when one task will be performed in different way, Its known as polymorphism.
Ex: method overloading and method overriding. We will discuss these topics later in detail.

What is Abstraction: Abstraction is a concept where internal details will be hidden to the outer world and only functionality will be exposed.
For ex: ATM machine, We use to withdraw cash from ATM machine in day to day life but we don’t know its internal details.
Java syntax: In java abstract keyword is used to make a class abstract.
What is Encapsulation: Encapsulation is a concept of binding or wrapping the data and code together.

Real life ex: capsule (a medicine), it is wrapped with different medicines. 

Wednesday, 3 June 2015

Helloworld Example Java

First of all we are going to see an example of hello world:



Java is an object oriented programming language.
First Line public class HelloWorld defines a class called HelloWorld.

public: its a access specifier and if its public then this class is accessible from any where. i.e. from any directory, any package, any application.

class: class is a keyword and which is used to declare a class. Java is an OOP language so each and every thing in java should be inside class.

HelloWorld: HelloWorld is a class name. You can give any name as class .

Note that if a class access specifier is public than it should always saved by className.java
here class name is HelloWorld and its access specifier is public so this Java file should be saved as HelloWorld.java.


public static void main(String[] args): main method is entry point for a Java class. It should be always static.

System.out.println( ): 

System: System is a class of java.lang package
out: out is a static member of System class and is an instance of java.io.PrintStream
println: println is a method of java.io.PrintStream. println method is overloaded to print the message in console.

Why main method is public static in Java

Syntax of java main method is:

public static void main(String[] args)
{

}

reason for public static void main are:

public: This is because public access specifier is accessible from any where. i.e. inside or outside of the application. And main method is entry point of a class so it should be accessible from anywhere.

static: As main method is an entry point of Java program, while invoking main method there wont be any object. That's why static is required for main method.
we all know while we want to invoke a method there should be an object or method should be static. As for static method no need of object, using class name we can invoke a static method.

void: Return type of main method is void, because Java is platform independent and if main method return any value then meaning of that value can be different in different system.

String[] args:   String[] args is an array of java.lang.String type.And this is specially used to store values received from console.

We all know that Java only deals with String, because we can store any types of value in string.
Thats the another reason for using String array as argument of main method.

Note that args is just a name you can give any name to this. ex: public static void main(String[] variable) and it will work
for ex: String s="123";
and easily we can convert 123 to no.