Data types & Identifier | Java

Data types & Identifier | Java

Basics • Java Fundamentals

In Java every variable and every expression has some type each and every data type define the type of data that a variable can hold. Every assignment should be checked by compiler for type compatibility.

A variable is a named container for a particular set of bits or type of data.

Because of above reason we can conclude that Java language is strongly typed language. So that the defined value and variable type should match.

int y = 10;
int x = 10.5;  // Type mismatch error
boolean a = 0; // Type mismatch error

Java is not considered as pure object oriented language because several feature is not satisfied by Java like operator overloading, multiple inheritance etc.

Moreover we are depending on primitive data which are non objects.

In Java there are two types of data types -

  1. Primitive Data type: Primitive data types are the basic data types provided by Java. They are used to represent simple values.

  2. Reference data type: Reference data types are used to store references to objects. They don't store the actual data but rather a reference (memory address) to the location where the data is stored.

Primitive data type

Primitive data types are the basic data types provided by Java. They are used to represent simple values. they are the basic structure for building sophisticated data type.

it is of two types -

  1. Numeric data types

    • Integeral Data type: This group includes byte, short, int, and long, which are for whole valued signed numbers.

    • Floating data type: Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. This group includes float and double.

  2. Non-Numeric data type

    • char : This group includes char, which represents symbols in a character set, like letters and numbers.

    • boolean : This group includes boolean, which is a special type for representing true/false values

Numeric data type are also called signed data type.

For signed data type we can assign Positive and negative number for example -

int a = -10;
int b = 10;
double c = -10.5;
char d = -'a';      //Invalid
boolean e = -false; //Invalid

byte

  • This is smallest integeral data type.

  • It is Best choice if you want to handle data in terms of his dream either from the file or from network file supported or network supported form is byte.

  • Used using byte keyword followed by variable name.

Size: 8 bits
Min value: -127
Max value: 128
Range: -128 to 127

Note: The most significant bit act as sign bit where 0 means positive number directly in memory, 1 mean negative number represented in two's complement form.

byte b = 12;
byte b = 127;
byte b = 128;    // Type mismatch can't convert from int to byte
byte b = 10.2;   // Type mismatch can't convert from double to byte
byte b = false;  // Type mismatch can't convert from boolean to byte
byte b = 'java'; // Invalid character constant

short

  • Most rarely used data type in Java.

  • Short data type is best suitable for 16 bit processor like 8085 (Used in mid 90s) but these processors are completely outdated and hence corresponding data piece outdated data type.

  • Used using short keyword followed by variable name.

Size: 2 bytes
Min value: - 215
Max value: 215 - 1
Range: -32,768 to 32,767

short b = 31127;
short b = 32767;
short b = 32768;  // Type mismatch can't convert from int to short
short b = 10.2;   // Type mismatch can't convert from double to short
short b = false;  // Type mismatch can't convert from boolean to short
short b = 'java'; // Invalid character constant

int

  • Most commonly used data type in Java.

  • It's a signed data type.

  • Variables of type int are commonly employed to control loops and to index arrays.

  • Although you might think that using a byte or short would be more efficient than using an int in situations in which the larger range of an int is not needed, this may not be the case. The reason is that when byte and short values are used in an expression, they are promoted to int when the expression is evaluated.

Size: 4 bytes
Min value: - 231
Max value: 231 - 1
Range: -2,14,74,83,648 to 2,14,74,83,647

int b = 7483647;
int b = 2147483647;
int b = 2147483648; // Type mismatch can't convert from long to int
int b = 483648;     // Type mismatch can't convert from double to int
int b = false;      // Type mismatch can't convert from boolean to int
int b = 'java';     // Invalid character constant

long

  • Sometimes it may not enough to hold big values then we should go for long type.

  • For example - The amount of distance travelled by light in 1000 days, to hold this value int may not enough we should go for long data type.

    long l = 12600 * 60 * 60 * 24 * 1000;

Size: 8 bytes
Min value: - 263
Max value: 263 - 1

Note:

  • All the work data types (byte, short, int, long) meant for representing integral value.

  • If you want to represent floating point values then use then we should go for float data type

float

    • The type float specifies a single-precision value that uses 32 bits of storage.

      • 5 to 6 places after decimal.

      • Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small.

      • Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision.

      • For example, float can be useful when representing dollars and cents.

Size: 4 bytes
Min value: 1.4e–045
Max value: 3.4e+038

double

    • Double precision, as denoted by the double keyword.

      • Uses 64 bits to store a value.

      • 14 to 15 places after decimal.

      • Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations.

      • All transcendental math functions, such as sin(), cos(), and sqrt(), return double values.

      • When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.

Size: 8 bytes
Min value: -4.9e–324
Max value: 1.8e+308

boolean

  • Size not applicable it is a virtual machine dependent.

  • Range are not applicable but allowed values are true or false

  • boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

boolean b = true;
boolean b = 0;     // Cannot convert int to boolean
boolean = True;    // Invalid
boolean = "True";

For boolean b = True, compiler treating True as variable that you declare and true as symbol because it's neither boolean, not int nor string.

public class App {
    public static void main(String[] args) throws Exception {
        if (1) {
            System.out.println("Hi");
        }
    }
}

char

  • It is a data type used to store character values.

  • Java uses Unicode to represent characters.

  • The number of different allowed ASCII characters are less than or equal to 256.

  • To represent these 256 characters a bits are enough hence the size of character in old languages is 1 byte i.e., 8 bit.

  • Java is unique based and the number of different unique code characters are greater than 256 or less than or equal to 65,536.

  • To represent these many characters 8 bit may not enough to compulsory we should go for 16 bit hence the size of character in Java is 2 bytes.

Size: 2 bytes
Range: 0 to 65536

char ch = 97;
System.out.println(ch); // a

Note: null is a default values for object reference. we can't apply for primitive if we are trying to use for primitive then we will get compile time error.

Identifier

A name in Java programme is called identifier which can be used for identification purpose it can be method name or variable name, class name or label name.

For example -

public class App {
    public static void main(String[] args) throws Exception {
        char ch = 97;
        System.out.println(ch); // a
    }
}

In the above programme following are the identifier -

App: Name of class

main: Name of method

String: Name of array

args: Predefined Java class name

ch: Variable name

Rules for defining Java identifier

Rule 1 - The only allowed characters in Java into fire are -

  • A to Z

  • a to z

  • 1 to 9

  • $ or _

If you use any other characters we will get compile time error.

int total_number;   // Valid
int total#;         // Invalid

Rule 2 - Identifier cant start with digits.

int total45;   // Valid
int 4total;    // Invalid

Rule 3 - Java identifiers are case sensitive. Java language itself is case sensitive programming language.

class Test{
    // All variables have different memory allocated
    int number = 10;
    int Number = 20;
    int NUMBER = 30;
}

Rule 4 - There is no luck limit for Java identifier but it not recommended to take too lengthy identifier.

Rule 5 - We cant use reserve word or keywords as identifier.

int total45 = 20;   // Valid
int if = 20;        // Invalid 'if' is a keyword

Note: All predefined class name and method names can be used as Identifiers. Even though its valid but its not good programming practise because it reduces reliability and create confusion.

public class App {
    public static void main(String[] args) throws Exception {
        char String = 97;
        System.out.println(String); // a
    }
}

In above program we can use String as an identifier but it might create confusion so it's not recommended.

Conclusion

Java data types include primitives (numeric, non-numeric) and references. It's a strongly typed language, enforcing type compatibility. Key numeric types: byte, short, int, long, float, double. Non-numeric types: char, boolean.

Did you find this article valuable?

Support Xander Billa by becoming a sponsor. Any amount is appreciated!