- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Expressions
In this part of the Java tutorial, we will talk about expressions.
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators.
An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.
An operator usually has one or two operands. Those operators that work with only one operand are called unary operators. Those who work with two operands are called binary operators. There is also one ternary operator ?:
which works with three operands.
Certain operators may be used in different contexts. For example the +
operator. It can be used in different cases. It adds numbers, concatenates strings or indicates the sign of a number. We say that the operator is overloaded.
Sign operators
There are two sign operators: +
and -
. They are used to indicate or change the sign of a value.
package com.zetcode; public class SignOperators { public static void main(String[] args) { System.out.println(2); System.out.println(+2); System.out.println(-2); } }
The +
and -
signs indicate the sign of a value. The plus sign can be used to signal that we have a positive number. It can be omitted and it is mostly done so.
package com.zetcode; public class MinusSign { public static void main(String[] args) { int a = 1; System.out.println(-a); System.out.println(-(-a)); } }
The minus sign changes the sign of a value.
The assignment operator
The assignment operator =
assigns a value to a variable. A variable is a placeholder for a value. In mathematics, the = operator has a different meaning. In an equation, the =
operator is an equality operator. The left side of the equation is equal to the right one.
int x = 1;
Here we assign a number to the x
variable.
x = x + 1;
This expression does not make sense in mathematics. But it is legal in programming. The expression adds 1 to the x variable. The right side is equal to 2 and 2 is assigned to x.
3 = x;
This code line results in syntax error. We cannot assign a value to a literal.
Concatenating strings
In Java the + operator is also used to concatenate strings.
package com.zetcode; public class ConcatenateStrings { public static void main(String[] args) { System.out.println("Return " + "of " + "the king."); System.out.println("Return".concat(" of").concat(" the king.")); } }
We join three strings together.
System.out.println("Return " + "of " + "the king.");
Strings are joined with the +
operator.
System.out.println("Return".concat(" of").concat(" the king."));
An alternative method for concatenating strings is the concat
method.
$ java com.zetcode.ConcatenateStrings Return of the king. Return of the king.
This is the output of the com.zetcode.ConcatenateStrings
program.
Increment, decrement operators
Incrementing or decrementing a value by one is a common task in programming. Java has two convenient operators for this: ++
and --
.
x++; x = x + 1; ... y--; y = y - 1;
The above two pairs of expressions do the same.
package com.zetcode; public class IncDec { public static void main(String[] args) { int x = 6; x++; x++; System.out.println(x); x--; System.out.println(x); } }
In the above example, we demonstrate the usage of both operators.
int x = 6; x++; x++;
We initiate the x
variable to 6. Then we increment the x
two times. Now the variable equals to 8.
x--;
We use the decrement operator. Now the variable equals to 7.
$ java com.zetcode.IncDec 8 7
And here is the output of the example.
Arithmetic operators
The following is a table of arithmetic operators in Java.
Symbol | Name |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder |
The following example shows arithmetic operations.
package com.zetcode; public class Arithmetic { public static void main(String[] args) { int a = 10; int b = 11; int c = 12; int add = a + b + c; int sb = c - a; int mult = a * b; int div = c / 3; int rem = c % a; System.out.println(add); System.out.println(sb); System.out.println(mult); System.out.println(div); System.out.println(rem); } }
In the preceding example, we use addition, subtraction, multiplication, division, and remainder operations. This is all familiar from the mathematics.
int rem = c % a;
The %
operator is called the remainder or the modulo operator. It finds the remainder of division of one number by another. For example, 9 % 4
, 9 modulo 4 is 1, because 4 goes into 9 twice with a remainder of 1.
$ java com.zetcode.Arithmetic 33 2 110 4 2
This is the output of the example.
Next we will show the distinction between integer and floating point division.
package com.zetcode; public class Division { public static void main(String[] args) { int c = 5 / 2; System.out.println(c); double d = 5 / 2.0; System.out.println(d); } }
In the preceding example, we divide two numbers.
int c = 5 / 2;
In this code, we have done integer division. The returned value of the division operation is an integer. When we divide two integers the result is an integer.
double d = 5 / 2.0;
If one of the values is a double or a float, we perform a floating point division. In our case, the second operand is a double so the result is a double.
$ java com.zetcode.Division 2 2.5
We see the result of the com.zetcode.Division
program.
Boolean operators
In Java we have three logical operators. The boolean
keyword is used to declare a Boolean value.
Symbol | Name |
---|---|
&& | logical and |
|| | logical or |
! | negation |
Boolean operators are also called logical.
package com.zetcode; public class BooleanOperators { public static void main(String[] args) { int x = 3; int y = 8; System.out.println(x == y); System.out.println(y > x); if (y > x) { System.out.println("y is greater than x"); } } }
Many expressions result in a boolean value. Boolean values are used in conditional statements.
System.out.println(x == y); System.out.println(y > x);
Relational operators always result in a boolean value. These two lines print false and true.
if (y > x) { System.out.println("y is greater than x"); }
The body of the if
statement is executed only if the condition inside the parentheses is met. The y > x
returns true, so the message "y is greater than x" is printed to the terminal.
The true
and false
keywords represent boolean literals in Java.
package com.zetcode; public class AndOperator { public static void main(String[] args) { boolean a = true && true; boolean b = true && false; boolean c = false && true; boolean d = false && false; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
The code example shows the logical and operator. It evaluates to true only if both operands are true.
$ java com.zetcode.AndOperator true false false false
Only one expression results in true.
The logical or ||
operator evaluates to true if either of the operands is true.
package com.zetcode; public class OrOperator { public static void main(String[] args) { boolean a = true || true; boolean b = true || false; boolean c = false || true; boolean d = false || false; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
If one of the sides of the operator is true, the outcome of the operation is true.
$ java com.zetcode.OrOperator true true true false
Three of four expressions result in true.
The negation operator !
makes true false and false true.
package com.zetcode; public class Negation { public static void main(String[] args) { System.out.println(! true); System.out.println(! false); System.out.println(! (4 < 3)); } }
The example shows the negation operator in action.
$ java com.zetcode.Negation false true true
This is the output of the com.zetcode.Negation
program.
The ||
, and &&
operators are short circuit evaluated. Short circuit evaluation means that the second argument is only evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of the logical and evaluates to false, the overall value must be false; and when the first argument of logical or evaluates to true, the overall value must be true. Short circuit evaluation is used mainly to improve performance.
An example may clarify this a bit more.
package com.zetcode; public class ShortCircuit { public static boolean One() { System.out.println("Inside one"); return false; } public static boolean Two() { System.out.println("Inside two"); return true; } public static void main(String[] args) { System.out.println("Short circuit"); if (One() && Two()) { System.out.println("Pass"); } System.out.println("#############"); if (Two() || One()) { System.out.println("Pass"); } } }
We have two methods in the example. They are used as operands in boolean expressions. We will see if they are called or not.
if (One() && Two()) { System.out.println("Pass"); }
The One()
method returns false. The short circuit && does not evaluate the second method. It is not necessary. Once an operand is false, the result of the logical conclusion is always false. Only "Inside one" is only printed to the console.
if (Two() || One()) { System.out.println("Pass"); }
In the second case, we use the ||
operator and use the Two()
method as the first operand. In this case, "Inside two" and "Pass" strings are printed to the terminal. It is again not necessary to evaluate the second operand, since once the first operand evaluates to true, the logical or is always true.
$ java com.zetcode.ShortCircuit Short circuit Inside one ############# Inside two Pass
We see the result of the com.zetcode.ShortCircuit
program.
Relational Operators
Relational operators are used to compare values. These operators always result in a boolean value.
Symbol | Meaning |
---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
!= | not equal to |
Relational operators are also called comparison operators.
package com.zetcode; public class Relational { public static void main(String[] args) { System.out.println(3 < 4); System.out.println(3 == 4); System.out.println(4 >= 3); System.out.println(4 != 3); } }
In the code example, we have four expressions. These expressions compare integer values. The result of each of the expressions is either true or false. In Java we use the ==
to compare numbers. Some languages like Ada, Visual Basic, or Pascal use =
for comparing numbers.
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal, or hexadecimal symbols are only notations of a same number. Bitwise operators work with bits of a binary number. Bitwise operators are seldom used in higher level languages like Java.
Symbol | Meaning |
---|---|
~ | bitwise negation |
^ | bitwise exclusive or |
& | bitwise and |
| | bitwise or |
The bitwise negation operator changes each 1 to 0 and 0 to 1.
System.out.println(~7); // prints -8 System.out.println(~ -8); // prints 7
The operator reverts all bits of a number 7. One of the bits also determines, whether the number is negative or not. If we negate all the bits one more time, we get number 7 again.
The bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 only if both corresponding bits in the operands are 1.
00110 & 00011 = 00010
The first number is a binary notation of 6, the second is 3 and the result is 2.
System.out.println(6 & 3); // prints 2 System.out.println(3 & 6); // prints 2
The bitwise or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if either of the corresponding bits in the operands is 1.
00110 | 00011 = 00111
The result is 00110
or decimal 7.
System.out.println(6 | 3); // prints 7 System.out.println(3 | 6); // prints 7
The bitwise exclusive or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1.
00110 ^ 00011 = 00101
The result is 00101
or decimal 5.
System.out.println(6 ^ 3); // prints 5 System.out.println(3 ^ 6); // prints 5
Compound assignment operators
The compound assignment operators consist of two operators. They are shorthand operators.
a = a + 3; a += 3;
The +=
compound operator is one of these shorthand operators. The above two expressions are equal. Value 3 is added to the a variable.
Other compound operators are:
-= *= /= %= &= |= <<= >>=
package com.zetcode; public class CompoundOperators { public static void main(String[] args) { int a = 1; a = a + 1; System.out.println(a); a += 5; System.out.println(a); a *= 3; System.out.println(a); } }
In the example, we use two compound operators.
int a = 1; a = a + 1;
The a
variable is initiated to one. 1 is added to the variable using the non-shorthand notation.
a += 5;
Using a +=
compound operator, we add 5 to the a variable. The statement is equal to a = a + 5;
.
a *= 3;
Using the *=
operator, the a is multiplied by 3. The statement is equal to a = a * 3;
.
$ java com.zetcode.CompoundOperators 2 7 21
This is the example output.
The instanceof operator
The instanceof
operator compares an object to a specified type.
package com.zetcode; class Base {} class Derived extends Base {} public class InstanceofOperator { public static void main(String[] args) { Base b = new Base(); Derived d = new Derived(); System.out.println(d instanceof Base); System.out.println(b instanceof Derived); System.out.println(d instanceof Object); } }
In the example, we have two classes: one base and one derived from the base.
System.out.println(d instanceof Base);
This line checks if the variable d
points to the class that is an instance of the Base
class. Since the Derived
class inherits from the Base
class, it is also an instance of the Base
class too. The line prints true.
System.out.println(b instanceof Derived);
The b
object is not an instance of the Derived
class. This line prints false.
System.out.println(d instanceof Object);
Every class has Object
as a superclass. Therefore, the d
object is also an instance of the Object
class.
$ java com.zetcode.InstanceofOperator true false true
This is the output of the com.zetcode.InstanceofOperator
program.
Operator precedence
The operator precedence tells us which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.
What is the outcome of the following expression, 28 or 40?
3 + 5 * 5
Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.
(3 + 5) * 5
To change the order of evaluation, we can use parentheses. Expressions inside parentheses are always evaluated first. The result of the above expression is 40.
The following table shows common Java operators ordered by precedence (highest precedence first):
Operator | Meaning | Associativity |
---|---|---|
[] () . | array access, method invoke, object member access | Left-to-right |
++ -- + - | increment, decrement, unary plus and minus | Right-to-left |
! ~ (type) new | negation, bitwise NOT, type cast, object creation | Right-to-left |
* / % | multiplication, division, modulo | Left-to-right |
+ - | addition, subtraction | Left-to-right |
+ | string concatenation | Left-to-right |
<< >> >>> | shift | Left-to-right |
< <= > >= | relational | Left-to-right |
instanceof | type comparison | Left-to-right |
== != | equality | Left-to-right |
& | bitwise AND | Left-to-right |
^ | bitwise XOR | Left-to-right |
| | bitwise OR | Left-to-right |
&& | logical AND | Left-to-right |
|| | logical OR | Left-to-right |
? : | ternary | Right-to-left |
= | simple assignment | Right-to-left |
+= -= *= /= %= &= | compound assignment | Right-to-left |
^= |= <<= >>= >>>= | compound assignment | Right-to-left |
Table: Operator precedence and associativity
Operators on the same row of the table have the same precedence. If we use operators with the same precedence, then the associativity rule is applied.
package com.zetcode; public class Precedence { public static void main(String[] args) { System.out.println(3 + 5 * 5); System.out.println((3 + 5) * 5); System.out.println(! true | true); System.out.println(! (true | true)); } }
In this code example, we show a few expressions. The outcome of each expression is dependent on the precedence level.
System.out.println(3 + 5 * 5);
This line prints 28. The multiplication operator has a higher precedence than addition. First, the product of 5*5
is calculated, then 3 is added.
System.out.println(! true | true);
In this case, the negation operator has a higher precedence than the bitwise OR. First, the initial true value is negated to false, then the |
operator combines false and true, which gives true in the end.
$ java com.zetcode.Precedence 28 40 true false
This is the example output.
Associativity
Sometimes the precedence is not satisfactory to determine the outcome of an expression. There is another rule called associativity. The associativity of operators determines the order of evaluation of operators with the same precedence level.
9 / 3 * 3
What is the outcome of this expression, 9 or 1? The multiplication, deletion and the modulo operator are left to right associated. So the expression is evaluated this way: (9 / 3) * 3
and the result is 9.
Arithmetic, boolean, relational, and bitwise operators are all left to right associated. The assignment operators, ternary operator, increment, decrement, unary plus and minus, negation, bitwise NOT, type cast, object creation operators are right associated.
package com.zetcode; public class Associativity { public static void main(String[] args) { int a, b, c, d; a = b = c = d = 0; String str = String.format("%d %d %d %d", a, b, c, d); System.out.println(str); int j = 0; j *= 3 + 1; System.out.println(j); } }
In the example, we have two cases where the associativity rule determines the expression.
int a, b, c, d; a = b = c = d = 0;
The assignment operator is right to left associated. If the associativity was left to right, the previous expression would not be possible.
int j = 0; j *= 3 + 1;
The compound assignment operators are right to left associated. We might expect the result to be 1. But the actual result is 0. Because of the associativity. The expression on the right is evaluated first and than the compound assignment operator is applied.
$ java com.zetcode.Associativity 0 0 0 0 0
Output of the com.zetcode.Associativity
program.
The ternary operator
The ternary operator ?:
is a conditional operator. It is a convenient operator for cases where we want to pick up one of two values, depending on the conditional expression.
cond-exp ? exp1 : exp2
If cond-exp is true, exp1 is evaluated and the result is returned. If the cond-exp is false, exp2 is evaluated and its result is returned.
package com.zetcode; public class TernaryOperator { public static void main(String[] args) { int age = 31; boolean adult = age >= 18 ? true : false; System.out.println(String.format("Adult: %s", adult)); } }
In most countries the adulthood is based on your age. You are adult if you are older than a certain age. This is a situation for a ternary operator.
bool adult = age >= 18 ? true : false;
First the expression on the right side of the assignment operator is evaluated. The first phase of the ternary operator is the condition expression evaluation. So if the age is greater or equal to 18, the value following the ?
character is returned. If not, the value following the :
character is returned. The returned value is then assigned to the adult variable.
$ java com.zetcode.TernaryOperator Adult: true
A 31 years old person is adult.
Calculating prime numbers
In the following example, we are going to calculate prime numbers.
package com.zetcode; public class PrimeNumbers { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; System.out.print("Prime numbers: "); for (int num : nums) { if (num == 1 || num == 2 || num == 3) { System.out.print(num + " "); continue; } int i = (int) Math.sqrt(num); boolean isPrime = true; while (i > 1) { if (num % i == 0) { isPrime = false; } i--; } if (isPrime) { System.out.print(num + " "); } } System.out.print('\n'); } }
In the above example, we deal with several operators. A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. We pick up a number and divide it by numbers, from 1 up to the picked up number. Actually, we do not have to try all smaller numbers; we can divide by numbers up to the square root of the chosen number. The formula will work. We use the remainder division operator.
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
We will calculate primes from these numbers.
if (num == 1 || num == 2 || num == 3) { System.out.print(num + " "); continue; }
We skip the calculations for the 1, 2, and 3 numbers. They are primes. Note the usage of the equality and conditional or operators. The ==
has a higher precedence than the ||
operator. So we do not need to use parentheses.
int i = (int) Math.sqrt(num);
We are OK if we only try numbers smaller than the square root of a number in question.
while (i > 1) { ... i--; }
This is a while loop. The i
is the calculated square root of the number. We use the decrement operator to decrease the i
by one each loop cycle. When the i is smaller than 1, we terminate the loop. For example, we have number 9. The square root of 9 is 3. We will divide the 9 number by 3 and 2. This is sufficient for our calculation.
if (num % i == 0) { isPrime = false; }
If the remainder division operator returns 0 for any of the i values, then the number in question is not a prime.
In this part of the Java tutorial, we covered Java expressions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论