Java Basics Part 2: Operators, Modifiers and Structures
This second Study Guide describes the basics of Java, providing an overview of operators, modifiers and control Structures.
In Part One of our Learn Java Basics Study Guide we looked at a number of topics, including key terminology, data types, variables, syntax and Java programming best practices. In this, Part Two, we will discuss additional Java basics including operators, modifiers and control structures.
This Webopedia Study Guide is an introduction to the Java programming language and is intended for the beginner student. No programming experience is required to use this guide.
Java Basics Study Guide Checklist
Java Basics Study Guide, Part 1
Getting Started: Key Terms to Know
Java Variables: An Introduction
- Primitive data types
- Declaring a Variable in Java
Java Syntax and Basic Conventions
Java Programming Best Practices
Java uses many different operators. The first group of operators is the arithmetic operators. Some look familiar and in fact will work exactly as you expect them to.
For example: 1+1 does indeed give you a value of 2. Similarly, 1*5 yields 5. 9-6 will give you 3, and 12/3 will give you 4. Fortunately, the 4 basic arithmetic operators are the same in Java as they are in mathematics. Others may be a new concept.
Here are a few examples:
- 10%4 – This is the modulus operator (%), and it returns the remainder of the left hand side divided by the right. In this case, the remainder of 10/4 is 2, so 10%4 is 2.
- A++ — This looks like the addition operator, however it is an incremental operator. If A was a variable set to 3, A++ would give you 4. Similarly, A-- would give you 2
Remember these 7 operators if you want to do any kind of calculation in your Java programs: + - / * % ++ --
In programming, you may want to know if two variables or expressions are equal. If you have a variable called A and a variable called B, you can ask if they are equal by using the following:
A == B
Which will output a result of either true if they are equal or false if they are not.
In Java '==' is known as a relational operator, the first of six that the language recognizes. All 6 relational operators return one of two responses: true or false. The most common use case for these operators is within a control structure, such as an if-statement, due to the true or false nature of the operators.
- == (equal to): Checks if the expressions on each side are equal (true if they are)
- != (not equal to): Checks if the expressions on each side are equal (true if they are not)
- > (greater than): Checks if the left expression is greater than the right expression (true if it is)
- < (less than): Checks if the left expression is less than the right expression (true if it is)
- >= (greater than or equal to): Checks if the left expression is greater than, or equal to, the right (true if it is)
- <= (less than or equal to): Checks if the left expression is less than, or equal to, the right (true if it is)
Additionally, you may need to use some logical statements. In Java, there are 3 logical operators, one of which can be seen in the list above.
- && (logical AND): Checks if the expression on each side is true. If they are both true, the AND expression will be true
- || (logical OR): Checks if the expression on each side is true. If at least one of the expressions are true, the OR expression will be true.
- ! (logical not): This operator is unary, meaning it only operates on one expression. It negates the expression (turns a true to a false, and a false to a true).
When you create a variable, method or class in Java you may wish to change its meaning or its access level. There are four levels of access you can assign using modifiers.
- Visible to the package: This is default, no modifier needed.
- Visible to the class only: This uses the ‘private’ modifier.
- Visible to the world: This uses the ‘public’ modifier.
- Visible to the package and all subclasses: This uses the ‘protected’ modifier
To use a modifier, declare a variable like this:
Two important control structures to know at the beginning stages of Java are loops and if-statements.
Java if-statements Explained
If-Statements help decide the flow of a program by asking a logic question, then based on whether the result is true or false, decides which code block to execute. Here is the basic syntax:
Using real code, this might look like:
The result would be "The sum is greater than 1000."
Only one code block from an if-statement can be run. So once a condition is found to be true, the code belonging to all other conditions is ignored. Additionally, you do not need to use else if, or else. You can simply have an if. This is useful if you don’t want anything special to happen when the condition is false.
Java Loops Explained
Java loops are a control structure that that allows a code block to be executed multiple times in a row, without actually rewriting the code. Here is an example of a counting program, where you execute code over and over again.
This program will add 1 to the variable sum, 10 times, and print it each time. The for-loop used has a specific syntax that looks like this:
Congratulations! You now have a better understanding of Java basics and can construct a few basic objectless java programs using the correct syntax, including variables and logic. After practicing – and mastering – the Java basics in this Webopedia Study Guide, you will have the background knowledge required to delve into the more complex topics such as Object Oriented Programming.
<< PREVIOUS PAGE Java Basics Study Guide, Part 1
Stay up to date on the latest developments in Internet terminology with a free weekly newsletter from Webopedia. Join to subscribe now.
Like everything in technology, AI touches on so many other trends, like self-driving cars and automation, and Big Data and the Internet of Things... Read More »DevOp's Role in Application Security
As organizations rush to release new applications, security appears to be getting short shrift. DevSecOps is a new approach that holds promise. Read More »Slideshow: Easy Editorial SEO Tips to Boost Traffic
This slideshow reviews five easy on-page editorial SEO tips to help drive organic search engine traffic, including the page title, heading,... Read More »
Java is a high-level programming language. This guide describes the basics of Java, providing an overview of syntax, variables, data types and... Read More »Java Basics, Part 2
This second Study Guide describes the basics of Java, providing an overview of operators, modifiers and control Structures. Read More »The 7 Layers of the OSI Model
The Open System Interconnection (OSI) model defines a networking framework to implement protocols in seven layers. Use this handy guide to compare... Read More »