Java Data Types

Data types in Java specify the type of data that a variable can store. Java is a statically typed language, which means that all variables must be declared with a specific data type before they can be used.

int: Integer Numbers

Theintdata type is used to store integer numbers. An integer is a whole number (i.e., a number without any decimal points). For example, 42, -17, and 0 are all integer numbers.

double: Floating-Point Numbers

Thedoubledata type is used to store floating-point numbers. A floating-point number is a number that contains a decimal point. For example, 3.14, -0.001, and 2.71828 are all floating-point numbers.

boolean: Boolean Values

Thebooleandata type is used to store boolean values, which can be eithertrue or false. Boolean values are often used in expressions to make decisions in a program.

String: Textual Data

TheStringdata type is used to store textual data, such as words and sentences. In Java, a string is enclosed in double quotes. For example, "Hello", "Java", and "World!" are all strings.

Naming Conventions

When naming variables in Java, there are some rules and conventions that you should follow:

  • Variable names are case-sensitive.
  • A variable name can only contain letters, digits, dollar signs, and underscores.
  • The name cannot begin with a digit. It can begin with a letter, a dollar sign ("$"), or an underscore ("_").
  • Use a name that reflects the purpose of the variable. For example, if the variable is used to store the age of a person, you can name it "age".