# AP Computer Science Unit 1 Review

Advanced Placement Computer Science A Unit 1 Review

Variables and Data Types

``````public class VariableExample {
public static void main(String[] args) {
// Defining and assigning a variable of type int
int age = 25;

// Defining and assigning a variable of type double
double temperature = 26.5;

// Defining and assigning a variable of type char

// Defining and assigning a variable of type boolean
boolean isRaining = true;
}
}
``````

Expressions and Assignment Statements

``````public class ExpressionExample {
public static void main(String[] args) {
int x = 10;
int y = 5;

// Arithmetic expressions
int sum = x + y;
int difference = x - y;
int product = x * y;
int quotient = x / y;

// Comparison expressions
boolean isEqual = (x == y);
boolean isGreater = (x > y);

// Logical expressions
boolean andResult = (x > 0) && (y > 0);
boolean orResult = (x > 0) || (y > 0);
}
}
``````

Compound Assignment Operators

``````public class CompoundAssignmentExample {
public static void main(String[] args) {
int x = 10;

// Compound assignment operators
x += 5; // x = x + 5;
x -= 3; // x = x - 3;
x *= 2; // x = x * 2;
x /= 4; // x = x / 4;
}
}
``````

Casting and Ranges of Variables

``````public class CastingExample {
public static void main(String[] args) {
// Widening conversion
int intValue = 10;
double doubleValue = intValue; // Automatic conversion

// Narrowing conversion (Caution: Data loss may occur!)
double doubleNum = 15.75;
int intNum = (int) doubleNum; // Manual conversion
}
}
``````