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:
-
String:
- The
Stringtype is used to store text data. String values are sequences of characters enclosed within double quotes ("). - Example:
"Hello","Java is awesome"
- The
-
int:
- The
inttype is used to store integer values, which are whole numbers without any decimal points. - Example:
123,-456
- The
-
float:
- The
floattype is used to store floating-point numbers, which are numbers with decimal points. - Example:
19.99f,-3.14f
- The
-
char:
- The
chartype is used to store single characters, enclosed within single quotes ('). - Example:
'a','B'
- The
-
boolean:
- The
booleantype is used to store values representing two states:trueorfalse. - Example:
true,false
- The
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