Variables in Java are indeed containers for storing data values, and there are various types of variables available to accommodate different data types and values. Let's briefly discuss each type mentioned:

  1. String:

    • The String type is used to store text data. String values are sequences of characters enclosed within double quotes (").
    • Example: "Hello", "Java is awesome"
  2. int:

    • The int type is used to store integer values, which are whole numbers without any decimal points.
    • Example: 123, -456
  3. float:

    • The float type is used to store floating-point numbers, which are numbers with decimal points.
    • Example: 19.99f, -3.14f
  4. char:

    • The char type is used to store single characters, enclosed within single quotes (').
    • Example: 'a', 'B'
  5. boolean:

    • The boolean type is used to store values representing two states: true or false.
    • Example: true, false

In addition to these primitive data types, Java also supports other data types such as double, long, byte, short, and more, each with its specific range and usage.

Here's a simple example demonstrating the declaration and initialization of variables of different types:



Practice Excercise Practice now