Essential Java Programming - Language Statements
The course is part of this learning path
Essential Java Programming
This training course takes you through many of the essential Java programming features. We’ll review in depth features like Language Statements, Using Strings, Subclasses, Fields and Variables, Using Arrays, Java Packages and Visibility, and much more.
- Understand key Java language statements and keywords
- Be able to develop confidently with Strings
- Be able to implement specialization using subclasses
- Work with fields and variables
- Implement Arrays to store multiple values in a single variable
- Learn how to structure your Java code using Packages and Visibility
- A basic understanding of software development
- A basic understanding of the software development life cycle
- Software Engineers interested in learning Java to develop applications
- Software Architects interested in learning Java to design applications
- Anyone interested in basic Java application development and associated tooling
- Anyone interested in understanding the basics of the Java SDK
- [Male Narrator] Okay welcome back. In this lecture we'll familiarize ourselves with many of the key JAVA language statements such as the if-else conditional statements, the looping statements while and for. The arithmetic operators, increment and decrement operators, the comparison operators and several other ones. For starters JAVA has the complete C operator suite with one minor addition to the batch of operators. Operator precedence is the same as C. It may be altered with the use of parenthesis. If you are unsure of evaluation order, use parenthesis to make the meaning of your code clear. If you know C, JAVA will be very familiar. All statements can be multi lined and are terminated with a semi colon. When using the JAVA shorthand increment and decrement operators, take time to understand the difference and behavior run time. In the example showing here, you can see that the value printed out within the print integer method is different depending on whether the earlier prefix or postfix operator was used to increment the X variable being passed into the print integer method. JAVA's comparison operators always result in a Boolean value. This is different from C or C plus plus and eliminates the misuse of the assignment operator, a single equal sign, this is the equality operator which is a double equal sign. If statements may be nested and if-else trees are supported. In general it is good practice to over use braces to make the meaning of your code clear to yourself and others. When a conditional operation is a single statement only, curly brackets are optional. Logical operators are used to combine test. There is a subtle difference when using double character logical operators, this is single character logical operators. Let's now quickly review each of these, first the doubles. A double empiecen is used to perform a logical end with processing stopped on the first step that evaluates false. Double pipes are used to perform a logical or with processing stopped on the first step that evaluates true. And now for the singles.
A single empiecen is used to perform a logical end with all steps processed and a single pipe is used to perform logical or with all steps processed. The ternary operator is very useful when you want to test the condition of something and then based on the outcome perform value assignment all on a single line using Compaq it expressive notation. The for statement is used to control looping. The general form is starting values and ending condition test and then incrementing a value. You may initialize multiple starting values and have multiple increment expressions. JAVA five introduced the popular enhanced for loop. When a collection of data is to be iterated, the enhanced for loop can be used. It's simple structure allows one to simply code by presenting for loops that visit each element of an array or collection without explicitly expressing how one goes from one element to the other. Additionally, using the syntax prevents the need for explicitly having to cast each iterated item if the type of each element in the collection is unknown. We'll go into this in further detail later on. A while statement may execute zero or more times with a test being performed at the top of the loop cycle.
The key point of a while loop is that the test is performed at the top. Keep this in mind as we next review the next slide. The do statement will always execute at least once unlike the while statement in the previous slide, the do statement performs the test for completion at the end of each loop cycle. The continue statement continues the looping whereas the break statement breaks out of the loop. This behavior is very similar to the constructs in other programing languages. The switch statement eliminates complex if-else code structures by testing an integer value multiple times. If you do not have a break statement at the end of a case, execution will fall into the next case so by ordering your cases appropriately you can selectively execute multiple case blocks. This can be very confusing to someone reading your codes so do this very selectively and add comments where appropriate. When the break statement is not added to a case processing will continue down the case statements until a break statement is encountered. In the example above dark is printed for both the value black and brown. The use of the default key word is optional when non of the cases meet the criteria nothing is printed okay? Before we consider the following questions to test yourself on the content that we've just reviewed, the answers to the above questions are one, plus, minus, asterisks for times and forward slash for divide. Two, double equals, exclamation mark equals, greater than or equals, less then or equals, greater than and less than. Three, double empiecen and double pipe. Four, if-else, for, while do and switch. Five, comparison operators always return Boolean and six, yes. Okay they are complete so selector go ahead and close it and we will see you shortly in the next one.
About the Author
Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.
Jeremy holds professional certifications for both the AWS and GCP cloud platforms.