DATA TYPES
A variable in Java must be a specified data type:
*Primitive datatype
*Non primitive datatype
Primitive datatype:
Java's primitive data types are the fundamental building blocks for storing basic data values, and they include boolean, char, byte, short, int, long, float, and double
For example:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
*Boolean Data Type
*In Java, the boolean data type represents a single bit of information with two possible states: true or false. The size of the Boolean data type is 1 byte (8 bits).
*It is used to store the result of logical expressions or conditions. Unlike other primitive data types like int or double, boolean does not have a specific size or range. It is typically implemented as a single bit, although the exact implementation may vary across platforms.
*Byte Data Type
*The byte data type in Java is a primitive data type that represents an 8-bits signed two's complement integer.
*The byte data type is commonly used when working with raw binary data or when memory conservation is a concern, as it occupies less memory than larger integer types like int or long.
_*Short Data Type
*The short data type in Java is a primitive data type that represents a 16-bits signed two-complement integer.
*Similar to the byte data type, short is used when memory conservation is a concern, but more precision than byte is required. Its default value is 0.
_*int Data Type
*The int data type in Java is a primitive data type that represents a 32-bits signed two's complement integer.
*The int data type is one of the most commonly used data types. It is typically used to store whole numbers without decimal points. Its default value is 0.
_*long Data Type
*The long data type in Java is a primitive data type that represents a 64-bits signed two's complement integer.
*The long data type is used when the int data type is not large enough to hold the desired value or when a larger range of integer values is needed.
_*float Data Type
*The float data type in Java is a primitive data type that represents single-precision 32-bits IEEE 754 floating-point numbers. It can represent a wide range of decimal values, but it is not suitable for precise values such as currency. Its default value is 0.0f or 0.0F.
*The float data type is useful for applications where a higher range of values is needed and precision is not critical.
_*double Data Type
The double data type in Java is a primitive data type that represents double-precision 64-bits IEEE 754 floating-point numbers. Its default value is 0.0. It provides a wider range of values and greater precision compared to the float data type, which makes it suitable for applications where accurate representation of decimal values is required.
_*char Data Type
*The char data type in Java is a primitive data type that represents a single 16-bits Unicode character. It can store any character from the Unicode character set, which allows Java to support the internationalization and representation of characters from various languages and writing systems.
*The char data type is commonly used to represent characters, such as letters, digits, and symbols. It can also be used to perform arithmetic operations, as the Unicode values of characters can be treated as integers.
Non primitive datatype:
Non-primitive data types in Java are not predefined. They are created by the programmer. Non-primitive data types are also called 'reference variables' or 'object references' as they reference a memory location where data is stored. Some of the examples of non-primitive types include strings, arrays, and classes
_*Class and objects:
*A class in Java is a user defined data type i.e. it is created by the user. It acts a template to the data which consists of member variables and methods.
*An object is the variable of the class, which can access the elements of class i.e. methods and variables.
_*String :
*Strings are used for storing text.
*A String variable contains a collection of characters surrounded by double quotes for ex "Hello world"
Top comments (0)