Performing an action in java is done through use of statements. You can add, subtract or use methods on various variables (containers that hold either abstract values, or location in memory of where an object (in terms of its own abstract code) is stored. Each statement ends with a semi-colon so the program knows when to move on, since whitespace does not matter.
There are several special statements you can use, some of which are method creation, conditionals and loops. Methods are a collection of statements that can be called all at once at any point by calling the method name on the object in question. Example: We have a class representing a deck of cards. To shuffle this deck, in terms of programming, we would need to get a random number, and then flip the card at that number with the current card. Instead of requiring this every time you want to shuffle, put it in a method named shuffle, then call Deck.shuffle().
If-statements take the form: if ( condition ) { action }; with any subsequent cases being noted with else if, and the final case being noted with just else.
Loops consist of while loops, and for loops. While loops take the form: while ( condition ) { action }; and repeats until the condition is no longer true. Thus, we need to make sure that within the action body, we modify the condition (often an integer) such that eventually, it'll break the while loop.
For loops have 2 types, there's the for-each loops, taking the form: for ( Type : list ) { action }; which takes every element of the list or array provided with Type's type, and performs the action on it.
The regular for loops takes the form: for ( int i = startingValue ; condition tied to i's value ; how to modify i at the end of each iteration ) { action }; which can be used for a set number of iterations.
Methods in Java can call other methods and even themselves. The latter is known as recursion and often simplifies code, although it is more stressful on the machine to run.
Declaring a variable requires you to place the type of the variable before the variable name. This is because each variable type or object takes up a different amount of space on disk and you want to assign the right amount of space. Similarly, if you want a method to return a value when it's done, such as a method to add 2 numbers that you actually want to use, you need to declare the returned values type when declaring the method.
There's a bunch of stuff on data files, but there's not really a way to easily summarize that, so go back and find it, I guess, as well as anything else I missed! Unless there's a specific question anyone has?
Otherwise, that's it for now..Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!
And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us.
How did you learn all this?
ReplyDeleteWell, you surely know a lot, I'm still trying to catch up with your last two posts.
ReplyDeletegood stuff man!
ReplyDelete@My 2 Pesos : I'm teaching it to myself currently. Every day I sit with the book in front of me, learn a chunk, practice it and just generally try to understand it. When I'm done that, I make a post about it!
ReplyDeletethanks so much for sharing all of this, it's really useful and accessible! good job!
ReplyDelete