Java Data and Variables
There are 8 primitive
data types. he 8 primitive data types are numeric types. The names of the eight
primitive data types are:
|
byte
|
short
|
int
|
long
|
float
|
double
|
char
|
boolean
|
There are both integer and floating
point primitive types. Integer types have no fractional part; floating
point types have a fractional part. On paper, integers have no decimal point,
and floating point types do. But in main memory, there are no decimal points:
even floating point values are represented with bit patterns. There is a
fundamental difference between the method used to represent integers and the
method used to represent floating point numbers.
|
Integer Primitive Data
Types
|
||
|
Type
|
Size
|
Range
|
|
byte
|
8 bits
|
-128 to +127
|
|
short
|
16 bits
|
-32,768 to +32,767
|
|
int
|
32 bits
|
(about)-2 billion to
+2 billion
|
|
long
|
64 bits
|
(about)-10E18 to
+10E18
|
|
Floating Point
Primitive Data Types
|
||
|
Type
|
Size
|
Range
|
|
float
|
32 bits
|
-3.4E+38 to +3.4E+38
|
|
double
|
64 bits
|
-1.7E+308 to
1.7E+308
|
Examples
int yr = 2006;
double rats = 8912 ;
double rats = 8912 ;
For each primitive
type, there is a corresponding wrapper class. A wrapper class can be used
to convert a primitive data value into an object, and some type of objects into
primitive data. The table shows primitive types and their wrapper classes:
|
primitive type
|
Wrapper type
|
|
byte
|
Byte
|
|
short
|
Short
|
|
int
|
Int
|
|
long
|
Long
|
|
float
|
Float
|
|
double
|
Double
|
|
char
|
Character
|
|
boolean
|
Boolean
|
Variables only exist within the structure in which they
are defined. For example, if a variable is created within a method, it cannot
be accessed outside the method. In addition, a different method can create a
variable of the same name which will not conflict with the other variable. A
java variable can be thought of as a little box made up of one or more bytes
that can hold a value of a particular data type:
Syntax: variabletype variablename = data;
Source Code ( demonstrating declaration of a variable )
class example
{
public static
void main ( String[] args )
{
long x =
123; //a declaration of a variable
named x with a datatype of long
System.out.println("The variable x has: " + x );
}
}
Source Code
public class MaxDemo {
public static
void main(String args[]) {
//integers
byte
largestByte = Byte.MAX_VALUE;
short
largestShort = Short.MAX_VALUE;
int
largestInteger = Integer.MAX_VALUE;
long
largestLong = Long.MAX_VALUE;
//real
numbers
float
largestFloat = Float.MAX_VALUE;
double
largestDouble = Double.MAX_VALUE;
//other
primitive types
char aChar
= 'S';
boolean
aBoolean = true;
//Display
them all.
System.out.println("largest byte value is " + largestByte +
".");
System.out.println("largest short value
is " + largestShort + ".");
System.out.println("largest integer value is " +
largestInteger + ".");
System.out.println("largest long value is " + largestLong +
".");
System.out.println("largest float value is " + largestFloat +
".");
System.out.println("largest double value is " + largestDouble
+ ".");
}
}
Sample Run
The largest byte value is 127.
The largest short value is 32767.
The largest integer value is 2147483647.
The largest long value is 9223372036854775807.
The largest float value is 3.4028235E38.
The largest double value is 1.7976931348623157E308.
No comments:
Post a Comment