Webopedia on Google+Webopedia on TwitterWebopedia on FacebookTech Bytes Blog
Main » guides »

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.

Webopedia Study GuideIn 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 2
Webopedia Study Guide SectionJava Arithmetic Operators
Webopedia Study Guide SectionJava Relational and Logical Operators
Webopedia Study Guide SectionJava Modifiers
Webopedia Study Guide SectionJava Control Structures
Webopedia Study Guide Section Summing it up

Java Basics Study Guide, Part 1
Getting Started: Key Terms to Know
Essential Tools
Java Variables: An Introduction
- Primitive data types
- Declaring a Variable in Java
Java Syntax and Basic Conventions
Java Programming Best Practices

Java Arithmetic Operators

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: +  -   /   *   %   ++   --

Java Relational and Logical Operators

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.

  1. == (equal to): Checks if the expressions on each side are equal (true if they are)
  2. != (not equal to): Checks if the expressions on each side are equal (true if they are not)
  3. > (greater than): Checks if the left expression is greater than the right expression (true if it is)
  4. < (less than): Checks if the left expression is less than the right expression (true if it is)
  5. >= (greater than or equal to): Checks if the left expression is greater than, or equal to, the right (true if it is)
  6. <= (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.

  1. && (logical AND): Checks if the expression on each side is true. If they are both true, the AND expression will be true
  2. || (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.
  3. ! (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).

Java Modifiers

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.

  1. Visible to the package: This is default, no modifier needed.
  2. Visible to the class only: This uses the ‘private’ modifier.
  3. Visible to the world: This uses the ‘public’ modifier.
  4. Visible to the package and all subclasses: This uses the ‘protected’ modifier

To use a modifier, declare a variable like this:

Java Modifier Example

Java Control Structures

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:

Java if statement example

Using real code, this might look like:

Java if Statement example 2

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.

Java Example Counter

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:

Java For Loop

Summing it up

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

What's Hot in Tech: AI Tops the List

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 Basics, Part 1

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 »