The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. 
Home Variables and Types Composition  
Search the VIAS Library  Index  
Composition
So far we have looked at the elements of a programming languagevariables, expressions, and statementsin isolation, without talking about how to combine them. One of the most useful features of programming languages is their ability to take small building blocks and compose them. For example, we know how to multiply numbers and we know how to print; it turns out we can do both at the same time: System.out.println (17 * 3);Actually, I shouldn't say "at the same time," since in reality the multiplication has to happen before the printing, but the point is that any expression, involving numbers, strings, and variables, can be used inside a print statement. We've already seen one example: System.out.println (hour*60 + minute);But you can also put arbitrary expressions on the righthand side of an assignment statement: int percentage;percentage = (minute * 100) / 60; This ability may not seem so impressive now, but we will see other examples where composition makes it possible to express complex computations neatly and concisely. WARNING: There are limits on where you can use certain expressions; most notably, the lefthand side of an assignment statement has to be a variable name, not an expression. That's because the left side indicates the storage location where the result will go. Expressions do not represent storage locations, only values. So the following is illegal: minute+1 = hour;.


Home Variables and Types Composition 