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
// Addition
int sum = 2 + 3; // sum = 5
// Subtraction
int difference = 5 - 2; // difference = 3
// Multiplication
int product = 2 * 3; // product = 6
// Division
double quotient = 10.0 / 3.0; // quotient = 3.3333333333333335
// Modulus (remainder)
int remainder = 10 % 3; // remainder = 1
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