Monday, April 21, 2014

Sample Java Program

Simple Hello Word Program to understand basic keywords

class
Manager {
public static void main(String[] args) {
System.out.println("Hello Words");
}
}

Terminology:
public:
It can be accessible from anywhere.
static:
No need to create the object to run the method.
void:
It means main method is not returning any value.
main:
It’s a method name from where execution will get started.
String[] args:
It’s an array of string type which can be filled by passing run time value if required.
System.out.println:
This statement will print the enclosed the value. In this case After execution output will be Hello Words on the screen.
class:
It tells the compiler where the class starting point with class name.

How to compile the program:
  • Save above java code under your jdk -> bin folder with "Managar.java" name.

  • Go to Command Prompt and go to jdk bin folder location. Command to go to bin location
          cd C:\Program Files\Java\jdk1.6.0\bin and then press enter.
  • javac Manager.java
  • -> It will compile the java program line by line. If there is no error it will not print anything otherwise it will print the error with line number. If program gets compile successfully. One class file will be generated with "Manager.class" name which will hold compiled code which can be executed in anywhere other system also which will hold JDK installed. That’s the reason Java is Platform independent language.

  • Java Manager
  • -> It will execute the java program which shows "Hello Word" as per the current example. It will read the class file which got generated after compilation.
    Note: Here no need to give .class while running the java program. Automatically it reads the class file only.
    

    Memory Management in JAVA


    Javac is a Compiler for compiling Java Classes. Its comiles line by line. If any line is having error like symentic error or sentex error, Java file wont get comiled.
    Java is a Interperator used to run Java Class (Byte Codes). Its run the byte code gets after compilation of the java class.

    How memory management is handled.
    Whey we try to run our java program. Java interperator request to the OS to give some memory for operations. After getting the memory from OS, It divides memory into two parts one is called Stack memory and second is called Heap memory. Stack is used to store the reference name and Heap is used to store the objects. Heap memory have some class level memory we can also say memory for static members.