Java
In this tutorial we will learn about data types in Java programming language.
Constants are the fixed values that never change during execution of a program.
Following are type of constants in Java.
These are the numbers without any decimal part. And they can be in decimal (bas 10), octal (base 8) or hexadecimal (base 16) number system.
Decimal integer constants uses ten digits from 0 to 9.
Example: -99, 0, 100 are decimal integer constants.
Octal integer constants starts with 0 and uses eight digits from 0 to 7.
Example: 01, 010, 011 are octal integer constants.
Hexadecimal integer constants uses ten digits from 0 to 9 and six letters from A to F. We can also use small letter from a to f.
Example: 0xA, 0xff, 0x0b are hexadecimal integer constants.
These are the numbers with decimal part.
Example: -10.12, 0.5, 100.234 are all real constants.
We can represent real constants in exponential a.k.a scientific notation.
mantisa e exponent
Where, mantisa
is either a real number or integer.
And exponent
is an integer with plus and minus symbol.
In the following example we are expressing 123000 in exponential form.
123000 = 1.23e5
Similarly, we can represent -0.012 in exponential form as follows.
-0.012 = -1.2e-2
Character constants are single character enclosed in single quotes.
Example: 'a', 'b', 'c', '1', '2' etc are character constants.
String constants are sequence of characters enclosed in double quotes.
Example: "Hello World", "A", "B", "1" etc are string constants.
'a'
is a character constant as it is using single quotes.
"a"
is a string constant as it is using double quotes.
These are special backslash character constants in Java.
Some of the escape sequence is listed below.
Character Constant | Description |
---|---|
'\'' | Single quote |
'\"' | Double quotes |
'\a' | Audible alert(bell) |
'\b' | Backspace |
'\f' | Form feed |
'\r' | Carriage return |
'\t' | Horizontal tab |
'\v' | Vertical tab |
'\\' | Backslash |
'\/' | Forward slash |
A variable is a named memory location to hold some value.
The value stored in a variable can be changed any time we want.
Example of variables: isGameOver, namge, age etc.
To name variables we have to follow the given rules.
Variable names are case-sensitive so, isGameOver
and isgameover
are treated as two separate variables.
There are two categories of data types in Java.
The first one is Primitive data type. And the second one is Derivesd data type.
Following are the primitive data types available in Java.
byte
short
int
long
float
double
char
boolean
Derived types are Class
, Interface
, Array
etc. and we will discuss them in the later part of this tutorial series.
KEYWORD | SIZE (bytes) | RANGE |
---|---|---|
byte | 1 | -128 to 127 |
short | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
KEYWORD | SIZE (bytes) | RANGE |
---|---|---|
float | 4 | -3.4e38 to 3.4e38 |
double | 8 | -1.7e308 to 1.7e308 |
These take 2 bytes of memory space and we use the char
keyword to create a character data type variable.
Example:
char ch = 'y';
Boolean date type can take only two values true
and false
and we use the boolean
keyword to create a boolean type variable.
Example:
boolean isGameOver = true;
ADVERTISEMENT