Variable:
Variable is name of reserved area allocated in memory.
Type of Variable.
Local Variable.: A Variable that is declared inside the method is called local variable.
Instance variable: A variable that is declared inside the class but outside the method is called instance variable. it is not declared as static.
Instance variable: A variable that is declared inside the class but outside the method is called instance variable. it is not declared as static.
Static variable: A variable that is declared as static is called static variable. it cannot be local.
Primitive Data Types The primitive data types are predefined data types, which always hold the value of the same data type, and the values of a primitive data type don't share the state with other primitive values. These data types are named by a reserved keyword in Java programming language.
There are eight primitive data types supported by Java programming language :
byte
The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). This type of data type is useful to save memory in large arrays.. We can also use byte instead of int to increase the limit of the code. The syntax of declaring a byte type variable is shown as:
The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). This type of data type is useful to save memory in large arrays.. We can also use byte instead of int to increase the limit of the code. The syntax of declaring a byte type variable is shown as:
byte b = 5; |
short
The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767.short is used to save memory in large arrays. The syntax of declaring a short type variable is shown as:
The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767.short is used to save memory in large arrays. The syntax of declaring a short type variable is shown as:
short s = 2; |
int
The int data type is used to store the integer values not the fraction values. It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more enough to store large number in your program. However for wider range of values use long. The syntax of declaring a int type variable is shown as:
The int data type is used to store the integer values not the fraction values. It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more enough to store large number in your program. However for wider range of values use long. The syntax of declaring a int type variable is shown as:
int num = 50; |
long
The long data type is a 64-bit signed two's complement integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. The syntax of declaring a long type variable is shown as:
The long data type is a 64-bit signed two's complement integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. The syntax of declaring a long type variable is shown as:
long ln = 746; |
float
The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. The syntax of declaring a float type variable is:
The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. The syntax of declaring a float type variable is:
float f = 105.65; float f = -5000.12; |
double
This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values. The syntax of declaring a double type variable is shown as:
This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values. The syntax of declaring a double type variable is shown as:
double d = 6677.60; |
char
The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not integral data type like int, short etc. i.e. the char data type can't hold the numeric values. The syntax of declaring a char type variable is shown as:
The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not integral data type like int, short etc. i.e. the char data type can't hold the numeric values. The syntax of declaring a char type variable is shown as:
char caps = 'c';
boolean
The boolean data type represents only two values: true and false and occupy is 1-bit in the memory. These values are keywords in Java and represents the two boolean states: on or off, yes orno. We use boolean data type for specifying conditional statements as if, while, do, for. In Java, trueand false are not the same as True and False. They are defined constants of the language. The syntax of declaring a boolean type variable is shown as:
The boolean data type represents only two values: true and false and occupy is 1-bit in the memory. These values are keywords in Java and represents the two boolean states: on or off, yes orno. We use boolean data type for specifying conditional statements as if, while, do, for. In Java, trueand false are not the same as True and False. They are defined constants of the language. The syntax of declaring a boolean type variable is shown as:
boolean result = true; |
The ranges of these data types can be described with default values using the following table:
Data Type | Default Value (for fields) | Size (in bits) | Minimum Range | Maximum Range |
byte | 0 | Occupy 8 bits in memory | -128 | +127 |
short | 0 | Occupy 16 bits in memory | -32768 | +32767 |
int | 0 | Occupy 32 bits in memory | -2147483648 | +2147483647 |
long | 0L | Occupy 64 bits in memory | -9223372036854775808 | +9223372036854775807 |
float | 0.0f | Occupy 32-bit IEEE 754 floating point | 1.40129846432481707e-45 | 3.40282346638528860e+38 |
double | 0.0d | Occupy 64-bit IEEE 754 floating point | 4.94065645841246544e-324d | 1.79769313486231570e+308d |
char | '\u0000' | Occupy 16-bit, unsigned Unicode character | 0 to 65,535 | |
boolean | false | Occupy 1- bit in memory | NA | NA |
When we declare a field it is not always essential that we initialize it too. The compiler sets a default value to the fields which are not initialized which might be zero or null. However this is not recommended.
Integer Data Types
So far you would have been known about these data types. Now lets take an Integer data type in brief to better understand:
So far you would have been known about these data types. Now lets take an Integer data type in brief to better understand:
As we have told that an integer number can hold a whole number. Java provides four different primitive integer data types that can be defined as byte, short, int, and long that can store both positive and negative values. The ranges of these data types can be described using the following table:
Data Type | Size (in bits) | Minimum Range | Maximum Range |
byte | Occupy 8 bits in memory | -128 | +127 |
short | Occupy 16 bits in memory | -32768 | +32767 |
int | Occupy 32 bits in memory | -2147483648 | +2147483647 |
long | Occupy 64 bits in memory | -9223372036854775808 | +9223372036854775807 |
Examples of floating-point literals are:
0 1 123 -42000 |
Floating-point numbers
A floating-point number represents a real number that may have a fractional values i.e. In the floating type of variable, you can assign the numbers in an in a decimal or scientific notation. Floating-point number have only a limited number of digits, where most values can be represented only approximately. The floating-point types are
float
and double with a
single-precision 32-bit IEEE 754 floating point and double-precision 64-bit IEEE 754 floating point respectively. Examples of floating-point literals are: 10.0003 48.9 -2000.15 7.04e12 |
Reference Data Types
- Lets have a discussion about Reference Data Types in brief
In Java a reference data type is a variable that can contain the reference or an address of dynamically created object. These type of data type are not predefined like primitive data type. 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// Points to an array instanceclass type// Points to an object or a class instance interface type // Points to an object and a method, which is implemented to the corresponding interface |
As you know that Java is an object-oriented programming language where an object is a variable, associated with methods that is described by a class. The name of a class is treated as a type in a java program, so that you can declare a variable of an object-type, and a method which can be called using that object- type variable.
Whenever a variable is created, a reference to an object is also created using the name of a class for its type i.e. that variable can contain either null or a reference to an object of that class. It is not allowed to contain any other kinds of values. Such type is called reference types in Java. The object becomes an instance when the memory is allocated to that object using new keyword. In addition, array typesare reference types because these are treated as objects in Java. For example:
class Fruit { fColor(){....} fSize(){....}};Fruit mango; Fruit banana; ... |
the behaviors associated with that class as mango.fColor(); within the main method of the super class.
Array Type:
An array is a special kind of object that contains values called elements. The java array enables the user to store the values of the same type in contiguous memory allocations. The elements in an array are identified by an integer index which initially starts from 0 and ends with one less than number of elements available in the array. All elements of an array must contain the same type of value i.e. if an array is a type of integer then all the elements must be of integer type. It is areference data type because the class named as Array implicitly extendsjava.lang.Object. The syntax of declaring the array is shown as:
DataType [] variable1, variable2, .......variableN;DataType [] variable = new DataType [ArraySize]; DataType [] variable = {item 1, item 2,...item n}; |
int [] a = new int [10]; String [] b = {"reference","data", "type"}; |
In the second statement, the array "b" is declared of string data type that has the enough memory spaces to directly holds the three string values. Thus each value is assigned for each index position of the array.
Interface Type:
Java provides an another kind of reference data type or a mechanism to support multiple inheritance feature called an interface. The name of an interface can be used to specify the type of a reference. A value is not allowed to be assign to a variable declared using an interface type until theobject implements the specified interface.
When a class declaration implements an interface, that class inherits all of the variables and methods declared in that interface. So the implementations for all of the methods declared in the interface must be provided by that class. For example, Java provides an interface called ActionListener whose method named actionPerformed() is used to handle the different kind of event . Java also provides a class called Thread that implements Runnable interface.
Thus the following assignment can be allowed:
Runnable r; r = new Thread(); |
No comments:
Post a Comment