1564460095
The Java operators is an essential thing in Java programming. They included in every logical, algorithm, methods, class, etc of the Java Programming. Java Operators are special symbol or character that use by specific operation between 1-3 operands then return a result. Here’s an example of one operand.
int result = 100++;
Two operands.
int result = 100 + 200;
Three operands.
int result = 100 + 200 * 2;
Above example describe that “=”, “+” and “*” symbol is an operator and the numbers (100, 200, 300) are operands.
There are a group of operators that classify into the following groups:
We will learn by practice all of those Java operators groups using Netbeans.
Java assignment operators assign the value on its right and the operand on its left.
int age = 100;
Above example describe that “int age” is the operand, “100” is the value, and “=” is the assignment operator. There are more than one Assignment operators, you can learn all of them the examples below.
Open the Netbeans application then create a new Java application project and give it the name AssignmentOperators
. The newly created Netbeans Java application project will open the AssignmentOperators
file automatically which it’s the main and runnable Java application.
Next, we will put all Assignment Operators examples inside the main method.
Simple and most used operators that assign a value on its right and operand on its left. It only uses one character or symbol “=”.
int a = 100;
System.out.println(a);
Output:
100
The operand on its left increase or add by the value on its right then save the result on the operand. It uses two characters or symbols “+=”.
int b = 5;
b += 5;
System.out.println(b);
Output:
10
The operand on its left is decreased or subtract by the value on its right then save the result on the operand. It uses two characters or symbols “-=”.
int c = 100;
c -= 50;
System.out.println(c);
Output:
50
The operand on its left is multiplied by the value on its right then save the result on the operand. It uses two characters or symbols “*=”.
int d = 8;
d *= 50;
System.out.println(d);
Output:
400
The operand on its left is divided by the value on its right then save the result on the operand. It uses two characters or symbols “/=”.
int e = 200;
e /= 10;
System.out.println(e);
Output:
20
The operand on its left is modulus by the value on its right then save the result on the operand. It uses two characters or symbols “%=”.
int f = 20;
f %= 3;
System.out.println(f);
Output:
2
The operand on its left is a binary left shift by the value on its right then save the result on the operand. It uses three characters or symbols “<<=”.
int g = 10;
g <<= 2;
System.out.println(g);
Output:
40
How did it work? The default value of the operand is “10” or “1010” then left a shift by “2” or add two digits of “0” on the right of “1010” then result in the value “101000”. The result “101000” converted to decimal will be “40”.
The operand on its left is a binary right shift by the value on its right then save the result on the operand. It uses three characters or symbols “>>=”.
int h = 13;
h >>= 2;
System.out.println(h);
Output:
3
How did it work? The default value of the operand is “13” or “1101” then right shift by “2” or remove two digits on the right then result in the value “101000”. The result “11” converted to decimal will be “3”. If the value more same or more than binary digits of its operand, the result will always “0”.
The operand on its left is binary AND by the value on its right then save the result on the operand. It uses two characters or symbols “&=”.
int i = 20;
i &= 30;
System.out.println(i);
Output:
20
How did it work? The default value of the operand is “20” or “10100”, binary AND by “30” or “11110” then result in the value “10100”. The result “10100” converted to decimal will be “20”.
The operand on its left is binary OR by the value on its right then save the result on the operand. It uses two characters or symbols “|=”.
int j = 25;
j &= 15;
System.out.println(j);
Output:
9
How did it work? The default value of the operand is “25” or “11001”, binary OR by “15” or “1111” then result in the value “1001”. The result “1001” converted to decimal will be “9”.
The operand on its left is binary XOR by the value on its right then save the result on the operand. It uses two characters or symbols “^=”.
int k = 20;
k ^= 30;
System.out.println(k);
Output:
10
How did it work? The default value of the operand is “20” or “10100”, binary XOR by “30” or “11110” then results in the value “1010”. The result “1010” converted to decimal will be “10”.
The Arithmetic is part of Mathematics. So, Java Arithmetic operators mean use Mathematics Arithmetic operators.
Still, the Netbeans application opened then create a new Java application project and give it the name ArithmeticOperators
. The newly created Netbeans Java application project will open the ArithmeticOperators
file automatically which it’s the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.
Adds two values together or left value add by right value. It uses the character “+”.
int a = 200 + 300;
System.out.println(a);
Output:
500<span style="color: rgba(0, 0, 0, 0.8); font-family: Roboto, sans-serif; font-size: 1.17em; background-color: rgb(255, 255, 255);">
</span>
Subtract the left value by the right value. It uses the character “-”.
int b = 500 - 300;
System.out.println(b);
Output:
200
Multiply two values or left value multiply by right value. It uses the character “*”.
int c = 10 * 10;
System.out.println(c);
Output:
100
Divide two values or left value divide by right value. It uses the character “/”.
int d = 100 / 10;
System.out.println(d);
Output:
10
Modulus two values or left value modulus by the right value and return the result that left from the division. It uses the character “%”.
int e = 35 % 4;
System.out.println(e);
Output:
3
Increase the value of the operand on the left. It uses two characters or symbols “++”.
int f = 20;
++f;
System.out.println(f);
Output:
21
Decrease the value of the operand on the left. It uses two characters or symbols “–”.
int g = 20;
--g;
System.out.println(g);
Output:
19
The Java Unary Operators use only on one operand that performs a various operation such as incrementing/decrementing a value by one step, negating an expression, and inverse a boolean value.
Still, the Netbeans application opened then create a new Java application project and give it the name UnaryOperators
. The newly created Netbeans Java application project will open the UnaryOperators
file automatically which it’s the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.
Java unary plus operator just an indication of the positive value. It uses a character or symbol “+” before its value.
int a = +1;
System.out.println(a);
Output:
1
Java unary minus operator just an indication of the negative value. It uses a character or symbol “-” before its value.
int b = -1;
System.out.println(b);
Output:
-1
Java unary increment operator increases the operand value. It uses two characters or symbols “++” after its operand.
int c = 10;
c++;
System.out.println(c);
Output:
11
Java unary decrement operator decreases the operand value. It uses two characters or symbols “–” after its operand.
int d = 10;
d--;
System.out.println(d);
Output:
9
Java unary logical complement operator inverts the value of boolean. It uses a character or symbol “!” before its operand.
boolean status = true;
System.out.println(!status);
Output:
false
Java equality and relational operators use to determine one operand is greater than, greater than or equal, equal, less than, less than or equal, not equal to another operand.
Still, the Netbeans application opened then create a new Java application project and give it the name EqualityRelationalOperators
. The newly created Netbeans Java application project will open the EqualityRelationalOperators
file automatically which its the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.
Java greater than operator used to compare that left operand is greater than the right operand. It uses a character or symbol “>”.
int a = 20;
int b = 10;
if (a > b)
System.out.println(true);
Output:
true<span style="color: rgba(0, 0, 0, 0.8); font-family: Roboto, sans-serif; font-size: 1.17em; background-color: rgb(255, 255, 255);">
</span>
Java greater than or equal operator use to compare that left operand is greater than or equal right operand. It uses two characters or symbols “>=”.
int c = 20;
int d = 20;
if (c >= d)
System.out.println(true);
Output:
true
Java less than operator used to compare that left operand is less than the right operand. It uses a character or symbol “<”.
int e = 10;
int f = 20;
if (e < f)
System.out.println(true);
Output:
true
Java less than or equal operator use to compare that left operand is less than or equal right operand. It uses two characters or symbols “<=”.
int g = 20;
int h = 20;
if (g <= h)
System.out.println(true);
Output:
true
Java equal operator use to compare that left operand is equal to the right operand. It uses two characters or symbols “==”.
int i = 20;
int j = 20;
if (i == j)
System.out.println(true);
Output:
true
Java bot equal operator uses to compare that left operand is not equal to the right operand. It uses two characters or symbols “!=”.
int k = 20;
int l = 10;
if (k != l)
System.out.println(true);
Output:
true
Java Conditional Operators “&&” and “||” use to join with conditional between two boolean expressions.
Still, the Netbeans application opened then create a new Java application project and give it the name ConditionalOperators
. The newly created Netbeans Java application project will open the ConditionalOperators
file automatically which it’s the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.
Java conditional-AND operator uses to join two boolean expressions which both have the non-null result. This operator uses the characters or symbols “&&”.
int a = 10;
int b = 20;
if((a == 10) && (b != 10)) {
System.out.println(true);
}
Output:
true
Java conditional-OR operator uses to choose two boolean expressions which one of them has the non-null result. This operator uses the characters or symbols “||”.
int c = 10;
int d = 20;
if((c == 10) || (b == 10)) {
System.out.println(true);
}
Output:
true
Java bitwise and bit shift operators perform a binary operation between two integer value. Bitwise operators use the characters or symbols AND “&”, OR “|”, XOR “^”, and compliment “~”. Bit shift operators use the characters or symbols left shift “<<”, right shift “>>”, and zero-fill right shift “>>>”.
Still, the Netbeans application opened then create a new Java application project and give it the name BitwiseAndBitshiftOperators
. The newly created Netbeans Java application project will open the BitwiseAndBitshiftOperators
file automatically which it’s the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.
Java bitwise AND operator perform a binary operation between two integer value with logical AND. It uses a character or symbol “&”.
int a = 13; // 00001101
int b = 12; // 00001100
int c = a & b; // 00001101 & 00001100 = 00001100
System.out.println(c);
Output:
12
Java bitwise OR operator perform a binary operation between two integer value with logical OR. It uses a character or symbol “|”.
int d = 13; // 00001101
int e = 12; // 00001100
int f = d | e; // 00001101 | 00001100 = 00001101
System.out.println(f);
Output:
13
Java bitwise XOR operator performs a binary operation between two integer value with logical XOR. It uses a character or symbol “^”.
int g = 13; // 00001101
int h = 12; // 00001100
int i = g ^ h; // 00001101 ^ 00001100 = 00000001
System.out.println(i);
Output:
1
Java bit left shift operator to perform a binary operation between two integer value by left shift the left value by the amount of the right value. It uses a character or symbol “<<”.
int j = 13; // 00001101
int k = 12;
int l = g << h; // Add 12 digits 0 of 00001101 to the right = 00001101000000000000
System.out.println(l);
Output:
53248
Java Bit Right Shift Operator “>>”
Java bit right shift operator perform a binary operation between two integer value by removing left value by the amount of right value. It uses a character or symbol “>>”.
int m = 8; // 00001000
int n = 2;
int o = m >> n; // Remove 2 digits of 00001000 to the left
System.out.println(o);
Output:
2
Bit Zero Fill Right Shift Operator “>>>”
Java bit zero-fill right shift operator perform a binary operation between two integer value by left shift the left value by the amount of the right value depends on sign extension. It uses a character or symbol “>>>”.
int q = 8; // 1000
int r = 2;
int s = q >>> r; // Remove 2 digits of 00001000 to the left
System.out.println(s);
Output:
2
That it’s, Java Tutorial: Basic Operators Examples. You can find the example source code on our GitHub.
#java
1677668905
Mocking library for TypeScript inspired by http://mockito.org/
mock
) (also abstract classes) #examplespy
) #examplewhen
) via:verify
)reset
, resetCalls
) #example, #examplecapture
) #example'Expected "convertNumberToString(strictEqual(3))" to be called 2 time(s). But has been called 1 time(s).'
)npm install ts-mockito --save-dev
// Creating mock
let mockedFoo:Foo = mock(Foo);
// Getting instance from mock
let foo:Foo = instance(mockedFoo);
// Using instance in source code
foo.getBar(3);
foo.getBar(5);
// Explicit, readable verification
verify(mockedFoo.getBar(3)).called();
verify(mockedFoo.getBar(anything())).called();
// Creating mock
let mockedFoo:Foo = mock(Foo);
// stub method before execution
when(mockedFoo.getBar(3)).thenReturn('three');
// Getting instance
let foo:Foo = instance(mockedFoo);
// prints three
console.log(foo.getBar(3));
// prints null, because "getBar(999)" was not stubbed
console.log(foo.getBar(999));
// Creating mock
let mockedFoo:Foo = mock(Foo);
// stub getter before execution
when(mockedFoo.sampleGetter).thenReturn('three');
// Getting instance
let foo:Foo = instance(mockedFoo);
// prints three
console.log(foo.sampleGetter);
Syntax is the same as with getter values.
Please note, that stubbing properties that don't have getters only works if Proxy object is available (ES6).
// Creating mock
let mockedFoo:Foo = mock(Foo);
// Getting instance
let foo:Foo = instance(mockedFoo);
// Some calls
foo.getBar(1);
foo.getBar(2);
foo.getBar(2);
foo.getBar(3);
// Call count verification
verify(mockedFoo.getBar(1)).once(); // was called with arg === 1 only once
verify(mockedFoo.getBar(2)).twice(); // was called with arg === 2 exactly two times
verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg between 2-3 exactly three times
verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times
verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times
verify(mockedFoo.getBar(anything())).atMost(4); // was called with any argument max four times
verify(mockedFoo.getBar(4)).never(); // was never called with arg === 4
// Creating mock
let mockedFoo:Foo = mock(Foo);
let mockedBar:Bar = mock(Bar);
// Getting instance
let foo:Foo = instance(mockedFoo);
let bar:Bar = instance(mockedBar);
// Some calls
foo.getBar(1);
bar.getFoo(2);
// Call order verification
verify(mockedFoo.getBar(1)).calledBefore(mockedBar.getFoo(2)); // foo.getBar(1) has been called before bar.getFoo(2)
verify(mockedBar.getFoo(2)).calledAfter(mockedFoo.getBar(1)); // bar.getFoo(2) has been called before foo.getBar(1)
verify(mockedFoo.getBar(1)).calledBefore(mockedBar.getFoo(999999)); // throws error (mockedBar.getFoo(999999) has never been called)
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(10)).thenThrow(new Error('fatal error'));
let foo:Foo = instance(mockedFoo);
try {
foo.getBar(10);
} catch (error:Error) {
console.log(error.message); // 'fatal error'
}
You can also stub method with your own implementation
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
when(mockedFoo.sumTwoNumbers(anyNumber(), anyNumber())).thenCall((arg1:number, arg2:number) => {
return arg1 * arg2;
});
// prints '50' because we've changed sum method implementation to multiply!
console.log(foo.sumTwoNumbers(5, 10));
You can also stub method to resolve / reject promise
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.fetchData("a")).thenResolve({id: "a", value: "Hello world"});
when(mockedFoo.fetchData("b")).thenReject(new Error("b does not exist"));
You can reset just mock call counter
// Creating mock
let mockedFoo:Foo = mock(Foo);
// Getting instance
let foo:Foo = instance(mockedFoo);
// Some calls
foo.getBar(1);
foo.getBar(1);
verify(mockedFoo.getBar(1)).twice(); // getBar with arg "1" has been called twice
// Reset mock
resetCalls(mockedFoo);
// Call count verification
verify(mockedFoo.getBar(1)).never(); // has never been called after reset
You can also reset calls of multiple mocks at once resetCalls(firstMock, secondMock, thirdMock)
Or reset mock call counter with all stubs
// Creating mock
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn("one").
// Getting instance
let foo:Foo = instance(mockedFoo);
// Some calls
console.log(foo.getBar(1)); // "one" - as defined in stub
console.log(foo.getBar(1)); // "one" - as defined in stub
verify(mockedFoo.getBar(1)).twice(); // getBar with arg "1" has been called twice
// Reset mock
reset(mockedFoo);
// Call count verification
verify(mockedFoo.getBar(1)).never(); // has never been called after reset
console.log(foo.getBar(1)); // null - previously added stub has been removed
You can also reset multiple mocks at once reset(firstMock, secondMock, thirdMock)
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
// Call method
foo.sumTwoNumbers(1, 2);
// Check first arg captor values
const [firstArg, secondArg] = capture(mockedFoo.sumTwoNumbers).last();
console.log(firstArg); // prints 1
console.log(secondArg); // prints 2
You can also get other calls using first()
, second()
, byCallIndex(3)
and more...
You can set multiple returning values for same matching values
const mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(anyNumber())).thenReturn('one').thenReturn('two').thenReturn('three');
const foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1)); // one
console.log(foo.getBar(1)); // two
console.log(foo.getBar(1)); // three
console.log(foo.getBar(1)); // three - last defined behavior will be repeated infinitely
Another example with specific values
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn('one').thenReturn('another one');
when(mockedFoo.getBar(2)).thenReturn('two');
let foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1)); // one
console.log(foo.getBar(2)); // two
console.log(foo.getBar(1)); // another one
console.log(foo.getBar(1)); // another one - this is last defined behavior for arg '1' so it will be repeated
console.log(foo.getBar(2)); // two
console.log(foo.getBar(2)); // two - this is last defined behavior for arg '2' so it will be repeated
Short notation:
const mockedFoo:Foo = mock(Foo);
// You can specify return values as multiple thenReturn args
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');
const foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1)); // one
console.log(foo.getBar(1)); // two
console.log(foo.getBar(1)); // three
console.log(foo.getBar(1)); // three - last defined behavior will be repeated infinity
Possible errors:
const mockedFoo:Foo = mock(Foo);
// When multiple matchers, matches same result:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(3)).thenReturn('one');
const foo:Foo = instance(mockedFoo);
foo.getBar(3); // MultipleMatchersMatchSameStubError will be thrown, two matchers match same method call
You can mock interfaces too, just instead of passing type to mock
function, set mock
function generic type Mocking interfaces requires Proxy
implementation
let mockedFoo:Foo = mock<FooInterface>(); // instead of mock(FooInterface)
const foo: SampleGeneric<FooInterface> = instance(mockedFoo);
You can mock abstract classes
const mockedFoo: SampleAbstractClass = mock(SampleAbstractClass);
const foo: SampleAbstractClass = instance(mockedFoo);
You can also mock generic classes, but note that generic type is just needed by mock type definition
const mockedFoo: SampleGeneric<SampleInterface> = mock(SampleGeneric);
const foo: SampleGeneric<SampleInterface> = instance(mockedFoo);
You can partially mock an existing instance:
const foo: Foo = new Foo();
const spiedFoo = spy(foo);
when(spiedFoo.getBar(3)).thenReturn('one');
console.log(foo.getBar(3)); // 'one'
console.log(foo.getBaz()); // call to a real method
You can spy on plain objects too:
const foo = { bar: () => 42 };
const spiedFoo = spy(foo);
foo.bar();
console.log(capture(spiedFoo.bar).last()); // [42]
Author: NagRock
Source Code: https://github.com/NagRock/ts-mockito
License: MIT license
1600135200
OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.
In this article, we will be installing OpenJDK on Centos 8.
#tutorials #alternatives #centos #centos 8 #configuration #dnf #frameworks #java #java development kit #java ee #java environment variables #java framework #java jdk #java jre #java platform #java sdk #java se #jdk #jre #open java development kit #open source #openjdk #openjdk 11 #openjdk 8 #openjdk runtime environment
1620458875
According to some surveys, such as JetBrains’s great survey, Java 8 is currently the most used version of Java, despite being a 2014 release.
What you are reading is one in a series of articles titled ‘Going beyond Java 8,’ inspired by the contents of my book, Java for Aliens. These articles will guide you step-by-step through the most important features introduced to the language, starting from version 9. The aim is to make you aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.
In this article, we will talk about the most important new feature introduced with Java 10. Officially called local variable type inference, this feature is better known as the **introduction of the word **var
. Despite the complicated name, it is actually quite a simple feature to use. However, some observations need to be made before we can see the impact that the introduction of the word var
has on other pre-existing characteristics.
#java #java 11 #java 10 #java 12 #var #java 14 #java 13 #java 15 #verbosity
1621103940
Continuing on the Quick Revision of Important Questions for My Interviews. These Are Good Puzzles or Questions Related to Data Structures.
My Article Series on Algorithms and Data Structures in a Sort of ‘Programming Language Agnostic Way’. Few of the Algorithms and Data Structures in C, Few in C++, and Others in Core Java. Assorted Collection for Learning, Revising, Revisiting, Quick Refresh, and a Quick Glance for Interviews. You May Even Include them Directly for Professional or Open Source Efforts. Have Included Explanation Only for Few of These! Hope these turn out to be Really Helpful as per the Author’s Intention.
#java #core java #data structures #dijkstra #core java basics #data structure using java #algorithms and data structures #java code examples #linked list in java #circular linked list
1602637135
Java is not the hardest language to start with. So, it becomes way popular among novice developers joining the ranks of Java coders every single day. If you are reading this blog post, you might be interested in learning Java.
Java is widely used across industry, and especially in the area of Enterprise software, which results in many high paying job opportunities and makes this programming language a common language for newbies. A general promotion of it within colleges and other institutions providing a formal Computer Science education also contributes to its popularity.
However, these are not the only advantages of Java — among other things, it allows you to adopt good practices and makes it way easier to learn other languages in the future. And with no doubt, you can easily learn it if you’re following the right approach. In this post, I am going to share some of them with you.
Beyond all doubt, practice is important and valuable. But, before we get to the advantages of hands-on experience, I want to draw your attention to one essential thing I often tell my students.
New programmers who are just learning and start implementing things, without being supervised, often end up adapting bad practices. To avoid that, especially when you are making your first steps in programming, I recommend looking for a person who will supervise you and teach you. A strong mentorship with someone engaged in a serious project, as well as communication within the community in the form of sharing code and asking for feedback, is worth the effort. Similarly, when you are applying for your first job, you want to be looking for a company with a strong team and a good leader who would be keen on investing into your learning.
Now, let’s return to practical experience. Learning by doing is different from learning by passively consuming the information. To make sure we can use all the newly acquired technology, we should put our skills to test and write tons of code. The benefits of hands-on experience are almost endless.
By practicing, you get a clear understanding of what programming is. Consequently, you start doing better with each new hands-on task, complete it faster, and thus become more productive.
Even if you are not working on real-world projects yet, it’s important to get used to having deadlines. They are inextricably linked to the programming process. My recommendation is to set up your own deadlines while practicing stage and follow them as closely as possible.
#java #learn java #java code #learn java in easy way #learn java course #learn java development