javabymanish

Search results

Thursday, 31 October 2013

Chapter - 5 (Java Class and Programming Structures)

Class : -  The class is a logical construct upon which the entire java language is built because it defines the nature of an object. Class is defines a new data type. Once defined, this new type can be used to create objects of that type.Thus, a class is a template for an object and an object is an instance of a class. A class is declared by the use of the class keyword. The general form of a class definition is shown here :

                  class <class-name> {
                         type <instance-variable 1>;                         
                         type <instance-variable 2>;
                         //.........................................;
                         type <instance-variable n>;

                         type methodname 1(parameter-list) {
                             // Body of the method
                          }
                         type methodname 2(parameter-list) {
                                // Body of the method
                          }
                          //......................................
                        
                         type methodname n(parameter-list) {
                                // Body of the method
                          }
                 }

 The data or variables defined within class are called instance variable. The methods and variables defined within a class are called member of the class.

                             class Abc
                                 {
                                      int a;
                                      int b;
                                 }
 Here a and b are instance variables and Abc is a new data type.

Declaring Objects:- 

Declaring objects of a class is a two step process as follows :

1 - You declare a class type variable that refer to an object.

                           Abc a;
                          
2 - In second step you must acquire an actual, physical copy of the object and assign it to that variable with the help of  new operator. The new operator dynamically allocates memory for an object and returns a reference to it.

                                        Abc a = new Abc();

Variables :-

Variables are locations in the memory that can hold values. Before assigning any value to the variable, it must be declared. The content of the memory may be change during the execution of the program.

Types Of Variables :-

1 - Instance Variable :- Variables declared inside the class scope are known as instance variable.

                                                   class Abc
                                                    {
                                                          int x, y;
                                                    }
2 - Local Variable :- These variables are declared inside methods or constructors.

                                        class Abc {
                                           void show() {
                                                 int x=10;
                                               }
                                          }
3 - class variable :- These variables are global to a class and to all instances of the class. The keyword static is used to declare a class variable.

Data Type :-
                                                                           
     


                                                               

Control Statement :- 

A programming language uses control statements to cause the flow of execution to advance and brand based on changes to the state of a program. In java, there are three categories of control statements :
selection, iteration, jump.

Selection Statements :- Java supports two selection statements : if and switch.
1 - 
             if(condition)
              {
                 statement 1;
                ...................
                ...................
                statement n;  
              }
            else
              {
                statement 1;
                ...................
                ...................
                statement n; 
               }

 2 - 
        switch(variable_name)
         {
           case exp1:
                             statements;
                             break;
           case exp2:
                             statements;
                             break;
           ...........................
           ...........................    
           case expn:
                             statements;
                             break;
           default :
                         statements;
                             }

Looping Constructs: - A loop causes a selection of a program to repeated a certain number of times. There are three types of loops : -
1 - while loop :-
                               while(boolean_exp)
                                  {
                                       statements;
                                  }
2 - do-while loop :- 
                                   do
                                     {
                                        statements;
                                     }while(boolean_exp);
3 - for loop:-
                       for(initialization; condition; iteration)
                         {
                            statements;
                         }

 Arrays :- 

Array is representation of data in contiguous space in the memory of your computer. Indexing of an array begins with zero(0). All the variables of an array are same data type. There are three types of array :-

1 - One-Dimensional Array : -  
                                         data_type [] variable_name;
                                         int [] a;
2 - Two-Dimensional Array :- 
                                     data_type [][] variable_name;
                                     int [][] a;
3 - Multidimensional Array :- More than two-dimensional array called multidimensional array.

To create an array in the memory, use the following statement : -

                                        a = new int[3][3];


A simple program in java :-

                            class Abc
                              {
                                 public static void main(String []args)
                                  {
                                     System.out.println("My First Java Program.");
                                   }
                               }

Explanation :-  In java java.lang,  package is by default imported, package , container of the classes and interfaces(discuss later). Every java program must inherited a class named as Object, root class of all java classes.
Here class is a keyword and Abc, name of class. public, is an access specifier, static is a keyword(discuss later), void means no return type and main, is the name of function or method. String, is a class defined in java.lang package and System, is also a class defined in java.lang package and System.out is used output on console and println, is a method defined in java.io package used for print the output.

 
 
 
                         

                          

Wednesday, 23 October 2013

Chapter - 4 (JDK, JVM and JRE)

Before starting the introduction to JDK, JRE and JVM..........We discuss that which types of programming did in java.
So, using java we do three types of programming in java........

1 - Desktop Application   : Desktop application is a program that runs on your computer, under the         operating system of the computer. You can also create Graphical User Interface (GUI) applications. These application is used in the windows environment. This application do not need any kind of server for the execution.

2 - Web Application : Web application is a program that runs on server means that for the execution of web application you must need a server such as Weblogic, apache tomcat,glassfish. etc.

3 -  Mobile Application : Mobile application is a program that is used in mobile phones and also in embedded programming.  

 Types Of Java :

 1 - J2SE (Java 2 Standard Edition) or Core Java - used for desktop application.
 2 - J2EE (Java 2 Enterprise Edition) or Advanced Java -  used for web application.
 3 - J2ME(Java 2 Micro Edition) or Mobile Application - used for mobile application.

Now we are going to discuss the core things in java i.e. JDK, JRE and JVM.............

JDK : JDK stands for Java Development Kit. JDK provides the tools to integrate and execute applets and applications. JDK consists mainly of the core API classes, a java compiler and java virtual machine
interpreter.

JVM : JVM stands for Java Virtual Machine. JVM is used for interpreting java program. JVM is not platform independent.

JRE : JRE stands for Java Runtime Environment. JRE provides an environment for the execution of java applications. Following figure shows the working of JRE :



           



Access Specifier :

                             Access specifier, specify the scope of variables, methods and class. In Java there are four types of access specifier :-

1 - public : Access inside and outside of the package.
2 - default : Access within package only not outside.
3 - protected : Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. Protected can't applied on class and interface.
4 - private : Access within block where it can be defined.


                                         
                                                               

Java Program Compilation Environment:

                                             

                                                                           
 

 Path Setting : For the execution of java program we can set the path. There are following step for set the path :

  • Right click on computer
  • Click on manage
  • Advanced Setting
  • Environment Variables
  • In user variable click on new
  • type path inside variable name
  • type path till bin i.e in my case (C:\Program Files\Java\jdk1.6.0\bin) after this use semicolon
  • then ok   
   Operators : Operator that operate on operands. There many types of operator. Some of them are follows:
  • Arithmetic Operator - Used for arithmetic operation (+, - , /, *, %)
  • Comparison Operator - Used for decision making statements (<, >, <=, >=, !=, ==)
  • Assignment Operator - Used for assign the values (+=, -=, /=, *=, %=)
  • Logical Operator - Also used for decision making statements (&&, ||, !)
  • Unary Operator - Used on single variable. There are two types of unary operator as following :                                                             
                Increment/Decrement Operator :

                                                         

  • new operator : Used for creating object. There are two ways to create an object, as follows : 
                               a) <class-name> <variable-name> = new <class-name>();
                                 Ex.:         Abc a = new Abc();

                              b) <class-name> <variable-name>;
                                  <variable-name> = new <class-name>();
                                Ex.:            Abc a;
                                                   a = new Abc();

                            Where a is called Reference variable.

Java Keywords : Keywords are words reserved for java and can't be used as identifiers or variables names. There are following keywords in java as follows :

                                                                     


Compiling and Executing Java Programs :

                         For compiling java program use the following syntax :
                                              javac <class-name>.java 
                                      Ex. : javac Abc.java

                         For executing the java program use the following syntax :
                                               java <class-name>
                                         Ex. : java Abc

                  

                                                     
                     
                                


                                  
                                                           

Monday, 21 October 2013

Chapter - 3 (An Introduction to Java)

An Introduction to Java


Java is a set of several computer software products and specification form Sun Microsystems(Merge with Oracle), that together provide a system for developing application software and deploying it in a cross-plateform computing environment.
Java was conceived by James Gosling, Patrick Naughton, Chirs Warth, Ed Fronk and Mike Sherfidan, in Sun Microsystems in 1991. The language was initially called 'oak' but was renamed 'Java' in 1995.

Features Of Java :

 1 - Simple, Small and Familiar : 

                                                                  Java was designed to be easy for the professional programmer to use and learn effectively. If you already understand the basic concepts of Object Oriented Programming, learning java will be even easier.

2 - Object Oriented :

                                         Almost everything in java is a class, a method or an object. Any code that you write in java is inside a class.

3 - Distributed :

                               Java was designed for the distributed environment of the internet, because it handles TCP/IP protocols. Java included feature for intraddress-space messaging. This allowed objects on two different computers to execute procedures remotely.

4 - Interpreted and Compiled :

                                                             Java is both compiled and interpreted with a compiler (javac), by this you can translate java program into bytecode (or .class file), it is platform independent code that is interpreted by java interpreter (java).

5 - Robust :

                       The ability to create robust programs was given a high priority in the design of java and it is reliable language. Java program can't cause a crash because it doesn't have permission to access all of you computer memory. It also provides exception.

6 - Secure :

                      This feature is apply in many ways. One of them is also byte-code verification that just occur before interpreting the byte-code by interpreter (java). 

7 - Platform-Independence : 

                                                        Java enables the creation of cross - platform programs by compiling into an intermediate representation called java byte-code. This code can execute on any systems running on different operating system.

8 - Portable :

                         Portable. it means that easy to carry . In java programming, we can carry the byte-code that is generated after compiling the java program on any system and execute there. So our byte-code is like a portable.

9 - High Performance :

                                              Java byte-code efficiently designed so that it would be easy to translate directly into native machine code for high performance by using a Just-In-Time (JIT) compiler. Java is faster than other interpreted language. Since it is compiled and interpreted.

10 - Multithreaded :  

                                        Multithreading is the ability of an application to perform multiple task at the same time.

11 - Dynamic :

                            In java, maintaining different versions of an application is very easy. Java is capable of dynamically linking in class libraries, methods and objects. Java support dynamic memory allocation with automatic garbage collection.