Objective:
Learn about:
- The use of variables
- Working with different data types
and their implications
- How to create different types
of variables and to print their
output to screen
- Garbage Collection
- Concept:
Primitive
Data Types
Programs operate on data. Therefore
we must be able to refer to various
kinds of data. Each data value has
a specific data type that determines
the kinds of operations that we can
perform on it. Several common data
types are built into the java language
and are called primitive data types
in java: four variations of integers,
two variations of floating point numbers,
a Boolean data type, and a character.
Integers
and Floating Points
Java has two basic kinds of numeric
values: integers (which have no fractional
part) and floating points (which do).
There are four integer data types
(byte, short, int, and long)
and two floating-point data types
(float and double). All of the numeric
types differ by the amount of memory
space used to store a value of that
type, which determine the range of
the values that can be represented.
The size of ech dta type is the same
for all hardware platforms. All numeric
types are signed, meaning that both
negative and positive values can be
stored in them.
Type
|
Description
|
Min
Value
|
Max
Value
|
byte
|
8-bit
signed integer.
|
-128
|
127
|
short
|
16-bit
signed integer.
|
-32768
|
32767
|
int
|
32-bit
signed integer.
|
-2,147,483,648
|
2,147,483,647
|
long
|
64-bit
signed integer.
|
-9223,372,036,854,775,808
|
-9223,372,036,854,775,807
|
float
|
32-bit
|
Approximately
-3.4E+38 with 7 significant
digits
|
Approximately
3.4E+38 with 7 significant
digits
|
double
|
64-bit
|
Approximately
-1.7E+308 with 15 significant
digits
|
Approximately
1.7E+308 with 15 significant
digits
|
char
|
16-bit
Unicode character.
|
|
|
Boolean
|
A
true or false value, using
the keywords true
and false --
pretty clever. There is no
conversion between booleans
and other types, such as int 's.
|
Characters
Characters are another fundamental
type of data used and managed on a
computer. A character set is alist
of characters in a particular order.
A character literal is expressed in
a java program with single quotes,
such as 'b', 'c'. Strings are the
combination of characters. You can
store a complete sentence in a string.
String literals are used with the
double quotes. The string type is
not a primitive data type in java.
Boolean
The Boolean data types has only two
values: true and false.
A Boolean variable is usually used
to indicate if a particular condition
is true or not. But it can also be
used to represent any situation that
has two states, such as a light bulb
being or or off. Because a Boolean
value represent one of two situations,
only one byte is needed to store it.
A Boolean value cannot be converted
to any other data type and any other
data type cannot be converted to a
Boolean value. The words true
and false are reserved
in java as Boolean literals and cannot
be used outside of this context.
Identifiers
A programmer must make up words
writing a program, such as a class
name or a method name. Theses words
are called identifiers. Most identifiers
have no predefined meaning in language.
Example:
public
static void main (String args[]){
int a = 29;
System.out.println("Hello "
+ a)
}
Here
in this example, main, a , string,
args and System.out.println are the
identifiers.
Keywords
Keywords are identifiers that
have a special meaning in a programming
language and can only be used in predefined
ways. These are predefined so we can
use them only according to the predefined
rules. In above example key words
are public, static, void.
Reserved
Words
Reserved words have same behavior
as keywords. These are one and the
same things. But only in some java
certification books keywords "false",
"true", and "null"
are marked as reserve words.
Literals
Literals are the values stored
in a variable against any data type
or we can say that the literals are
the explicit data type that are used
in a program. in above example 29
is a literal of int type.
Operators
Operators are the instructions:
with the help of those we can do some
operations on the data. For example
in "a+b", a and b refers
to the data and "+" sign
is a operator that indicates the multiplication
operation on the data. Operators are
of different types such as: assignment
operators, arithmetic operators, increment
and decrement operators, and logical
operators.
Operands
Operands are the values on which
an operators performs a specific action.
for example in "a+b" "a"
and "b" that are referencing
to the data are called the operands.
The operands used in the operations
might be literals, constants, variables,
or other sources of data.
Expressions
Programming statements often
involve expressions. These are combinations
of operators and operands used to
perform a calculation. In above examples
"a+b" is called a whole
expression.
Constants
A constant is some value that
does not change e.g 1, "Sam",
'n' etc
Naming
Convention
An identifier can be composed
of any combination of letters, digits,
the underscore character ( _
) the dollar sign ($), but it cannot
begin with a digit. Identifiers may
be of any length. Therefore, total,
leable7, next_stock_item, and $amount
are all valid identifiers, but 4th _word
and coin# value are not valid.
Both
uppercase and lowercase letters can
be used in an identifier, and the
difference is important. Java is case
sensitive, which means hat two identifier
names that only differ by the case
of their letter are considered to
be different identifiers. Therefore
total, Total and ToTal are different
identifiers.
Assignment
Statements
Assignment statements are used
to assign values to the variables.
For example in expression "a=12"
"a" is variable identifier
and "=" is the assignment
operator and "12" is the
literal that is assigned to the variable
a. In java or any other familiar programming
languages, it is important to note
that "=" sign does not means
the equality; it is used to assign
the values on the right side of this
operator to the identifiers on the
left side. So te above expressions
does not mean "a is equal to
12", but it means place the value
of 12 in the variable a.
Variables
A variable is a location in the
computer's memory where a value can
be stored for use by a program. All
variables must be declared with some
name and data type before they can
be used in a program.
Declaration
String
first, second;
int No1,
No2;
This declaration
specifies that variable first, second
are of data type String.
Instance
Variables
Java programmers usually create
their own user defined data types
called classes. Each class
contains data and set of functions
that manipulate the data. The data
components of a class are called "Instance
variables". An instance of
built in data type Int or String
is called a variable.
Just
as this an instance of a user defined
data type is called Object. The difference
between an object and a variable is
that Object contains both data and
methods in it while a variable contain
only data in it.
Static
Variables
Each object of a class has its
own copy of all the instance variables
of the class. In certain cases, only
a particular copy of a variable is
shared by all the objects of the class.
This particular variable is static
in nature and called static
variable. A static class variable
represent class wide information all
objects of the class share same piece
of data among themselves. The declaration
of static variable begins with keyword
static.
Declaration
public
static int count;
private static String str;
A
class public static class variable
can be accessed through a class name
using dot operator to an object of
that class. A class's private static
class variable can be accessed only
through the methods of that class.
Actually static class members exist
even when no object of that class
exists.
Always
remember that "A method declared
static cannot access non static class
members. Unlike simple methods a static
method has no this reference
because static class variables and
methods exist independent of any objects
of a class and before any object of
the class instantiated.
Final
Variables
Java uses final keyword to declare
constant variables. The constant variables
must be initialized before they can
be used in your program. They cannot
be modified thereafter. Any attempt
to make a change in the value of the
final variables is an Error. Constant
variables are also called Read only
variables. They are often used to
make a program more readable.
Declaration
final
int count = 10;
Usage
of variables
In this section we will discuss the
usage of all above three types of
variables.
Examples:
Class
variable {
String
first, second;
int no1, no2;
static int st;
final int count = 10;
public
static void main (String args[])
{
Variables
v = new variables ();
v.first = "Pakistan";
v.second = "Zindabad";
System.out.println(v.first + "\n"
+v.second);
//v.count
= 11; //Error can't modify static
variable
System.out.println("final
values: " + v.count);
v.no1
= 1999;
v.no2 = 2000;
System.out.println(v.no1 + "\n"
+ v.no2);
v.st = 36;
System.out.println("static
values: " +v.st);
}
}
Java
Keywords
Each of the word that we use in
java program like if else, while,
do while, final, static, etc are all
java keywords. These words are reserved
by the language to implement various
feature. Keywords cannot be used by
as identifiers (as variables names).
List of java variables is shown as:
Abstract
|
Boolean
|
break
|
byte
|
case
|
char
|
catch
|
class
|
continue
|
Do
|
Default
|
Double
|
else
|
Extends
|
For
|
False
|
Final
|
finally
|
float
|
If
|
Implements
|
Import
|
instance
of
|
interface
|
Int
|
Long
|
native
|
new
|
package
|
Null
|
Private
|
protected
|
public
|
return
|
Short
|
Static
|
super
|
synchronized
|
switch
|
This
|
Throw
|
throws
|
transient
|
true
|
try
|
Void
|
volatile
|
while
|
|
Garbage
Collection
We have often seen in the Java
language that new objects have been
created for different classes and
each object hold all the attributes
and behaviors of its class, so it
acquire various system resources such
as memory (specially when new
operator is used). Now we need a disciplined
way to give resources back to the
system when they are no longer in
use. Java provides us with the facility
of automatically reallocating the
resources. The technique is called
Garbage collection.
It
works like this: when no references
to an object exist, that object assumed
to be no longer needed then memory
occupied by the object can be automatically
reclaimed.
Declaration
System.gc();
//explicit call to the garbage collector
this
call uses public static void
method gc() from class system
in the java.lang package
finalize()
method
Sometimes there could be a situation
that an object need to perform some
action when it is destroyed. For this
purpose we use finalize() method.
It is originally defined in the class
object. A class's finalize() method
always have name finalize receives
no parameter and return no value.
Method finalize() is normally called
protected in a class.
Declaration
protected
void finalize() {
System.out.println("All the garbage
has been collected");
}
|