Skip to content

Commit

Permalink
cppToJava/arrays, operators: tweak text
Browse files Browse the repository at this point in the history
  • Loading branch information
damithc committed Aug 7, 2018
1 parent 8b748b7 commit de82361
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
55 changes: 43 additions & 12 deletions cppToJava/dataTypes/arrays/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,68 @@
Arrays are indicated using square brackets (`[]`). To create the array itself, you have to use the `new` operator. Here are some example array declarations:
```java
int[] counts;
counts = new int[4]; // create an int array of size 4

int size = 5;
double[] values;
counts = new int[4];
values = new double[size];
int[] scores = new int[4];
double[] prices = new double[size];
values = new double[size]; //use a variable for the size

double[] prices = new double[size]; // declare and create at the same time
```
<blockquote>
Alternatively, you can use the shortcut syntax to create and initialize an array:

```java
int[] values = {1, 2, 3, 4, 5, 6};

int[] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
```
{{ oracle }}
</blockquote>

{{ similar }} The `[]` operator selects elements from an array. Array elements indices start from 0 %%i.e., the index of the first element is 0, not 1%%.
{{ similar }} The `[]` operator selects elements from an array. Array elements <tooltip content="i.e., the index of the first element is 0, not 1">indices start from 0</tooltip>.
```java
int[] counts = new int[4];

System.out.println("The first element is " + counts[0]);

counts[0] = 7;
counts[0] = 7; // set the element at index 0 to be 7
counts[1] = counts[0] * 2;
counts[2]++;
counts[2]++; // increment value at index 2
```

{{ different }} A Java array is aware of its size.
{{ different }} A Java array is aware of its size. A Java array prevents a programmer from indexing the array out of bounds. If the index is negative or not present in the array, the result is an error named `ArrayIndexOutOfBoundsException`.
```java
int[] scores = new int[4];
System.out.println(scores.length)
System.out.println(scores.length) // prints 4
scores[5] = 0; // causes an exception
```
{{ icon_output }}
```
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.main(Main.java:6)
```

{{ different }} A Java array prevents a programmer from indexing the array out of bounds. If the index is negative or not present in the array, the result is an error named `ArrayIndexOutOfBoundsException`.

{{ similar }} It is also possible to create arrays of more than one dimension:
<blockquote>

```java
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};

System.out.println(names[0][0] + names[1][0]); // Mr. Smith
System.out.println(names[0][2] + names[1][1]); // Ms. Jones
```
{{ oracle }}
</blockquote>

##### Passing arguments to a program

Expand All @@ -61,8 +94,6 @@ You can run this program (after compiling it first) from the command line by typ

`abc`

==//todo mention array initializing using literals==

</div>

<div id="extras">
Expand Down
46 changes: 23 additions & 23 deletions cppToJava/dataTypes/operators/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
Java has the following **arithmetic operators**:
Operator | Description | Examples
---------|-------------|---------
`+` | Additive operator |
`-` | Subtraction operator |
`*` | Multiplication operator |
`/` | Division operator |
`%` | Remainder operator |
`+` | Additive operator | `2 + 3` %%{{ icon_output_right }}%% `5`
`-` | Subtraction operator | `4 - 1` %%{{ icon_output_right }}%% `3`
`*` | Multiplication operator | `2 * 3` %%{{ icon_output_right }}%% `6`
`/` | Division operator | ==`5 / 2` %%{{ icon_output_right }}%% `2`== but ==`5.0 / 2` %%{{ icon_output_right }}%% `2.5`==
`%` | Remainder operator | `5 % 2` %%{{ icon_output_right }}%% `1`

The following program uses some operators as part of an expression `hour * 60 + minute`:

Expand All @@ -42,33 +42,33 @@ Number of minutes since midnight: 719

Operator | Description {{ oracle }} | example
---------|-------------|--------
`+` | Unary plus operator; indicates positive value (numbers are positive without this, however) |
`-` | Unary minus operator; negates an expression |
`++` | Increment operator; increments a value by 1 | i++
`--` | Decrement operator; decrements a value by 1 | i--
`!` | Logical complement operator; inverts the value of a boolean |
`+` | Unary plus operator; indicates positive value<br>(numbers are positive without this, however) | `x = 5; y = +x` %%{{ icon_output_right }}%% `y` is `5`
`-` | Unary minus operator; negates an expression | `x = 5; y = -x` %%{{ icon_output_right }}%% `y` is `-5`
`++` | Increment operator; increments a value by 1 | `i = 5; i++` %%{{ icon_output_right }}%% `i` is `6`
`--` | Decrement operator; decrements a value by 1 | `i = 5; i--` %%{{ icon_output_right }}%% `i` is `4`
`!` | Logical complement operator; inverts the value of a boolean | `foo = true; bar = !foo` %%{{ icon_output_right }}%% `bar` is `false`


**Relational operators** are used to check conditions like whether two values are equal, or whether one is greater than the other. The following expressions show how they are used:

Operator | Description | example
---------|-------------|--------
`x == y` | x is equal to y |
`x != y` | x is not equal to y |
`x > y` | x is greater than y |
`x < y` | x is less than y |
`x >= y` | x is greater than or equal to y |
`x <= y` | x is less than or equal to y |
Operator | Description | example %%{{ icon_output_right }}%% `true`| example %%{{ icon_output_right }}%% `false`
---------|-------------|---------------------------------------|----------------------------------------
`x == y` | `x` is equal to `y` | `5 == 5` | `5 == 6`
`x != y` | `x` is not equal to `y` |`5 != 6` | `5 != 5`
`x > y` | `x` is greater than `y` | `7 > 6` | `5 > 6`
`x < y` | `x` is less than `y` | `5 < 6` | `7 < 6`
`x >= y` | `x` is greater than or equal to `y` | `5 >= 5` | `4 >= 5`
`x <= y` | `x` is less than or equal to `y` | `4 <= 5` | `6 <= 5`

The result of a relational operator is a boolean value.

Java has three **conditional operators** that are used to operate on boolean values.

Operator | Description | example
---------|-------------|--------
`&&` | |
`||` | |
`!` | |
Operator | Description | example %%{{ icon_output_right }}%% `true`| example %%{{ icon_output_right }}%% `false`
---------|-------------|-------------------------------------------|--------------------------------------------
`&&` | and | `true && true` %%{{ icon_output_right }}%% `true` | `true && false` %%{{ icon_output_right }}%% `false`
`||` | or | `true || false` %%{{ icon_output_right }}%% `true` | `false || false` %%{{ icon_output_right }}%% `false`
`!` | not | `not false` | `not true`


</div>
Expand Down

0 comments on commit de82361

Please sign in to comment.