Data Types, Variables and Constants
DATA TYPE:
·
A
data type is a scheme for representing values. An example is int which
is the Integer, a data type.
·
Values
are not just numbers, but any manner of data that a computer can process.
·
The
data type defines the kind of data that is represented by a variable.
·
As
with the keyword class, Java data types are case sensitive.
There are two types of data types
·
primitive
data type
·
non-primitive
data type
In primitive data types, there are
two categories
1.
numeric
means Integer, Floating points
2.
Non-numeric
means Character and Boolean
In non-primitive types, there are
three categories
1.
classes
2.
arrays
3.
interface
Following table shows the data types
with their size and ranges
Data type
|
Size (byte)
|
Range
|
byte
|
1
|
-128 to 127
|
boolean
|
1
|
True or false
|
char
|
2
|
A-Z,a-z,0-9,etc.
|
short
|
2
|
-32768 to 32767
|
Int
|
4
|
(about) -2 million to 2 million
|
long
|
8
|
(about) -10E18 to 10E18
|
float
|
4
|
-3.4E38 to 3.4E18
|
double
|
8
|
-1.7E308 to 1.7E308
|
INTEGER DATA TYPE:
Integer data type can hold the
numbers (the number can be positive number or negative number). In Java, there
are four types of integer as follows:
·
byte
·
short
·
int
·
long
We can make integer long by adding ‘l’ or ‘L’ at the end of
the number.
Floating point data type:
It is also called as Real number and
when we require accuracy then we can use it.
There are two types of floating point
data type.
·
float
·
double
It is represent single and double
precision numbers. The float type is used for single precision and it uses 4
bytes for storage space. It is very useful when we require accuracy with small
degree of precision. But in double type, it is used for double precision and
uses 8 bytes of storage space. It is useful for large degree of precision.
Character data type:
It is used to store single character
in memory. It uses 2 bytes storage space.
Boolean data type:
It is used when we want to test a
particular condition during the execution of the program. There are only two values that a boolean type
can hold: true and false. Boolean type
is denoted by the keyword boolean and uses only one bit of storage.
Following program shows the use of
datatypes.
Program:
import java.io.DataInputStream;
class cc2
{
public static void main(String
args[]) throws Exception
{
DataInputStream
s1=new DataInputStream(System.in);
byte rollno;
int
marks1,marks2,marks3;
float avg;
System.out.println("Enter
roll number:");
rollno=Byte.parseByte(s1.readLine());
System.out.println("Enter
marks m1, m2,m3:");
marks1=Integer.parseInt(s1.readLine());
marks2=Integer.parseInt(s1.readLine());
marks3=Integer.parseInt(s1.readLine());
avg =
(marks1+marks2+marks3)/3;
System.out.println("Roll
number is="+rollno);
System.out.println("Average
is="+avg);
}
}
Output:
C:\cc>java cc2
Enter roll number:
07
Enter marks m1, m2,m3:
66
77
88
Roll number is=7
Average is=77.0
Mixing Data types:
Java allows mixing of constants and
variables of different types in an expression, but during assessment it hold to
very strict rules of type conversion. When
computer consider operand and operator and if operands are different types then
type is automatically convert in higher type.
Following table shows the automatic
type conversion.
|
char
|
byte
|
short
|
int
|
long
|
float
|
double
|
Char
|
int
|
int
|
int
|
int
|
long
|
float
|
double
|
Byte
|
int
|
int
|
int
|
int
|
long
|
float
|
double
|
Short
|
int
|
int
|
int
|
int
|
long
|
float
|
double
|
Int
|
int
|
int
|
int
|
int
|
long
|
float
|
double
|
Long
|
long
|
long
|
long
|
long
|
long
|
float
|
double
|
Float
|
float
|
float
|
float
|
float
|
float
|
float
|
double
|
double
|
double
|
double
|
double
|
double
|
double
|
double
|
double
|
Variables:
Variables are labels that express a
particular position in memory and connect it with a data type. The first way to
declare a variable: This specifies its data type, and reserves memory for it.
It assigns zero to primitive types and null to objects.
Data-type variable-Name;
Example:
int a;
The second way to declare a variable:
This specifies its data type, reserves memory for it, and puts an initial value
into that memory. The initial value must be of the correct data type.
Data-type variable-Name = initial-Value;
Example:
int a=0;
The first way to declare two
variables: all of the same data type, reserves memory for each.
Data-type variable-Name1, variable-Name2;
Example:
int a,b;
The second way to declare two
variables: both of the same data type, reserves memory, and puts an initial
value in each variable.
Data-type variable-Name1= initial-Value
I, variable-Name2= initial-Value II;
Example:
int a=0,b=1;
Variable name:
·
Use
only the characters ‘a’ through ‘z’, ‘A’ through ‘Z’, ‘0’ through ‘9’, character ‘_’, and
character ‘$’.
·
A
name cannot include the space character.
·
Do
not begin with a digit.
·
A
name can be of any realistic length.
·
Upper
and lower case count as different characters.
·
A
name cannot be a reserved word (keyword).
· A name must not previously be in
utilized in this block of the program.
Constant:
Constant means fixed value which is
not change at the time of execution of program. In Java, there are two types of
constant as follows:
Numeric Constants
§ Integer
constant
§ Real
constant
Character Constants
§ Character
constant
§ String
constant
Integer Constant:
An Integer constant refers to a
series of digits. There are three types of integer as follows:
a) Decimal integer
Embedded
spaces, commas and characters are not allowed in between digits.
For example:
23 411
7,00,000
17.33
b) Octal integer
It allows us
any sequence of numbers or digits from 0 to 7 with leading 0 and it is called
as Octal integer.
For example:
011
00
0425
c) Hexadecimal integer
It allows the
sequence which is preceded by 0X or 0x and it also allows alphabets from ‘A’ to
‘F’ or ‘a’ to ‘f’ (‘A’ to ‘F’ stands for the numbers ‘10’ to ‘15’) it is called
as Hexadecimal integer.
For example:
0x7
00X
0A2B
Real Constant
It allows us fractional data and it
is also called as floating point constant. It is used for percentage, height
and so on.
For example:
0.0234
0.777
-1.23
Character Constant
It allows us single character within
pair of single quote.
For example:
‘A’
‘7’
‘\’
String Constant
It allows us the series of characters
within pair of double quote.
For example:
“WELCOME”
“END OF
PROGRAM”
“BYE …BYE”
“A”
Symbolic constant:
In Java program, there are many
things which is requires repeatedly and if we want to make changes then we have
to make these changes in whole program where this variable is used. For this
purpose, Java provides “final” keyword to declare the value of variable as
follows:
Syntax:
final type Symbolic_name=value;
For example:
If I want to declare the value of “PI”
then:
final float PI=3.1459
the condition is, Symbolic name will
be in capital letter( it shows the difference between normal variable and symbolic
name) and do not declare in method.
Backslash character constant:
Java support some special character
constant which are given in following table.
Constant
|
Importance
|
‘\b’
|
Back space
|
‘\t’
|
Tab
|
‘\n’
|
New line
|
‘\\’
|
Backslash
|
‘\‖’
|
Single quote
|
‘\‖’
|
Double quote
|
Comments:
A comment is a note written to
a human reader of a program. The program compiles and runs exactly the same
with or without comments. Comments start with the two characters ―//‖ (slash
slash). Those characters and everything that follows them on the same line are
ignored by the java compiler.
Everything between the two characters
“/*” and the two characters “*/” are unobserved by the compiler. There can be
many lines of comments between the “/*”and the “*/”.
Command line arguments:
Command line arguments are parameters
that are supplied to the application program at the time of invoking its
execution. They must be supplied at the time of its execution following the
file name.
In the main () method, the args is
confirmed as an array of string known as string objects. Any argument provided
in the command line at the time of program execution, are accepted to the array
args as its elements. Using index or subscripted entry can access the
individual elements of an array. The number of element in the array args can be
getting with the length parameter.
For example:
class Add
{
public static void main(String
args[])
{
int
a=Integer.parseInt(args[0]);
int
b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println(“Addition
is=”+c);
}
}
OUTPUT:
c:\javac Add.java
c:\java Add 5 2
7
No comments:
Post a Comment
Note: only a member of this blog may post a comment.