Saturday, August 16, 2014

Java Data Types

Data Types specify the size and type of values that can be stored. These are the basic data holders in a programming language. All the complex data holders are built using these basic data types.
The two categories of datatype in java:
  • Primitive data types.
  • Reference data types.
Primitive data types

These can store  a single value at a time. Java has eight primitive types of data types.
byte, short, int, long, char, float, double, and boolean.

These can be put in four groups:
  • Integers - byte, short, int, long
  • Floating-point - float, double
  • Characters - char
  • Boolean - boolean
Data Type Explanation
  • int - It is used to store large numbers. Its size is 4 byte. Unlike other programming languages where size of int is platform dependent, in Java the size is fixed. The range is -2,147,483,648 to +2,147,483,647
  • short - It is used to store smaller number .Its size 2 byte.The range is -32768 to +32767.
  • long - It is used to store very large number.Its size is 8-byte. The range is 9,223,372,036,854,775,808 to +9,223,372,036,8554,775,807.
  • byte - It is used to store small amount of data.Its size is 1-byte. Range is -128 to +127.Useful when working with a stream of data from a network or file. Byte variables are declared by use of the byte keyword. e.g ~~#FF0000: byte b;~~
  • float - It is used to store number with  decimals. Its size is 4-byte. Range is -3.40292347E+38 to +3.40292347E+38.
  • double - It is used to store large number. Its size is 8-byte. Range is -1.7976931E+308 to +1.796931E+308.
  • char - It is used to store names or character data. Its size is 2 byte. Its range is u0000 to \uFFFF.
  • boolean - the valid value for a boolean is 'true' or 'false' value. Its size is 1 bit.
Reference Data Types
  • Array - A collection of variables with same data type. E,g. names of student.
  • Class - A collection of variable and methods.E.g: class ''Student'' containing the complete details of student, and method that operate on the detail.
  • Interface - A collection of method/behavior which is implemented by class.
It is used to store data.These are declared before they can be used. There is no limit of length of variable name.
Syntax is - <Datatype> <identifier> = <value>;

  • Datatype - It may be Primitive or Reference  datatype.
  • Identifier'- The name of the variable. It should begin with a letter, an underscore(_),or dollar sign($).
  • value - It is asigned to variable. It is optional.
e.g int a=12;
int - datatype
a - variable
12 - value.

Scope of Variables
Java variable are classified into 4 kinds.

  • instance variable(Non-Static fields) : objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.  
  • class variable (Static Fields): A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. ''The code static int numGears = 6; '' would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change. It is declared inside the class.These are global to the class.
  • local variable :Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field ''(for example, int count = 0;)''. There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.  
  • Parameters : Recall that the signature for the main method is public static void main(String[] args). Here, the ''args ''variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields".
An array is a variable that stores data of same data type in consecutive memory location.Once the size of array is declared, it can't be changed.If an array is not initialized at the time of creation then all the elements are initialized by default values. The index number of the first element in an array  start from zero.

Three  step in creation of array.
  • Declare an array.
  • Create memory location.
  • Put values into memory location.
char ch[];   //declared a character array  named ch.

char ch[] =  new char [10];  //declared an array ch to store 10 element.
char ch[]={'a','b','c','d'}; //declared an array ch to store 4 preassigned character element.

Array is declared in two forms:
  • type arrayname[]; Ex. int num[];
  • type []arrayname;[] avg;
Two Dimensions Array
Fro example
int[][] multi=new int[3][4];
 array having [3] rows and [4] columns.
Arrays can be multidimensional also. For example
int integerArray[3][4][7][9]

No comments:

Post a Comment