Data Types in Java
1) Primitive Data Types
- These are the most basic data types in Java. They represent single values and are not objects.
There are different primitive data types -
There are different primitive data types -
a) Integer Types-
- byte - 8-bit signed integer. Range varies from -128 to 127
- short - 16-bit signed integer. Range varies from -32,768 to 32,767
- int - 32-bit signed integer. Range varies from -2^31 to 2^31-1
Example - int num = 10;
- long- 64-bit signed integer. Range varies from -2^63 to 2^63-1
b) Floating-Point types:
- float - 32-bit floating-point number. It should be suffixed with 'f' or 'F'
- double - 64-bit floating-point number.
Example - double pi = 3.14;
c) Other Primitive types:
- char - 16-bit Unicode character.
Example - char letter = 'H';
- boolean - Represents the two values - true and false
Example - boolean flag = true;
2) Reference Data Types
- These are used to refer to objects. They do not store the actual data but store the reference (Memory Address) of the object.
There are different reference data types -
a) Class types-
- String - Represent a sequence of characters.
Example - String text = "Hello";
b) Array types-
- Arrays - Ordered collection of elements of the same type.
c) Interface types-
- Interfaces - Defines a set of methods that a class must implement.
Comments in Java
Single-Line comments start with //
Multi-Line comments starts with /* and ends with */
Example-
// This is a single-line comment /* * This is a multi-line comment * This is the second multi-line comment. */ |