Thursday 25 June 2015

INTRODUCTION TO JAVA PROGRAMS

Block----> public class class_name
{
public static void main(string args[ ])
{
System.out.println (...);
}
}

Access specifier in JAVA language:
The major job of access specifier is to specify the scoop of the variable (data member), constructor out any class.
Access specifier---> variable, function, class, constructor.

JAVA folder-> package

Boundaries/scope:
within the same class
• within the same package
• outside the package

Types of access specifier in JAVA: public, protected, default(no access specifier), private

Public: within the same class, within the same package, outside the package
Protected: within the same class, within the same package and outside the package (only in inheritance)
Default: within the same class, within the same package
Private : within the same class.

In JAVA, you can't declare a variable or function or any member as global.

Java doesn't support an global member like C or C++, bit if a member is declared as public , it behaves as a global member in JAVA.

Execute that class that only contains the main.
You can't execute more than one class simultaneously.

If the class is declared as public then the class name and file name should be the same.

Almost one public class can be declared and that would be the entry point.

Private and public keywords are not allowed for top level class but nested class can be declared as private or protected.

Class
Class is a collection of similar kind of objects, that is from one class we can create "n" number of objects, all with similar type.
Class is a blue print of the object, I.e skeleton.

Class is a logical container which never allocated any memory.
In JAVA , class is able to contain 4 types of members.
1. Data mamber
2. Method member
3. Block member
4. Constructor member

public class Test
{
  int x; // data member
  Test() // constructor member
  {
  }
  static // block member
  {
  }
  public static void main( string args[]) //method member
  {
  System.out.println("Hello");
}

Javac: to compile the JAVA program.
Javap: JAVA dessemble. It will display all the members present in all user defined or pre defined class.

Class name is the identifier which maybe same as the file name, but should satisfy the rules of the identifier ( data member, object/ reference name, method name, constructor name, class name, Package name )
Space is not allowed in identifier.
Identifier rules:
1. Space is not allowed in identifier.
2. Digits are allowed but the name can't start with digits.
3. Should not be a reserved word. (Keyword)
4. No special symbols are allowed except "_" or "$"

PUBLIC STATIC VOID MAIN
In JAVA, "main" is user defined function, but the signature is predefined.
Compiler never checks the "main" method existence.
But the execution is always started from the main method.

KERNEL calls the main method in C and C++
But JVM( JAVA Virtual Machine ) calls the main function in JAVA.
• JAVA Virtual Machine.
• System software
• Development in C language.
• Platform dependant ( but JAVA is platform independant)

JVM is a system software inbuilt to OS (I.e itbis present by default)
The main job of JVM is to execute the class file, and the execution always starts from the main method.l, as the main method is always called by JVM from the OS, which is outside the package. So main method should be declared as public, else it can't be called from outside.

WHY MAIN METHOD IS STATIC IN JAVA?
Main method is called by the JVM with the name of the class.
If a method is called by the class name, it should be declared as static in JAVA.

No comments:

Post a Comment