Move articles out of core-java-lang part 2

This commit is contained in:
catalin-burcea
2019-10-06 15:57:30 +03:00
parent 7cbe9ac12c
commit 6b80543360
43 changed files with 49 additions and 62 deletions

View File

@@ -4,3 +4,4 @@
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof)
- [A Guide to Increment and Decrement Unary Operators in Java](https://www.baeldung.com/java-unary-operators)
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators)

View File

@@ -0,0 +1,111 @@
package com.baeldung.compoundoperators;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CompoundOperatorsUnitTest {
@Test
public void whenAssignmentOperatorIsUsed_thenValueIsAssigned() {
int x = 5;
assertEquals(5, x);
}
@Test
public void whenCompoundAssignmentUsed_thenSameAsSimpleAssignment() {
int a = 3, b = 3, c = -2;
a = a * c; // Simple assignment operator
b *= c; // Compound assignment operator
assertEquals(a, b);
}
@Test
public void whenAssignmentOperatorIsUsed_thenValueIsReturned() {
long x = 1;
long y = (x+=2);
assertEquals(3, y);
assertEquals(y, x);
}
@Test
public void whenCompoundOperatorsAreUsed_thenOperationsArePerformedAndAssigned() {
//Simple assignment
int x = 5; //x is 5
//Incrementation
x += 5; //x is 10
assertEquals(10, x);
//Decrementation
x -= 2; //x is 8
assertEquals(8, x);
//Multiplication
x *= 2; //x is 16
assertEquals(16, x);
//Division
x /= 4; //x is 4
assertEquals(4, x);
//Modulus
x %= 3; //x is 1
assertEquals(1, x);
//Binary AND
x &= 4; //x is 0
assertEquals(0, x);
//Binary exclusive OR
x ^= 4; //x is 4
assertEquals(4, x);
//Binary inclusive OR
x |= 8; //x is 12
assertEquals(12, x);
//Binary Left Shift
x <<= 2; //x is 48
assertEquals(48, x);
//Binary Right Shift
x >>= 2; //x is 12
assertEquals(12, x);
//Shift right zero fill
x >>>= 1; //x is 6
assertEquals(6, x);
}
@Test(expected = NullPointerException.class)
public void whenArrayIsNull_thenThrowNullException() {
int[] numbers = null;
//Trying Incrementation
numbers[2] += 5;
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void whenArrayIndexNotCorrect_thenThrowArrayIndexException() {
int[] numbers = {0, 1};
//Trying Incrementation
numbers[2] += 5;
}
@Test
public void whenArrayIndexIsCorrect_thenPerformOperation() {
int[] numbers = {0, 1};
//Incrementation
numbers[1] += 5;
assertEquals(6, numbers[1]);
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.diamond;
package com.baeldung.diamondoperator;
public class Car<T extends Engine> implements Vehicle<T> {

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.diamond;
package com.baeldung.diamondoperator;
public class Diesel implements Engine {

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.diamond;
package com.baeldung.diamondoperator;
public interface Vehicle<T extends Engine> {

View File

@@ -1,46 +0,0 @@
package com.baeldung.java.doublebrace;
import org.junit.Test;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toSet;
import static org.junit.Assert.assertTrue;
public class DoubleBraceUnitTest {
@Test
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
final Set<String> countries = new HashSet<>();
countries.add("India");
countries.add("USSR");
countries.add("USA");
assertTrue(countries.contains("India"));
}
@Test
public void whenInitializeSetWithDoubleBraces_containsElements() {
final Set<String> countries = new HashSet<String>() {
{
add("India");
add("USSR");
add("USA");
}
};
assertTrue(countries.contains("India"));
}
@Test
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
Set<String> countries = Stream.of("India", "USSR", "USA")
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
assertTrue(countries.contains("India"));
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.keyword.test;
package com.baeldung.keyword;
import org.junit.Assert;
import org.junit.jupiter.api.Test;