4.Java Data Types
Sunday, August 9, 2009
In this chapter we are going to learn how the user input will be stored in memory, in particular how memory will be arranged. In general there are five different places to store user data.
1. Registers: This is the fastest storage place in a computer, it resides inside the processor. Registers are allocated by the compiler according to its needs. We don’t have direct control or we cannot see any evidence in our programs that registers even exists.
2. Stack: The stack resides in the RAM (Random Access Memory) in the computer. It has direct support from the processor via its stack pointer. The pointer can move down to create new memory and moved up to release that memory. This is very fast and efficient way to store memory but only second to registers.
3. Heap: This is a pool of memory resides in RAM, where all Java Objects will be stored. Unlike the stack the compiler doesn’t need to know how much memory it needs, so the great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it using new and the storage is allocated on the heap when that code is executed.
4. Static Storage: This is used for fixed storage purpose, this is also resides in the RAM. Static storage contains data that is available for the entire time a program is running. We can use the static keyword to specify that one particular Object is static. Java Objects are never placed in static storage.
5. Constant Storage: .These values are placed directly in the program source code, which is safe since they can never change. So that they can be optionally placed in read-only memory (ROM).
Java Data Types
Java provides the fallowing data types,
1. Primitive or the simple data types
2. Abstract or the derived data types
Primitive Types: This is quite often used in java programming; these are defined (almost) identically on every machine on which Java runs, unlike other programming languages. Java is a strongly typed language which means every variable must have a declared type.
Java has 8 primitive or built-in data types and 4 integer types (byte, short, int, long) along with 2 floating point types (float, double) .In order to use True or False java has Boolean (boolean) , to represent character type date there is a type called Character (char) .
Java Primitives are not objects.
To calculate Speed of light we can use double, to find out the grade of a student in a course we can use char, to calculate grade point average this term double/float, to find out number of refrigerators in a room int, to locate a point on a screen we can use float/int.
Here is an example to demonstarate Pre and Post Increment Operators,
Using Java Data Types : Take a look at the fallowing program, to use different data types
Abstract Data Type: These are derived from the primitive data types, for example Strings. Strings stores letters, digits, and characters such as /, (), :, :, $, and #.
Variable: A variable is the name that refers to a memory location where some data value is stored. Each variable that is used in a program must be declared.
Naming Conventions for Variables:
1. The name of a variable needs to be meaningful, short, and without any embedded space or symbol.
2. A variable name must be unique.
3. A variable name must begin with a letter, an underscore (_), or the dollar symbol ($), which can be followed by a sequence of letters or digits (0 to 9), ‘$’, or ‘_’ .
4. A variable name should not start with a digit.
5. A variable name should not contain embedded white spaces .
6. A variable name should not consist of a keyword.
7. A variable name in Java is case sensitive.
In order to manipulate variables we can use Assignment Operator, we use the assignment operator (=) to assign a value to a variable. Java supports the fallowing operators,
Assignment is not the same as equality!!!
= is not the same as ==
Assignments are expressions: for this have a look at this snippet
Forms include +=, -=, *=, /=, &=, ^=, =, %=
1. Arithmetic Operators: These operators are used to perform arithmetic operations like addition, subtraction, multiplication and division etc.For more details have a look at the fallowing diagram.
Declaring Variables
In the fallowing example, we are going to demonstrate the “Pre and Post Increment” operators with suitable examples.
Have a look at the fallowing example also,
2. Logical Operators: By the help of logical operators we can produce the type of result either True or False (which is of type boolean) there are eight type of operators available in this category.
For example have a look at this fallowing code snippet
There are also “bitwise” operators; we won’t use these much (if at all), I am going to discuss bitwise operators in another session.
1. Registers: This is the fastest storage place in a computer, it resides inside the processor. Registers are allocated by the compiler according to its needs. We don’t have direct control or we cannot see any evidence in our programs that registers even exists.
2. Stack: The stack resides in the RAM (Random Access Memory) in the computer. It has direct support from the processor via its stack pointer. The pointer can move down to create new memory and moved up to release that memory. This is very fast and efficient way to store memory but only second to registers.
3. Heap: This is a pool of memory resides in RAM, where all Java Objects will be stored. Unlike the stack the compiler doesn’t need to know how much memory it needs, so the great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it using new and the storage is allocated on the heap when that code is executed.
4. Static Storage: This is used for fixed storage purpose, this is also resides in the RAM. Static storage contains data that is available for the entire time a program is running. We can use the static keyword to specify that one particular Object is static. Java Objects are never placed in static storage.
5. Constant Storage: .These values are placed directly in the program source code, which is safe since they can never change. So that they can be optionally placed in read-only memory (ROM).
Java Data Types
Java provides the fallowing data types,
1. Primitive or the simple data types
2. Abstract or the derived data types
Primitive Types: This is quite often used in java programming; these are defined (almost) identically on every machine on which Java runs, unlike other programming languages. Java is a strongly typed language which means every variable must have a declared type.
Java has 8 primitive or built-in data types and 4 integer types (byte, short, int, long) along with 2 floating point types (float, double) .In order to use True or False java has Boolean (boolean) , to represent character type date there is a type called Character (char) .
Java Primitives are not objects.
To calculate Speed of light we can use double, to find out the grade of a student in a course we can use char, to calculate grade point average this term double/float, to find out number of refrigerators in a room int, to locate a point on a screen we can use float/int.
Here is an example to demonstarate Pre and Post Increment Operators,
public class DataTypes
{
public static void main(String\[] args)
{
boolean isReal=true; // Names are case sensitive,
// start w/letter, have nos,_,$
byte d= 122; // Must be less than 127
short e= -29000; // Must be less than 32767
int f= 100000; // Must be less than 2.1 billion
long g= 999999999999L; // Must put L on end
float h= 234.99F; // Must be \< i=" 55E100;" cvalue=" '4';" name=" “Claudius”;">
Using Java Data Types : Take a look at the fallowing program, to use different data types
Abstract Data Type: These are derived from the primitive data types, for example Strings. Strings stores letters, digits, and characters such as /, (), :, :, $, and #.
Variable: A variable is the name that refers to a memory location where some data value is stored. Each variable that is used in a program must be declared.
Naming Conventions for Variables:
1. The name of a variable needs to be meaningful, short, and without any embedded space or symbol.
2. A variable name must be unique.
3. A variable name must begin with a letter, an underscore (_), or the dollar symbol ($), which can be followed by a sequence of letters or digits (0 to 9), ‘$’, or ‘_’ .
4. A variable name should not start with a digit.
5. A variable name should not contain embedded white spaces .
6. A variable name should not consist of a keyword.
7. A variable name in Java is case sensitive.
In order to manipulate variables we can use Assignment Operator, we use the assignment operator (=) to assign a value to a variable. Java supports the fallowing operators,
Assignment is not the same as equality!!!
= is not the same as ==
Assignments are expressions: for this have a look at this snippet
int x, y;
x = y = 5; // Same as x = (y= 5); assoc from R to L
Short cut forms exist:
int x = 5, y = 3;
x += y; // Same as x= x + y;
Forms include +=, -=, *=, /=, &=, ^=, =, %=
1. Arithmetic Operators: These operators are used to perform arithmetic operations like addition, subtraction, multiplication and division etc.For more details have a look at the fallowing diagram.
Declaring Variables
In the fallowing example, we are going to demonstrate the “Pre and Post Increment” operators with suitable examples.
public class DataType3
{
public static void main(String args[])
{
int a,b=20;
a=++b;
System.out.println("The Value of A : "+a);
System.out.println("The Value of B : "+b);
/*in above statements initially b=20
so here we are testing pre-increment operator which means the program
will evaluate in the fallowing stpes
1. b=20 (value 20 is assigned in variable b)
2. later b bcomes b+1 whic means b=b+1 because we are using pre-increment operator, so b will be 21 now
3 after that statement b value will be assigned in a, implies a=b, which means 21( b value) will be assigned in a
4. ultimately a=21 and b=21 and it will be the output
*/
int x,y=20;
x=y++;
System.out.println("The Value of X : "+x);
System.out.println("The Value of Y : "+y);
/* in the above case intially y=20
so here we are going to use post-increment operator
1. initially 20 will be assigned in y
2. since we are using post-increment y value will be assigend in x, => x=y => x will be 20
3. now post increment will takes place, means y becomes y+1, => y=y+1 will happen, so y becomes 21
4. then x will be 20 and y will be 21 (x=20, y=21) will be the output
*/
}
}
Have a look at the fallowing example also,
public class DataType2
{
public static void main(String\[] args)
{
int j, k, m;
int d= 122;
j= d++; // j is 122 and d is 123
System.out.println("j= " + j);
k= ++d; // k is 124 and d is 124
System.out.println("k= " + k);
m= --d; // m is 123 and d is 123
System.out.println("m= " + m);
m= k % j;
// Remainder op for int types
// k=124, j=122, so m= 2
System.out.println("m= " + m);
j= 5; k= 3; m= j/k; // Integer division: m= 1
System.out.println("m= " + m);
}
}
2. Logical Operators: By the help of logical operators we can produce the type of result either True or False (which is of type boolean) there are eight type of operators available in this category.
For example have a look at this fallowing code snippet
double c= 0.0, b= 3.0;
if (c != 0.0 && b/c > 5.0) System.out.println(“Boo”);
// Never use == with float or double (this is bad example)
// Short circuit evaluation: quit after false
// sub expression
There are also “bitwise” operators; we won’t use these much (if at all), I am going to discuss bitwise operators in another session.
Labels:
Data Types