- Why Data Types Are Important
- Java’s Primitive Data Types
- Literals
- The Scope and Lifetime of Variables
- Operators
- Type Conversion in Assignments
- Casting Incompatible Types Expressions
Why Data Types Are Important
Java is a strongly-type language. It means that every variable defined must have a data type assigned to it.
Java’s Primitive Data Types
| Integer | Floating Point | Character | True/False |
|---|---|---|---|
| byte short int long | float double | char | boolean |
| 1, 2, 3, 42 7L 0xff 0b or 0B | 3.0 22.9F .3337f 4.022e23 | ‘a’ ‘\u0061’ ‘\n’ | true false |
Integer
Java provides four different integer types to accommodate different size numbers. All the numeric types are signed, which means that they can hold positive or negative numbers.
The integer types have the following ranges.
- byte range is -128 to +127. Number of bits = 8
- short range is -32768 to +32767. Number of bits = 16
- int range is -2147483648 to +22147483647. The common integer type is int. Number of bits = 32
- long range is -9223372036854775808 to +9223372036854775807. Number of bits = 64
Floating Point
The floating-point types hold numbers with a fractional part and conform to the IEEE 754 standard. There are two types of floating points: float and double
double is called so because it provides double the precision of float. A float uses 32 bits to store data, whereas a double uses 64 bits.
Literals
Numeric literals
- Any number of underscore characters (_) can appear between digits in a numeric field.
- This can improve the readability of your code.

Rules for literals
You can place underscores only between digits; you cannot place underscores in the following places.
- At the beginning or end of a number
- Adjacent to a decimal point in a floating-point literal
- Prior to an F or L suffix
- In positions where a string of digits is expected
Note: The Java language is case-sensitive. In Java, the variable creaditCardNumber is different from CREDITCARDNUMBER. Convention indicates that Java variables and method names use “lower camel case” – lowercase for the first letter of the first element of a variable name and uppercase for the first letter of subsequent elements.
The Scope and Lifetime of Variables
The scope of a variable refers to the areas or the sections of the program in which the variable can be accessed, and the lifetime of a variable indicates how long the variable stays alive in the memory.
There are three types of variables in Java.
- Local variables
- All variables which are not instance or class variables are known as local variables.
- Scope: Within the block it is declared.
- Lifetime: Until control leaves the block in which it is declared.
- Instance variables
- A variable which is declared inside a class but is declared outside any methods and blocks is known as instance variable.
- Scope: Throughout the class except in the static methods.
- Lifetime: Until the object of the class stays in the memory.
- Class variables
- A variable which is declared inside a class, outside all the blocks and is declared as static is known as class variable.
- Scope: Throughout the class.
- Lifetime: Until the end of the program.
Operators
Assignment Operators
| = | Simple Assignment Operator |
Arithmetic Operators
| + | Additionally |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo |
Unary Operators
| + | Unary plus operator; indicates positive |
| – | Unary minus operator; negates an expression |
| ++ | Increment operator; increments a value by 1 |
| — | Decrement operator; decrements a value by 1 |
| ! | Logical complement operator; inverts the value of a Boolean |
Because numbers have been introduced, the list above shows a list of common operators. Most are common to any programming language, and a description of each is provided in the slide.
The binary and bitwise operators have been omitted for brevity. For details about those operators, refer to the Java tutorial; https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Note: Operators have definitive precedence. For the complete list, see the Java Tutorial link mentioned above.
Precedence can be overridden using parentheses.
Equality and Relational Operators
| == | equal to |
| != | not equal to |
| > | greater than |
| >= | greater than or equal to |
| < | less than |
| <= | less than or equal to |
Conditional Operators
| && | conditional-AND |
| || | conditional-OR |
| ?: | ternary (shorthand for if-else statement) |
Type Comparison Operator
| instanceof | Compares an object to a specified type |
Shorthand Assignments
Also known as Compound Assignment Operator. This operator can be used to connect Arithmetic operator with an Assignment operator.
| Operator | Descriptions |
|---|---|
| += | Increments then assigns |
| -= | Decrements then assigns |
| *= | Multiply then assigns |
| /= | Divide then assigns |
| %= | Modulus then assigns |
| &= | Binary AND then assigns |
| ^= | Binary exclusive OR then assigns |
| |= | Binary inclusive OR then assigns |
Type Conversion in Assignments
When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.
Widening conversion takes place when two data types are automatically converted. This happens when:
- The two data types are compatible.
- When we assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.

Casting Incompatible Types
Although the automatic type conversions are helpful, they will not fulfill all programming needs because they apply only to widening conversions between compatible types. For all other cases you must employ a cast. A cast is an instruction to the compiler to convert one type into another. Thus, it requests an explicit type conversion. A cast has this general form:
(target_type) expression
Expressions
A Java expression consists of variables, operators, literals, and method calls. To know more about method calls, visit Java methods. For example,
int score;
score = 90;