Monday, March 21, 2011

JAVA Programming : Data Types

JAVA Data Types


Java has a number of data types below is the complete list of them . Although syntax of Java is largely derived from C++ but datatypes are a bit different from C for example C does not have Boolean data type. Java Datatypes have a fixed size and also some default values that will bet set to variables if they are declared but not initialized.

  • Boolean : Boolean datatype does not have a specific size declared but conceptually it has one bit value. Boolean datatypes has only two possible values true or false. Default value for Booleans are false.
    Size:- not defined 
    Values:- true or false 
    Default value:- false
  • Char: A char is 16 bit value that can be treated like an integer value but is intended to hold characters. In a Java program a char datatype is always interpreted as a numeric value of a displayable character. 
    Size:- 16 bit
    Values:- from 0 to 65535
    Default value:- 0
  • Byte: A byte is an 8 bit integer value.
    Size:- 8 bit
    Values:- from -128 to 127
    Default value:- 0
  • Sort: A sort is an 16 bit integer value.
    Size:- 16 bit
    Values:- from -32768 to 32767 
    Default value:- 0
  • Int: A int is an 32 bit integer value.
    Size:- 32 bit
    Values:- from -2147483648 to 2147483647
    Default value:- 0
  • long: A Long is an 64 bit integer value and it is the largest of all the integer types. 
    Size:- 64 bit
    Values:- from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    Default value:- 0
  • float: A float is 32 bit single precision floating point decimal number.
    Size:- 32 bit
    Values:- from -1.4E-45 to 3.4028235E38 
    Default value:- 0.0
  • double: Double is 64 bit double precision floating point decimal number.
    Size:- 64 bit
    Values:- from -4.9E-324 to 1.7976931348623157E308 
    Default value:- 0.0

Friday, March 18, 2011

JAVA Programming : Hello World

JAVA Hello World Program



A Java Hello World program is the traditional program that you will find everywhere on the web and this  program is probably the simplest program that you can write and make it actually do something. Hello World program doesn't do much, it just displays one line of text on the screen but it is a complete java program that will actually compile and run.

Source Code of Hello World Java program:



   public class HelloWorld {

       public static void main(String args[]){       

           System.out.println("Hello World");   //prints a line on screen
       }

   }



A Closer look at Hello World Program :
The first line of the code declares a public class named HelloWorld. Declaring a class public means that it can be accessed by name form any place (talk more about Access specifiers letter). In second line we declare a main method. Every class file that can be executed form command line should have a main method just like this one. When ever we run a Java program the compilers looks for a main method to starts with. A main method is always declared as public so that it can be accessible from any where and static so it exist without a object being created. The main method takes a string array argument as parameter. We can pass a string array from command line at the time of running java program. Now inside the main method there is only one line of code that calls to the println method that prints a line of text.
The text that written after the "System.out.println("Hello World");" is known as comment. A Comment is not part of the actual code i.e. the compiler will not compile a comment. Comments are only used for increasing the readability of code. To make something a comment use two forward slashes(//) in front of it.


Thursday, March 17, 2011

JAVA Programming : Naming Rules and Conventions

JAVA Naming Rules and Conventions


Before we start writing any Java Program or code we must know about some naming rules and convention of Java, so first we talk about some rules.

Java Naming Rules: Below are the set of rules that you must follow to write a error free code.

Java is Case Sensitive language that means that upper case latter and lower case latter are two different things. In  other words we can say that in Java there are 52 characters (26 upper and 26 lower case). For example JAVACODESPOT , JavaCodeSpot and javacodespot will be treated as different identifiers in Java.

First Character of any name can be any of these:
 a-z (any lower case alphabets)
A-Z (any lower case alphabets)
 $ (A dollar sign)
 _ (Underscore sign)
Note: the first character of any name can not be digit (0-9)
After first character a name can be anything that a first character can be and it can have a digit or number (0-9) also.

Java Naming Conventions: Bellow are the set of standards that are given by Sun that programmer should follow to make their code easier to read for themselves and for other programmers.

For Classes and Interfaces the first letter of the name should be capitalized and if the name is made up with several words, start of every word should be capitalized for example Dog, CamelCase.

For Methods and Variables the first word of the name should be lowercase and if the name is made up with several words, start of every word except first word should be capitalized for example dog, camelCase.

For Constant there is no predefined type in Java but you can have a constant by declaring variable static and final. A Constant name should be of uppercase and if the name is made up with several words the underscore should be used between the words for example DOG, CAMEL_CASE.

For Packages names should be in lowercase and full stop (.) as separator.


JAVA Programming : Methods

JAVA Methods



A Java method contains set of Java statements which is declared inside a Java class. Methods are where class logic is stored or where the actual work gets done. Java methods are similar to functions or procedures in other programming languages.

A Java method declaration various parts like Modifiers, Return Type, Method name, parameter and and there is one more thing exceptions that can also be part of a method.

Syntex of a Java Method :

    Modifier ReturnType MethodName ( Parameters ){  
              // set of java statements                                    
    }


Example of a Java Method :


  public int giveSum (int a, int b){       

    int ans = a + b;
    return ans;  

  }



Once a method is defined, it can be called with in a Java class just by writing name of the method followed by parameters in the brackets according to method definition and followed by a semicolon. For example if above method giveSum is to called then :


   giveSum(10,20);  //calling a method