Friday, 16 January 2015

Features of java and sample program


How a Java program compiles?

• The source file name must be extended with .java extension. e.g. Myprog.java
• Execute the javac compiler.
• javac compiler creates a file called Myprog.class i.e. the bytecode version of Myprog.java.
• The bytecode is executed by the Java runtime-systems which is called Java Virtual Machine (JVM).
• JVM is platform dependent.
what is the difference between Procedural and OOPs?
Procedural
->programming logic follows certain procedures and the instructions are executed one after another
-> data is exposed to the whole program
Oops
-> unit of program is object, which is nothing but combination of data and code

Features of Java
The basic features of Java are given below :
• Java is simple.
• Java provides immense security.
• Java provides high portability.
• Java provides object oriented programming features
• Java provides robustness.
• Java is multithreaded.
• Java provides architecture neutrality.
• Java is distributed
• Java is dynamic

How java becomes object oriented?

• Java follows the paradigm of OO programming.
• Java follows modular approach.
• Java follows the abstraction aspect.
• Java follows the OO principle encapsultaion.
• Java follows the OO principle polymorphism.
• Java follows the OO principle inheritance.


Some of the explanations why java features are important
1.Simple:-
Java is a simple programming language.Because of 2 reasons.
1)Java Soft people maintained the same syntax of C and C++ in  Java.
2)Pointers are eliminated in Java.
II.  Why pointers are eliminated from Java?
1)Pointers can crash program very easily.
2)Using pointers,it is possible to develop harmful programs like virus and hacking programs. So Pointers are security threat for java.   Because of these reasons pointers are eliminated from java.

2) Object-Oriented:-
Java programs use Objects and classes.
    Object:- An object is anything that exists physically in this   world.
              An object will have properties(variables) and actions(methods).
    variable:- It represents memory location to store data.
     
     Class:- It is a group name that specifies properties and actions  of objects.
        ==> class also contains variables and methods.
==> Class is only specification but object exist physically.
==> Class is a model for creating Objects.

3) Distributed:-
Information is distributed on various computers on a network.
Using java,we can write programs which capture information and           distribute it to the clients.

4) Robust:-
      Robust means strong.Java programs are strong and they don't crash easily like c and C++.
There are 2 reasons for this.
1)Java has got excelent exception handling features.
2)Memory management features.
         Here memory allocation is done by JVM's Class loader subsystem.
      Memory deallocation is done by JVM's garbage collector.

5) Secure:-
Security problems are eleminated by using java on Internet.

Encapsulation:
-> that binds together code and data it manipulates and keeps both safe from outside interference and misuse.
Inheritance
-> is the process by which one object acquires the properties of another object.
Advantages:
1)      reusability of code and
2)      accessibility of variables and methods of the super class by subclasses.
Polymorphism
->Polymorphism is the feature that allows one interface to be used for general class actions.

• the access specifier is the 'public' keyword .
• 'static' keyword allows main() to be called without instantiating a particular instance of a class.
• 'void' affirns the compiler that no value is returned by main().
• 'main()' method is called at the beginning of a Java program.
• 'String args()' tells a parameter named args,which is an instance array of class String

What 'System.out.println()' signifies?

• 'System' is a predefined class .
• System class gives access to the system.
• 'out' is the output stream.
• 'println' is printing the line on the console.
• This is a console output statement.
what is the difference between print() and println()?
print()- method displays the result and keep the cursor in the same line.
println()- method displays the result and then throws the  cursor to the next line.

public String getName(String st)
 public: Modifier
String: return type
getname()- method name

What is JVM?

• JVM is the acronym stands for 'Java virtual machine'.
• JVM provides the execution environment.
• JVM is not platform independent..
• JVM is the Java run-time system.
• JVM is an interpreter of bytecode.
• JVM also makes the sytem secured.

What is bytecode?

• Bytecode is an instruction set.
• Bytecode extends wiith .class.
• 'javac' compiler translates the .java file into .class.
• JVM interpretes bytecode.
• Bytecode facility makes Java platform-independent.
• It also confirms security tothe Java code.

The following steps to be taken when you writing a java program:-
-----------------------------------------------------------------
Comment:-
       It represents description about all the features of a           program.  (or)
 Un executable lines in a program.
There are 3 types of comments in Java.
1. //single line comment
2. /* multiple line--
          ---comments------
          ------------------*/
3./**--java documentation
         ----comments-----------
         ---------------------*/  --> they are useful to create API   document from a .java program
importing classes:-
java library-->packages-->classes/interfaces-->methods

package:-
It is a sub directory which contains classes and interfaces.
   ex:- import java.lang.System;
import java.lang.String;
-->If you want to import several classes of same package then we write import java.lang.*;

what is the difference between #include and import?
#include makes a c (or) c++ compiler to copy entire header file code into the program.Thus the program size increases    unneccessarily and it wastes memory and processor time.
Import statement makes JVM to go to the Java library,and   execute the code there and copy the result into the java program.
So import is better mechanism than #include.

what is JRE (Java Runtime Environment)?
jre = jvm + java library.

We can call a method in java in 2 ways.
1.create an object to the class to which the method belongs.
     classname objectname = new classname();
 calling the method like this:
     objectname.methodname();

2. classname.methodname();
   The second method for static methods only.

static method:- Static method is a method that can be called and executed without using any object.

==> print method belongs to PrintStream class.
   There is a shortcut available to create the object of PrintStream     class.That is System.out (i.e: monitor).
    Here out is a variable (or) field in System class.
    field is nothing but static variable.

           \n    next line
           \t    Horizontal tab space
           \r    enter key
\b                    Back space
\\    displays \
\"    displays "
\'    displays '

-> Java is case sensitive language.
-> Every statement should end with semicolon.

In java we should follow naming conversions(rules):
Naming Conversions
1)Package names in java are written in all small letters.
   ex:-  java.awt
          java.io
        java.swing

2) Each word of class names and interfaces start with a capital.
   ex:-  String,DataInputStream,ActionListener

3) Method names start with small letter,then each word start with a     capital.
ex:- println(),readLine(),getNumberInstance(),etc..

4)Variable names also follow the above rule.
ex:- age , empName, employee_Net_Sal

5)Constants should be written using all capital letters.
ex:- PI , MAX_VALUE , Font.BOLD (Here Font is class)

6)All keywords should be written in all small letters.
ex:- public , void , import



java lang is the default package that is imported in to every program

Important packages of core java:

1.java.lang :-
   This package got primary classes and interfaces essential for java  program it consists of wrapper classes (useful to convert ordinary    data in to objects),strings,threads.

2.java.util:-
       This package contains useful classes and interfaces
     like Stacks,LinkedList,HashTable,Arrays

Source file's elements (in order)
Package declaration
Import statements
Class definitions

  • At most one public class definition per file. This class name should match the file name. If there are more than one public class definitions, compiler will accept the class with the file's name and give an error at the line where the other class is defined.

  • const and goto are reserved words, but not used.
  • main method can be overloaded.

  • main method can be final.


A naming convention is a rule to follow as you decide what to name your identifiers e.g. class, package, variable, constant, method etc.

No comments:

Post a Comment