Showing posts with label Fundamental of Java. OOPs. Show all posts
Showing posts with label Fundamental of Java. OOPs. Show all posts

Saturday, 2 February 2013

Introduction to Control Statements in Java


Control statements decide flow of a program

JAVA CONTROL STATEMENTS

if, if-else, switch, nested if, switch, for, while, do-while, break, continue and return control statements


Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
In Java, control statements can be divided under the following three categories:

Wednesday, 16 January 2013

Java Separators

Separators in Java

Separators used in Java Programming Lanuage

Separators help define the structure of a program. The separators used in HelloWorld are parentheses, ( ), braces, { }, the period, ., and the semicolon, ;. The table lists the six Java separators (nine if you count opening and closing separators as two). Following are the some characters which are generally used as the separators in Java.

Separator
Name
Use
.
Period
It is used to separate the package name from sub-package name & class name. It is also used to separate variable or method from its object or instance.
,
Comma
It is used to separate the consecutive parameters in the method definition. It is also used to separate the consecutive variables of same type while declaration.
;
Semicolon
It is used to terminate the statement in Java.
()
Parenthesis
This holds the list of parameters in method definition. Also used in control statements & type casting.
{}
Braces
This is used to define the block/scope of code, class, methods.
[]
Brackets
It is used in array declaration.
Separators in Java

Friday, 11 January 2013

Java Identifiers

Identifiers in Java

Basics in using Identifiers in Java Programming


Identifiers are programmer-designed tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. Java identifier’s basic rules are as follows:
  1. They can have alphabets, digits, and the underscore and dollar sign characters.
  2. They must not begin with a digit.
  3. Uppercase and lowercase letters are distinct.
  4. They can be of any length.
Identifier must be meaningful, short enough to be quickly and easily typed and long enough to be descriptive and easily read. Java developers have followed some naming conventions.
  • Names of all public methods and instance variables start with a leading lowercase letter. 
              Example: 
                       average
                       sum
  • When more than one word are used in a name, the second and subsequent words are marked with a leading uppercase letters. 
              Example: 
                       dayTemperature
                       firstDayOfMonth
                       totalMarks
  • All private and local variables use only lowercase letters combined with underscores.
              Example: 
                       length
                       Batch_strength
  • All classes and interfaces start with a leading uppercase letter(and each subsequent word with a leading uppercase letter).
Example: 
                       Student
                       HelloJava
                       Vehicle
                       MotorCycle
  • Variables that represent constant values use all uppercase letters and underscores between words.
Example: 
                       TOTAL
                       F_MAX
                       PRINCIPAL_AMOUNT
We may follow our own conventions as long as we do not break the basic rules of naming identifiers. 
The following table shows some valid and invalid identifiers:

Valid
Invalid
HelloWorld
Hello World (uses a space)
Hi_JAVA
Hi JAVA! (uses a space and punctuation mark)
value3
3value(begins with a number)
Tall
short (this is a Java keyword)
$age
#age (does not begin with any other symbol except _ $ )

It is standard Java practice to name multiple-word identifiers in lowercase except for the beginning letter of words in the middle of the name.

Thursday, 10 January 2013

Java Tokens

Tokens used in Java Programs

Reserved Keywords, Identifiers, Literals, Operators, Separators


A Java program is basically a collection of classes. A class is defined by a set of declaration statements and methods containing executable statements. Most statements contain expressions, which describe the actions carried out on data. Smallest individual unit in a program are known as tokens. The compiler recognizes them for building up expressions and statements. 

Java Character Set


The smallest units of Java language are the characters used to write Java tokens. These characters are defined by the Unicode character set, and emerging standard that tries to create characters for a large number of scripts world wide.
The Unicode is a 16-bit character coding system and currently supports more than 34,000 defined characters derived from 24 languages from America, Europe, Middle East, Africa and Asia. However, most of us use only  the basic ASCII characters, which include letters, digits and punctuation marks, used in normal English. We therefore, have used only ASCII character character set (a subset of UNICODE character set) in developing the programs. 

Java language includes five types of tokens and they are:



Monday, 7 January 2013

Data Types in Java

Different Data Types used in Java

Data type defines a set of permitted values on which the legal operations can be performed.

There are two data types available in Java:

  • Primitive Data Types
  • Reference/Object Data Types

Primitive Data Types


     Primitive Data Types defines 8 simple types of data: byte, short, int, long, char, float, double, and boolean. These can be put in four groups.

Integer:

This groups include byte, short, int, and long which are for whole valued signed number. 

Floating:

This group includes float and double, which represents numbers with fractional precision. 

Characters:

This group includes char, which respresents symbls in a character set like letters and numbers. 

Boolean:

This group includes boolean, which is a special type for representing true/false.

Data Type Default Value (for fields) Size (in bits) Minimum Range Maximum Range
 byte  0 8 bits  -128  +127
 short  0 16 bits  -32768  +32767
 int  0 32 bits  -2147483648  +2147483647
 long  0L 64 bits  -9223372036854775808  +9223372036854775807
 float  0.0f 32-bit 1.40129846432481707e-45  3.40282346638528860e+38
 double  0.0d 64-bit  4.94065645841246544e-324d  1.79769313486231570e+308d
 char  '\u0000' 16-bit  0 to 65,535
 boolean  false 1- bit  NA  NA

Reference Data Types


     Reference variables are created using defined constructors of the classes. They are used to access objects. Class objects, and various type of array variables come under reference data type. Default value of any reference variable is null. These non-primitive types are often called "reference types" because they are handled "by reference"--in other words, the address of the object or array is stored in a variable, passed to methods, and so on. By comparison, primitive types are handled "by value"--the actual primitive values are stored in variables and passed to methods. The reference data types are arrays, classes and interfaces that are made and handle according to a programmer in a java program  which can hold the three kind of values as:

    • Array Type
    • class type
    • Interface Type

        Saturday, 5 January 2013

        Creating First Java Application

        Java Simple Hello World! Program

        A program to print the Hello World! message on the Computer screen.


        You can use the notepad as an editor to write your first program. Read the below codes and carefully write on your editor as Java is case sensitive.


        Hello World! code


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

        This is the simple base program of Java. I will explain you line by line to get an idea about the unique features that constitute a Java Program. 

        Class Declaration: class HelloWorld


        This line declares a class, which is an object oriented construct. We must place all codes inside a class in Java. class is a keyword and declares that a new class definition follows. HelloWorld is the Java identifier that specifies the name of the class to be defined. 

        Opening and Closing Braces :{  }


        Every class definition in Java begins wih an opening brace "{" and ends with a matching closing brace "}", appearing in the last line in the example.

        The Main Line: public static void main (String args[])


        This line defines a method named main. Conceptually, this is similar to the main() fuctnion in C and C++; Every Java application program must include main() method. This is the starting point for the interpreter to begin the execution of the program. 

        public : The keyword public is an access specifier that declares the main method as unprotected and therefore making it accessible to all other classes.
        static:  Static declares this method as one that belongs to the entire class and not a part of any objects of the class. The main method must always be declared as static since the interpreter uses this method before any objects are created. 
        void: The type modifier void states that the main method does not return any value.

        The Output Line: System.out.println(“Hello World!”);


        This is similar to a printf() statement in C++. Since Java is Object Oriented, every method must be part of an object. The println() method is member of the out object, which is a static data member of System class. 

        Every Java statement must end with a semicolon.

        Note: We must save this program in a file called HelloWorld.java ensuring that the file name contains class name properly. This file is called the source file. If the program contains multiple classes, the file name must be the class name of the class containing the main method. 

        Compiling the Program


        To compile the Program, we should run the Java Compiler javac with the name of then source file on the command line as shown below:
        javac HelloWorld.java

        Running the Program


        We need to use the Java interpreter to run stand alone program. See the below line. 

        java Helloworld


        Output: 

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        HelloWorld!

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        Thursday, 3 January 2013

        Object Oriented Programming Vs Procedure Oriented Programming

        Difference between Object Oriented Programming and Procedure Oriented Programming


        What is the difference between object oriented programming and procedure oriented programming?


        Procedural Oriented Programming(POP)


        Procedural Oriented Programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. The program is divided into small parts called functions. Full importance is not given to data but to functions as well as sequence of actions to be done. It follows Top Down approach and it does not have any access specifier. When using Procedure Oriented Programming, data can move freely from function to function in the system. However, it is difficult to add data and function on a later point of time. The functions in the Procedure Oriented Programming uses Global data for sharing that can be accessed freely from function to function in the system. Hence the system does not have an option to hide the data and it leads to insecurity.

        Object Oriented Programming (OOPs)


        Object Oriented Programming is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Importance is given to the data rather than procedures or functions because it works as a real world. It follows Bottom Up approach and has access specifiers named Public, Private, Protected, etc. In Object Oriented Programming, objects can move and communicate with each other through member functions. This design provides easy way to add new functions. Data cannot move easily from function to function, it can be kept public or private so we can control the access of data which provides higher data security. There are so many other features available in Object Oriented Programming compared to traditional Procedure Oriented Programming; some of them are as follows. 

        The ability to create GUI (Graphical User Interface) programs

        Through inheritance, we can eliminate redundant code and extend the use of existing classes.

        We can build programs from standard working modules that communicate with one another rather than, having to start writing the code from scratch. This leads to saving of development time and higher productivity.

        Software complexity can be easily managed.