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