A variable in Java is a container that holds a value. It has a name and a specific data type.
Definition: A variable is a named storage location in a program’s memory that can hold different values during the execution of the program.
Example:
int age = 25;
String name = "John Doe";
A data type defines the kind of data a variable can hold and the operations that can be performed on it.
Java has two categories of data types:
// example of primitive data type
byte myByte = 127;
int myInt = 1000000;
double myDouble = 3.14159;
boolean isJavaFun = true;
char myChar = 'A';
String greeting = "Hello, World!";
int[] numbers = {1, 2, 3, 4, 5};
MyClass obj= new MyClass();
Operators are symbols that perform operations on variables and values.
Logical Operators: && (and), | (or), ! (not) |
Bitwise Operators: &, | , ^, ~, «, », »> |
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo - remainder)
++ (increment)
-- (decrement)
Example:
int a = 10;
int b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
increment and decrement operator postfix and prefix are not equivalent
int a=10,b=20,c=10,d=20;
int incrementA = a++; // a becomes 11, incrementA is 10
int decrementB = b--; // b becomes =19, decrementB is 20
int incrementC= ++c; // c becomes 11, incrementC is 11
int decrementD= --d; // d becomes 19m decrementB is 19
also call Comparison Operators
== (equal to)
!= (not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Example
boolean isGreater = 10 > 5; // Comparison
int x = 5;
int y = 8;
boolean isEqual = (x == y); // false
boolean isGreater = (x > y); // false
boolean notEqual = (x != y); // true
&& (logical AND)
|| (logical OR)
! (logical NOT)
Example
boolean result = (5 > 3) && (2 < 4); // Logical AND
= (assignment)
+= (addition assignment)
-= (subtraction assignment)
*= (multiplication assignment)
/= (division assignment)
%= (modulo assignment)
Example
int x = 5;
x += 3; // Equivalent to x = x + 3
a) int x = 100;
b) integer x = 100;
c) x = 100;
d) int x = 100.0;
a) int
b) float
c) String
d) char
What is the output of the following code?
int a = 10; int b = 3; System.out.println(a % b);
a) 3
b) 1
c) 0
d) 3.33
a) &
b) ||
c) &&
d) AND
What is the result of the following expression?
boolean x = true; boolean y = false; System.out.println(x || y);
a) true
b) false
c) 1
d) 0
What is the value of the variable x after the following code executes?
int x = 5; x += 3;
a) 5
b) 3
c) 8
d) 2
a) /
b) %
c) *
d) +
a) num = num - 2;
b) num -= 2;
c) num–; num–;
d) All of the above
Write a Java program that:
Given two integers `a` and `b`. (if could, Prompts the user to enter two integers)
Calculates the sum, difference, product, quotient, and remainder of the two numbers.
Prints the results in a formatted output. **Note**: For the coding problem, you would typically be expected to write and compile the code, then provide the output or code itself as your answer.