Friday, November 30, 2012

INTRODUCTION TO JAVA

JAVA programming Language

* Developed at Sun Microsystems in 1991
*  James Gosling, initially named “OAK”
* Formally announced java in 1995
* Object oriented and cant write procedural programs
* Functions are called methods
* Unit of a program is the class from which objects are created
* Automatic garbage collection
* Single inheritance only
* Each class contains data and methods which are used to manipulate the data
* Programs are small and portable
* Multithreaded which allows several operations to be executed concurrently
* A rich set of classes and methods are available in java class libraries
* Platform Independent
* Case sensitive

Java program development

* Edit – use any editor
* Compile – use command „javac‟   if your program compiles correctly, will create a file with extension .class
* Execute – use the command „java‟
Java language keywords 
* Keywords are special reserved words in java that you cannot use as identifiers for classes, methods or variables. They have meaning to the compiler, it uses them to understand what your source code is trying to do.

   First java Program

 class FirstPro  
                 {    
                            public static void main(String args[])    
                                     {   
                                         System.out.println("Hello World!“);    
                                       }  
                   } 

Java Source files

* All java source files must end with the extension „.java‟ 
* Generally contain at most one top level public class definition 
* If a public class is present, the class name should match the file name 

 Top level elements appears in a file 

* If these are present then they must appear in the following order.
* Package declarations 
* Import statements 
* Class definitions 

 Identifiers 

* An identifier is a word used by a programmer to name a variable, method class or label.
* Keywords may not be used as an identifier 
* Must begin with a letter, a dollar sign($) or an underscore( _ ). 
* Subsequent character may be letters, digits, _ or $. 
* Cannot include white spaces.   ref: Note

Declaring Variables


* Type identifier;

Type identifier=initial value;

Primitive Data Types

Type                           Bits                                          Range 
boolean                         1                                          true, false
char                              16                                        0 to 65,535 
byte                               8                                        -128 to +127 
short                             16                                       -32,768 to +32,767 
int                                 32                                           -232 to +232-1 
long                              64                                         -264 to +264-1 
float                               32                                      -3.4E+38 to +3.4E+38 (approx) 
double                           64                                      -1.8E+308 to +1.8E+308 (approx) 
* int grade; // an integer to hold a grade, no initial value 
*  int grade = 0; // an integer to hold a grade with initial value 0
* char answer; // an answer to something – one character 
* String name; // a string to hold a name
* String name = "Gumboot"; // as above, with an initial value
* boolean finished; // to hold "true" if finished
* boolean finished = false; // as above, but with an initial value

Declarations and Assignments

Declarations   

int grade;
char answer; 
String name; 

Assignments 

grade = 70; 
answer = 'a'; 
name = "alan turing";


Default Values

Data Type                                                            Default Value (for fields) 
byte                                                                              0 
short                                                                             0 
int                                                                                 0 
long                                                                              0L 
float                                                                              0.0f 
double                                                                          0.0d 
char                                                                             '\u0000' 
String (or any object)                                                  null 
boolean                                                                        false

Comments

* Single line comments begin with //  
* Multiline comments begin with /* and end with */ 

No comments: