•A category of data characterized by the supported elements of the category and the supported operations on those elements
Types of Data Types
Classification Of Data Types
Diagrammatic Representation
There are 9 data types in Java, 8 primitive types and a reference type.
boolean
The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
char
To support characters that are not traditionally used in (US) English, another character format was created: Unicode. This includes Latin-based and non-Latin-based languages.
To support Unicode characters, Java provides the char data type. Based on this, to declare a variable that would be used to store characters, use the char keyword. To initialize the variable, assign it a single-quoted character.
byte
A byte is series of 8 bits. It is used to represent (very) small
numbers. To declare a variable that would hold a natural number
between -128 and 127, you can use the byte data type.
The Java class associated with the byte data type is called Byte
short
An integer is referred to as short if its value is between -32768 and 32767. This number can fit in 16 bits.
The Java class associated with the short data type is called Short
int
The word integer is also used for a natural number. An integer is simply a natural number. Many, if not most, languages provide different data types for various categories of integers. In Java, an integer is variable whose values are between -2,147,483,648 and 2,147,484,647. The value is also said to fit in a 32-bit range.
The Java class associated with the int data type is called Integer
long
A number that is higher than a regular integer can hold is called a long integer. To support this type of number, the Java language provides the long data type. A long integer is a variable that can hold very large number that may require 64 bits to be stored accurately.
Minimum -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
When you declare a long variable, by default, the compiler "allocates" only enough memory for an integer so there would not be a waste. In some cases, you may want the compiler to use 64 bits for your long variable.
public class Main {
public static void main(String[] args) {
// TODO code application logic here
long days = 245885475475;
System.out.print("Days: ");
System.out.print(days);
}
}
This would produce:
init:
deps-jar:
Compiling 1 source file to C:\Programs\JavaLessons\Exercise1\build\classes
C:\Programs\JavaLessons\Exercise1\src\exercise1\Main.java:24: integer number too large: 245885475475
long days = 245885475475;
1 error
BUILD FAILED (total time: 0 seconds)
The program would not work because the compiler would have reserved space for an integer but the assigned value needs more room. If you insist on using enough memory for a long integer, when initializing it, on the right side of the value, type L. Here is an example:
package exercise1;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
long days = 245885475475L;
System.out.print("Days: ");
System.out.print(days);
}
}
This time, the program works fine because the compiler was explicitly asked to reserve enough memory.
The Java class associated with the long data type is called Long.
Double-Precision Values
Double
The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
The Java class associated with the double data type is called Double
Single-Precision Values
float
The float data type is a single-precision 32-bit IEEE 754 floating point.
Although a double variable uses memory more than the average variable, because memory is not expensive anymore, most programmers usually use the double data type instead of the float when declaring their variables. Use float only if you must.
The Java class associated with the float data type is called Float.
Type Conversion and Casting:
TypeCasting is defined as conversion of one data type into another datatype. As we know, that they are two types of data types in Java. The first is known as Fundamental or Primitive Data Type and the second is known as Reference or Advanced Data Type. TypeCasting is only possible between fundamental data types or between Advanced Datatypes. This is to made sure, that TypeCasting is not possible between Fundamental and Advanced Datatype and viceversa.
If programmer or developer wants to perform TypeCasting between fundamental datatypes, then it is possible with the help of an operator known as type-cast operator. And, if the developer wants to perform, typecasting between Advanced Datatype then it is possible with the help of a concept known as Wrapper Classes.(that is not in your syllabus)TypeCasting in Fundamental or Primitive Datatypes:
If programmer wants to convert one primitive datatype into another primitive datatype, then it can be done with the help of two ways:
Widening or Implicit TypeCasting
Narrowing or Explicit TypeCasting The primitive datatypes are categorized into two categories as higher types and the second is lower types. The higher types includes datatypes that contains a large number and their size is also large. The lower type include datatypes that contains a small number and their size is also small. The size of the datatype means amount of memory allocated to the variable belonging to that datatype. For example, in case of character datatype, the memory allocated will be 16-bits where as in case of long datatype, the memory allocated will be 64-bits. In typecasting, we can convert a higher type to lower type and viceversa. But we cannot convert boolean datatype into either higher type or lower type.byteàshortàcharàintàlongàfloatàdoubleLower type includes byte,short,character.Higher type includes int,long,float,double. Widening:
It is defined as a type casting that converts a lower type to a higher type. In other words, it is defined as a type casting that converts a lower datatype into higher datatype. In case of Widening, there is no loss of data i.e. data is not lost during the casting operation. If programmer wants to perform casting, then it is the programmer’s responsibility to use an operator known as cast operator. The cast operator includes opening of the circular brace and closing of circular brace and in the middle it contains data type that user is interested to cast. But in case of widening, if the programmer is not using any cast operator, then there is no compile time error. In other words, Java Compiler takes it as its responsibility to perform typecasting, without expecting it from the programmer. That is why, it is also known as Implicit typecasting.
public class WideningProg{
public static void main(String args[]){
int num=102;
double d;
d=num;
System.out.println (d);
}
} // The output is 102.0
When the above code is executed, the output is 102.0
The above code does not give any compile time error, though we did not provide, any cast operator. The reason is Java Compiler will do it internally. The above program can also be written as shown below:
public class WideningProg1 {
public static void main (String args[]){
int num=102;
double d;
d= (double) num; /*Here (double) is cast operator that converts any value into double*/
System.out.println (d);
}
} // The output is 102.0 Narrowing:
It is defined as a type casting that converts a higher type to lower type. In other words, it is defined as a type casting that converts a higher data type into lower data type. In case of Narrowing, there is loss of data i.e. there always exists, data getting lost during the casting operation. If programmer wants to perform casting, then it is the programmer’s responsibility to use an operator known as cast operator. The cast operator includes opening of the circular brace and closing of circular brace and in the middle it contains data type that user is interested to cast. In case of narrowing, if the programmer is not using any cast operator, then there is a compile time error. In other words, Java Compiler does not take any responsibility to perform typecasting. In other words, the typecasting responsibility must be taken by the programmer itself. That is why, it is also known as Explicit typecasting.Let us consider an example, to understand the concept of widening. Let us consider a lower data type as integer and the higher data type as float and let us perform typecasting as shown below:
Let us consider an example, to understand the concept of narrowing. Let us consider a higher data type as double and the lower data type as integer and let us perform typecasting as shown below:
Type Conversion and Casting:
TypeCasting is defined as conversion of one data type into another datatype. As we know, that they are two types of data types in Java. The first is known as Fundamental or Primitive Data Type and the second is known as Reference or Advanced Data Type. TypeCasting is only possible between fundamental data types or between Advanced Datatypes. This is to made sure, that TypeCasting is not possible between Fundamental and Advanced Datatype and viceversa.
If programmer or developer wants to perform TypeCasting between fundamental datatypes, then it is possible with the help of an operator known as type-cast operator. And, if the developer wants to perform, typecasting between Advanced Datatype then it is possible with the help of a concept known as Wrapper Classes.(that is not in your syllabus)TypeCasting in Fundamental or Primitive Datatypes:
If programmer wants to convert one primitive datatype into another primitive datatype, then it can be done with the help of two ways:
Narrowing or Explicit TypeCasting
It is defined as a type casting that converts a lower type to a higher type. In other words, it is defined as a type casting that converts a lower datatype into higher datatype. In case of Widening, there is no loss of data i.e. data is not lost during the casting operation. If programmer wants to perform casting, then it is the programmer’s responsibility to use an operator known as cast operator. The cast operator includes opening of the circular brace and closing of circular brace and in the middle it contains data type that user is interested to cast. But in case of widening, if the programmer is not using any cast operator, then there is no compile time error. In other words, Java Compiler takes it as its responsibility to perform typecasting, without expecting it from the programmer. That is why, it is also known as Implicit typecasting.
public class WideningProg{
public static void main(String args[]){
int num=102;
double d;
d=num;
System.out.println (d);
}
} // The output is 102.0
When the above code is executed, the output is 102.0
The above code does not give any compile time error, though we did not provide, any cast operator. The reason is Java Compiler will do it internally. The above program can also be written as shown below:
public class WideningProg1 {
public static void main (String args[]){
int num=102;
double d;
d= (double) num; /*Here (double) is cast operator that converts any value into double*/
System.out.println (d);
}
} // The output is 102.0 Narrowing:
It is defined as a type casting that converts a higher type to lower type. In other words, it is defined as a type casting that converts a higher data type into lower data type. In case of Narrowing, there is loss of data i.e. there always exists, data getting lost during the casting operation. If programmer wants to perform casting, then it is the programmer’s responsibility to use an operator known as cast operator. The cast operator includes opening of the circular brace and closing of circular brace and in the middle it contains data type that user is interested to cast. In case of narrowing, if the programmer is not using any cast operator, then there is a compile time error. In other words, Java Compiler does not take any responsibility to perform typecasting. In other words, the typecasting responsibility must be taken by the programmer itself. That is why, it is also known as Explicit typecasting.Let us consider an example, to understand the concept of widening. Let us consider a lower data type as integer and the higher data type as float and let us perform typecasting as shown below:
Let us consider an example, to understand the concept of narrowing. Let us consider a higher data type as double and the lower data type as integer and let us perform typecasting as shown below:
No comments:
Post a Comment