Java Data type
Every
variable in java have data type. Data type specify the size and the type of
value that can be stored. Java Language is rich in its data types. The variety
of data type allow the programmer to select the type appropriate to the need of
the application.
java datatype |
Data type defines the values that a variable can
take, for example if a variable has int data type, it can only take integer
values. In java we have two categories of data type: 1) Primitive data types 2)
Non-primitive data types – Arrays and Strings are non-primitive data types, we
will discuss them later in the coming tutorials. Here we will discuss primitive
data types and literals in Java.
Java has two categories of data:
1) Primitive data types
In
Java, we have eight primitive data types: boolean, char, byte, short, int,
long, float and double. Java developers included these data types to maintain
the portability of java as the size of these primitive data types do not change
from one operating system to another.
byte, short, int and long data types are used for
storing whole numbers.
float and double are used for fractional
numbers.
char is used for storing
characters(letters).
boolean data type is used for
variables that holds either true or false.
byte:
This can hold whole number between -128 and
127. Mostly used to save memory and when you are certain that the numbers would
be in the limit specified.
Example:
Example:
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
}
}
short:
This is greater than byte in terms of size
and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
Default size of this data type: 2 byte
Short num=45678
Integer:
Used when short is not
large enough to hold the number, it has a wider range: -2,147,483,648 to
2,147,483,647
Default size: 4 byte
Default value: 0
Example:
Default size: 4 byte
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
short num;
num = 150;
System.out.println(num);
}
}
long:
Used when int is not large enough to hold the
value, it has wider range than int data type, ranging from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Example:
class JavaExample {
public static void main(String[] args) {
long num = -12332252626L;
System.out.println(num);
}
}
float:
Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
Example
class JavaExample {
public static void main(String[] args) {
float num = 19.98f;
System.out.println(num);
}
}
boolean:
holds
either true of false.
Example
class JavaExample {
public static void main(String[] args) {
boolean b = false;
System.out.println(b);
}
}
character:
Hold character
Size:2byte
Example
class JavaExample {
public static void main(String[] args) {
char ch = 'Z';
System.out.println(ch);
}
}
size: 4 bytes
o Non-Primitive Data Type or Object
Data type:
The primitive data types
include byte, int, long, short, float, double, and char. They are part of the
core of Java and you don't need anything special to use them. For example, the
following declares a long variable for a partNumber:
A data type that is primitive, such as the
long variable, actually stores the
value. If we give a value to the partNumber value, for example 4030023, that is
what Java stores.
Reference types can be a class, interface, or
array variable. Remember that a class is
a set of plans for a given object. There are thousands of tree objects, but the parent set of plans
would belong in the tree class. Variables can exist inside the tree class, such
as height or tree type. These are reference variables.
An array is
a single object that contains multiple values of the same type. We could have
declared our integer for partNumbers as an array to hold a given number of
partNumbers in a single object.
There are three type of non-primitive data
type.
Class, Interface, Array etc.
Class
Data Types
Let's
say we declare a new class called Product:
class in java |
In
order to create a new non-primitive or reference variable for this class, we
have to create a new instance of the Product class. The new keyword is used to create
an object. Look at the following example where we'll be creating a new Product
called car wax.
The Java code is as follows:
java |
So
now we have a variable of carWax: But it's really an instance of the Product
class, and not a set value like the primitive variables.
Interface Data Types
An interface is
like a dashboard or control panel for a class. It has the buttons, but the
function is elsewhere. We won't go into detail on implementing interfaces since
the focus is on the interface as a non-primitive, or reference, data type.
java datatype |
No comments:
Post a Comment