20 August 2012

Tutorial 3

Java Tutorial #3

Java Programming Structure

Document section (suggested)
Package statement (optional)
Import statement (optional)
Interface statement (optional)
Class definition (optional)
Package statement (optional)
Main method class
{
Main method definition
}

 1.1 Document section- comprises of set of comment lines giving the name of program ,author, & other
  details which programmer would like to refer to at later stages

 1.2Package statement declares a package name & inform the compiler that the classes defined here
 belongs to this package
 e.g=> package author

 1.3Import statment we can have access to the classes that are not part of our package

 1.4Interface statement we can have access to the classes that are not part of our package

 1.5Main method  is same as in C++ from where the program execurtion starts and end with theend of
 main method
 Friends,always keep in mind that java is case sensitive ,so int radius & int RAdius are two different  
 variable (or identifier) not the same one
 The character used in java are defined by the unicode charecter set, which is a 16bit character coding 
 system. So char in java takes 2byte space unlike 1byte space in C or C++

 Keywords java language has 50 reserved keywords that are never been used as identifier. all keywords are
  given in lower case letter .
                     For more information about keywords Click here
 
Secret of java platform Independence
     Different language compilers translates source code into machine code for a specific computer. On the
 other hand, java compiler produces an inermediary code known as bytecode for a machine called
 java-virtual machine, that exists inside the computer memory and simulates computer within computer that
 converts bytecode into machine specific code. As bytecode of all the system are same and hence java is
 platform independent.


 Command line argument
Command line argument is one of the nice way of passing the data at runtime at the time of compilation.
Public static void main(String srgs[])
Here args is declared as an array of strings (known as string object).Any argument provided in command line (at time of execution) are passed to the array args as its element that can be used in the program



  Use of Command line argument

 
               /*
         * This program uses command line argument as input
                */
                Class static void main(String args[])
                {
                 int count , i=0;
                    String string;
                    count =args.length ; //inbuilt function to count the 
                                                //number of argument passed by the
                                                // user at run time
                    System.out.println("Number of arguments ="+count);
                    while(i < count)
                    {
                     string = args[i];
                        i+=1;
                         System.out.println(i+":"+"Java is"+string+"!");         
                    }
                }
 
 Data-type supported in java


  Integer type
  java doesnot support the concept of unsigned type,so java integer value are either positive or negative

 Floating point
 is treated as double precision quantities(double). To force them to be in single precision mode we must
 append f or F to the number

 NOTEAll mathematical function such as sin,cos,sqrt return or take double type value Floating point data
 type support a special value known as Not-a-Number(NaN), that is used to represent the result of
 opertation such as divie by zero where actual number is not produced

 Character type is same as in C or C++ or any standard procedural or object oriented language

 Boolean type  is denoted by the keyword boolean and uses only 1 bit of storage that can take only two
  values "true" or "false"

 NOTEIt should be remembered that wider data type requires more time for manipulation and therefore it is
  advisable to use smaller data type whenever possible
 one of the great advantage of java is that if a variable is not initialzed it is autometically set to zero(or the
  standard default value of that primitive data type)
Typecasting
  converts one primitive data-type to another primitive data-type
    Automatic conversion
    is possible only if the destination type has enough precision to store the source value
    eg=> byte b=75;
    int a=b; s valid statement
    Note that we need to explicitely cast if we are to convert a more precised value to the less precised one
    Syntax
    type variable1=(type) variable2;

No comments:

Post a Comment