Primitive types of Java
There are eight primitive types of data that are defined by java:
- Byte
- Short
- Int
- Long
- Char
- Float
- Double
- Boolean
The above eight types can be put into four different groups
- Integers
- Floating point numbers
- Characters
- Boolean
1. Integer:
- It includes int, byte, short, long
- They are Whole-valued signed numbers
2. Floating Point numbers:
- This group includes float and number
- They are numbers with fractional precision
3. Characters:
- It includes char
- Characters are represented in a character set
4. Boolean:
- This group includes Boolean
- Special values are represented by this like on/off, true/false etc.
Integer
Byte, short, int, and long
All of the above integers are signed i.e. positive and negative values
No unsigned integer is supported by java (positive-only)Width of an integer
Width of an integer refers to the behavior it defines for variables and expressions of that type.
Width should not be considered as the amount of storage it consumes.
Range and width of an integer is shown in table below:
Name of an integer Width Rangeint
32 bits
-2,147,483,648 to 2,147,483,647
Long
64 bits
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Short
16 bits
-32,768 to 32,767
Byte
8 bits
-128 to 127
Floating Point
They are also called real numbers.
They are used for evaluating expressions that require fractional precision.
Floating point is of two kinds/types:
- Float (represents single precision numbers)
- Double (represents double precision numbers)
Range and width of a floating type is shown in table below:
Name Width RangeDouble
64 bits
1.7e-308 to 1.7e+308
Float
32 bits
3.4e-038 to 3.4e+038
Character
Characters are stored by a data type called char.
Characters are represented as Unicode in java
- Unicode
A fully international character set, that can represent all of the characters that are found in all human languages
Range and width of a character is shown in table below:
Name Width RangeChar
16 bits
0 to 65,536
Boolean
To represent logical values we use Boolean type variables.
So this type of variable may either true or false
It can store only one value either true or false at a time
Scope and life time of a variables
All of the variables have certain scope and life-time.
- Scope
Scope defines the visibility.
Scope determines which part of object is visible to other parts
- Lifetime
The lifetime of a variable is confined to its scope
Objects declared in the outer scope will be visible to code within the inner scope.
But, the reverse is not true.
Objects declared within the inner scope will not be visible outside it.
Operators
The java operators can be divided into four main groups
- Arithmetic
- Bitwise
- Logical
- Relational
1. Arithmetic operators
Example:Arithmetic operators are used for all types of arithmetic activities like addition, subtraction, multiplication, division, modulus, increment, and decrement etc...
The operands of arithmetic operator must be numeric.
2. Bitwise Operator
Bitwise operators can be applied to the integer types like long, int, short, char, and byte.
These operators act upon the individual bits of their operands.
Example
The byte value for 42 in binary is 00101010.
To represent negative numbers, Java uses two’s complement encoding
- Inverting all of the bits in a value, then adding one to the result.
- Example: -42 are represented by inverting all of the bits in 00101010, which yields11010101, then adding 1, which results in 11010110, or –42.
3. Relational Operators
They determine the relationship of one operand with other operand.
Specifically, they determine the equality and ordering.
The end result of these operations is a Boolean value.
Operator
Result
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
4. Logical Operators
The logical operators are those which operate on logical values.
All of the binary logical operators combine two boolean values to form a resultant Boolean value.
Operator
Result
&
Logical AND
|
Logical OR
^
Logical XOR (exclusive OR)
||
Short-circuit OR
&&
Short-circuit AND
!
Logical unary NOT
&=
AND assignment
|=
OR assignment
^=
XOR assignment
==
Equal to
Comments
Add new comment
- Add new comment
- 49 views