☰ See All Chapters |
Data types in java with example
Data type is used to specify the type of data that variable can hold. A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.
Java has 3 types of data types
Primitive Types: byte, short, int, long, float, double, char and boolean
Derived Types: Arrays
User Defined Types: Class, subclass, abstract class, interface, enumerations and annotations
Integers
This group includes byte, short, int, and long, which are for whole-valued signed numbers. Java does not support unsigned, positive-only integers. Many other computer languages support both signed and unsigned integers. All four byte, short, int, and long are signed. If a large value is used then we use long data type, but for such long values need append “L” at last
public class IntegerDemo { public static void main(String[] args) { long y = 12345678987654321L; // notice the “L” } } |
An interesting note about Java is that it performs integer arithmetic at the int level. Octal numbers must start with 0. Hexadecimal numbers must start with 0x or 0X.
Floating-Point Types
Float values are known as real numbers. float is single- precision number and double is double-precision number. Floating-point literals are treated as a double value by default .Hence if we assign any floating point literal to a variable, value will be assigned as double. . Thus if we want to assign floating-point literals as single- precision we need to append “ F” to the value at last. Assigning a value to float data type without appending “F” gives an error. Because Floating-point literals are treated as double value by default which is double-precision and float is single- precision. Hence single- precision variable cannot store single- precision value, possible loss of precision Error comes.
public class FloatDemo { public static void main(String [] args) { double pi = 3.14159; float f = 2.7F; System.out.println("pi = " + pi); System.out.println("f = " + f); int n = 15, d = 4; f = n/d; System.out.println("15/4 = " + f); int radius = 10; double area = pi * radius * radius; System.out.println("area = " + area); } } |
Boolean Data Type
boolean is a built-in data type in java used to represent Boolean values. A variable of type boolean can be either true or false. Note that true and false are special literals in Java.
public class BooleanDemo { public static void main(String [] args) { boolean t = true; System.out.println("t is " + t); int x = 10; boolean y = (x > 15); System.out.println("y is " + y); // y = x; // Does not compile! } } |
Char Data Type
Single quotes are used to denote a character literal.
class CharDemo { public static void main(String args[]) { char ch1; ch1 = 'A'; System.out.println("ch1 contains " + ch1); ch1++; // increment ch1 System.out.println("ch1 is now " + ch1); } } |
Output:
ch1 contains A ch1 is now B |
In this Program if ch1=M, then ch1++ will be N, if ch1=I, then ch1++ will be J.
class CharDemo { public static void main(String args[]) { char ch1 = 65; System.out.println(ch1); } } |
This program prints A, which is the UNICODE equivalent of 65.
Size and range of data types
Type | Data Type | Size (bits) | Minimum Value | Maximum Value | Default Value |
Integers | long | 64 | -2(64-1) | 2(64-1)-1 | 0 |
Int | 32 | -2(32-1) | 2(32-1)-1 | 0 | |
short | 16 | -2(16-1) | 2(16-1)-1 | 0 | |
byte | 8 | -2(8-1) | 2(8-1) | 0 | |
Floating-Point | double | 64 | 4.9e–324 | 1.8e+308 | 0.0 |
float | 32 | 1.4e–045 | 3.4e+038 | 0.0 | |
char | char | 16 | \u0000 | uFFFF | Space |
boolean | boolean | n/a | true or false |
| false |
Array
Declaring an array | |
Use any of the below syntax: dataType[] nameOfArray; dataType [] nameOfArray; dataType []nameOfArray; dataType nameOfArray[]; dataType nameOfArray []; | Example : int[] arr; int [] arr; int []arr; int arr[]; int arr []; |
Instantiating array | |
nameOfArray = new dataType[size]; | arr = new int[10]; |
Initializing array | |
Values should match with the data type of array nameOfArray[index] = value; | int[0] = 50; int[1] = 100; The below assignment is not possible due to data type mismatch int[0] = true; int[1] = ‘A’; |
Declaring and instantiating array | |
dataType[] nameOfArray = new dataType[size]; dataType [] nameOfArray = new dataType[size]; dataType []nameOfArray = new dataType[size]; dataType nameOfArray[] = new dataType[size]; dataType nameOfArray [] = new dataType[size]; | int[] arr1 = new int[10]; int [] arr2 = new int[10]; int []arr3 = new int[10]; int arr4[] = new int[10]; int arr5 [] = new int[10];
|
Declaring, instantiating and initializing array | |
dataType[] nameOfArray = {value1, value2…}; dataType [] nameOfArray = {value1, value2…}; dataType []nameOfArray = {value1, value2…}; dataType nameOfArray[] = {value1, value2…}; dataType nameOfArray [] = {value1, value2…}; | int[] arr1 = {1, 2, 3}; int [] arr2 = {1, 1, 1}; int []arr3 = {4, 5, 3}; int arr4[] = {4, 4, 6}; int arr5 [] = {1, 8, 3};
|
All Chapters