Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
114241e8
Commit
114241e8
authored
3 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Cosmetics
parent
7b0b3a65
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/statements.xml
+11
-13
11 additions, 13 deletions
Doc/Sd1/statements.xml
with
11 additions
and
13 deletions
Doc/Sd1/statements.xml
+
11
−
13
View file @
114241e8
...
...
@@ -529,13 +529,6 @@ Found: <emphasis role="red">int</emphasis></screen>
<para>
In math
<quote>
=
</quote>
denotes the equality of
objects
<abbrev>
e.g.
</abbrev>
values, sets, functions and
so on.
</para>
<para>
More formally the expression
<code
language=
"java"
>
count = 4
</code>
is of type
<code
language=
"java"
>
int
</code>
evaluating to 4 (surprise!).
However an
<code
language=
"java"
>
if (...)
</code>
operates
on
<code
language=
"java"
>
boolean
</code>
values only. We
thus must not supply expressions of any other type.
</para>
</glossdef>
</glossentry>
...
...
@@ -559,15 +552,19 @@ Found: <emphasis role="red">int</emphasis></screen>
</itemizedlist>
<para>
In particular an expression like
<code
language=
"java"
>
count == 4
</code>
is of type boolean
:
E
ither
<code
language=
"java"
>
true
</code>
or
<code
language=
"java"
>
count == 4
</code>
is of type boolean
being
e
ither
<code
language=
"java"
>
true
</code>
or
<code
language=
"java"
>
false
</code>
.
</para>
</glossdef>
</glossentry>
</glosslist>
<para>
Thus
<code
language=
"java"
>
count = 4
</code>
is an
expression evaluating to 4. So the code in question may be
<para>
More formally the expression
<code
language=
"java"
>
count =
4
</code>
is of type
<code
language=
"java"
>
int
</code>
evaluating
to 4 (surprise!). However an
<code
language=
"java"
>
if
(...)
</code>
operates on
<code
language=
"java"
>
boolean
</code>
values only and
<code
language=
"java"
>
if (4)
</code>
thus does
not make sense at all. The code in question may therefore be
re-written as:
</para>
<programlisting
language=
"java"
>
int count = 1;
...
...
@@ -579,7 +576,7 @@ if (countAssignment) { <emphasis role="red">// Error: An int is not a boolean!</
}
</programlisting>
<para>
Since the assignment operator is being evaluated from
right to left we do not need braces:
</para>
right to left we
actually
do not need braces:
</para>
<programlisting
language=
"java"
>
...
int countAssignment = count = 4; // Assigning expression count = 4 to variable countAssignment
...
...
@@ -596,7 +593,8 @@ int countAssignment = count = 4; // Assigning expression count = 4 to variable c
final boolean test = (count == 4); // Now using "==" (comparison) in favour of "=" (assignment)
System.out.println("test=" + test);
</programlisting>
<para>
Again we may omit braces here:
</para>
<para>
Again we may omit braces here due to operator priority
rules:
</para>
<programlisting
language=
"java"
>
...
final boolean test = count == 4; // Now using "==" (comparison) in favour of "=" (assignment)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment