Java Data type
![]() |
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:
byte:
Example:
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
}
}
short:
Default size of this data type: 2 byte
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:
![]() | |
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 |
Interface Data Types
![]() | |
java datatype |
No comments:
Post a Comment