Types and operators theory + first problem: comparing disk usage between two servers
Problem statement
Every value in a program has a type, what kind of thing it is. A whole number like 8080 is an int. A number with a decimal point like 245.5 is a float (Python, C++) or double (Java). Text like "web-01" is a string. The type decides what you're allowed to do with a value, and how much precision it can hold.
Operators are the symbols that work on values: +, -, *, / for arithmetic, and >, <, == for comparing two values. A comparison operator doesn't calculate a new number, it answers a yes/no question and gives you back true or false.
- Python:
diskA > diskB - Java:
diskA > diskB - C++:
diskA > diskB
Same symbol, same meaning, in all three, this is one of the few things that doesn't change across languages.
A comparison operator always produces exactly one of these two values, never a number, never the original values themselves. That's what makes it different from an arithmetic operator like + or -, which produces a new value of the same type you started with.
Common mistake: using = when you mean ==.
Wrong: diskA = diskB (this does NOT compare)Right: diskA == diskB (this compares)A single = is assignment: it copies diskB's value into diskA, overwriting whatever was there. A double == is comparison: it asks "are these equal?" and changes nothing. Mixing them up is one of the most common beginner bugs in all three languages.
Your task: two servers report their disk usage in GB as diskA = 245.5 and diskB = 198.75. Print whether diskA is using more space than diskB.
Expected output:
truePython prints True with a capital T. Java and C++ print true. Same answer, different spelling.
Approach
The comparison itself is identical across languages, diskA > diskB. What differs is how each language declares a decimal-valued variable and prints a boolean result:
- Python doesn't need a type keyword.
diskA = 245.5is automatically afloat, andprint(diskA > diskB)printsTrueorFalse. - Java needs the type stated up front,
double diskA = 245.5;, andSystem.out.println(diskA > diskB);printstrueorfalse. - C++ is the same idea as Java,
double diskA = 245.5;, andstd::cout << (diskA > diskB) << std::endl;prints1for true and0for false unless you turn onstd::boolalpha, which switches it to printtrue/falseinstead.
diskA = 245.5
diskB = 198.75
print(diskA > diskB)