Data types and variables

Understanding data types

In programming, data types are used to classify different types of data. Here are some common data types:

  • Integer: int age = 25;

  • Floating-point: double pi = 3.14;

  • Boolean: boolean isTrue = true;

  • Character: char letter = 'a';

  • String: String name = "Alice";

Declaring and initializing variables

Variables are used to store data in a program. Here's an example of how to declare and initialize variables in Java:

// Declare and initialize an integer variable
int age = 25;

// Declare and initialize a double variable
double pi = 3.14;

// Declare and initialize a boolean variable
boolean isTrue = true;

// Declare and initialize a character variable
char letter = 'a';

// Declare and initialize a string variable
String name = "Alice";

Basic arithmetic operations

Arithmetic operations are used to perform mathematical calculations in a program. Here are some examples

In the above examples, int and double are data types used to represent integer and floating-point numbers, respectively. The arithmetic operators (+, -, *, /, and %) are used to perform addition, subtraction, multiplication, division, and modulus (remainder) operations, respectively.

Last updated