types of variables

What are the types of variables in java

In Java, variables are containers that store data values. There are several types of variables in Java, broadly categorized into primitive types and reference types.

types of variables

1. Primitive Types:

1. byte: (Size: 8 bits)

Example
byte myByte = 127;

2. short: (Size: 16 bits)

Example
short myShort = 32000;

3. int: (Size: 32 bits)

Example
int myInt = 123456;

4. long: (Size: 64 bits)

Example
long myLong = 123456789L;

5. float: (Size: 32 bits – single-precision)

Example
float myFloat = 3.14f;

6. double: (Size: 64 bits – double-precision)

Example
double myDouble = 3.14159265359;

7. char: (Size: 16 bits)

Example
char myChar = 'A';

8. boolean: (Size: Not precisely defined, typically implementation-dependent)

Example
boolean isTrue = true;

2. Reference Types:

1. Object References:

Example
String myString = "Hello, Java!";

//or

Integer myInteger = 42;

2. Array References:

Example
int[] myArray = {1, 2, 3, 4, 5};

3. Custom Class References:

Example
class MyClass {
    // class definition
}

MyClass myObject = new MyClass();

4. Interface References:

Example
interface MyInterface {
    // interface definition
}

MyInterface myImplementation = new MyInterface() {
    // anonymous implementation
};

These are the basic types of variables in Java. It’s important to choose the appropriate type based on the nature of the data you want to store. Primitive types are used for simple values, while reference types are used for more complex structures and objects.

Leave a Reply

Your email address will not be published. Required fields are marked *