[VV22] increment operator, jdbc template, react routing, behavior driven development, chatGPT unit test, asking questions, Lombok
??? JAVA CERTIFICATION QUESTION: increment and decrement operators
?? Do Java expressions such as ii[++i] = 0, ii[i++]++, or i = +(i--) give you a headache?
Given the following Calc class:
class Calc {
??Integer i;
??final int[] ii = {0};
??{
????ii[++i] = 0; // line 1
????i--;?????// line 2
????ii[i++]++;??// line 3
????(i--)--;???// line 4
????i = +(i--);?// line 5
??}
}
Which statement is correct? Choose one.
Answer: Compilation fails at line 4 only.
First, line 1 would throw a NullPointerException exception as i is initialized by default with null (and doing ++i with i=null fails).
Still, line 1 does compile, so according to the question, we're good with line 1.
Next, lines 2 and 3 same stories as line 1: it throws an NPE but compiles.
Line 4 would fail to compile.
One of the requirements for using the increment and decrement operators is that the target of such an operator must be in storage that can be updated.
Such a value is sometimes referred to as an L-value, meaning an expression that can be on the left side of an assignment.
In line 4, the expression is (i--)-- and the problem is that while i-- is valid in itself, the resulting expression is simply the numeric value that is contained in the object to which i now refers.
And, in the same way, that you cannot write 3++ (where would you store the result?), you cannot increment or decrement such a simple expression.
Line 5 is syntactically valid, the unary plus operator has no effect.
Conclusion: Compilation fails at line 4 only.
?
???? SPRING CERTIFICATION QUESTION:?When does the JDBC template acquire (and release) a connection ?? - for every method called or once per template?
Answer:
???????????????????????? acquires and releases connection of each called method.
???????????????????????? source code confirms that (look at ????????????????????????#???????????????).
This connection acquiring/releasing strategy is used to minimize connection holding time allowing other clients to utilize free connections.
?? ?? JdbcTemplate source code ???https://lnkd.in/ev-N8ph3
?
QUOTE ???
?
REACT: example of routing ??? in React using the React Router library would be as follows:
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Home from "./components/Home";
import About from "./components/About";
function App() {
? return (
? ? <Router>
? ? ? <Routes>
? ? ? ? <Route exact path="/" element={<Home />} />
? ? ? ? <Route path="/About" element={<About />} />
? ? ? </Routes>
? ? </Router>
? );
}
export default App;
In this example, the Router component is used to wrap the entire application, and the Route component is used to specify which component should be rendered when the user navigates to a certain path. The exact prop is used to make sure that only the component specified for the root path is rendered when the user is on the root path. The path prop is used to specify the path that the component should be rendered for.
In this example, when the user navigates to the root path (/), the Home component will be rendered. When the user navigates to the /about path, the About component will be rendered.
?
领英推荐
SCRUM DEVELOPER CONCEPT: Behavior-Driven Development ?? ????
Behavior Driven Development is a variation ?? / extension of Test-Driven Development methodology, where the main focus is on the following:
1) Behavioral specifications of the product ?? ?? or application (or its features).
2) User ?? ♂? and System ?? Interactions.
Behavior Driven Development uses Ubiquitous ?? language that can be understood by the developers?? ?? and stakeholders?? ??.
When Behavior Driven Development is adapted to a project, the technical ?? nitty-gritty aspects of the requirements and implementation are outlined in a business-oriented ?? ?? language.
?
chatGPT: Generating Unit test with chatGPT ????
ChatGPT is a computer program designed to understand and generate human-like text. It can be used for tasks such as language translation, text generation, and creating chatbots. It's a type of Artificial Intelligence model, more specifically it's a language model, that can understand and generate text.
Developers can leverage their coding skills thanks to chatGPT.
Let's see one use case: the generation of a unit test
I first ask chatGPT to generate a Java bean with this prompt (a prompt is a command to give to chatGPT):
Write a Java bean to store a postal address.Use the Lombok annotations to have constructors, getters, setters, and builders.
chatGPT in reply gave me:
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Builder
@Getter
@Setter
public class Address {
? ? private String street;
? ? private String city;
? ? private String state;
? ? private String zip;
? ? private String country;
}
which is great ???
Now, let's ask to generate the unit test with the following prompt:
Write a Junit test that tests every fields of the Java bean from the snippet below
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Builder
@Getter
@Setter
public class Address {
private String street;
private String city;
private String state;
private String zip;
private String country;
}:
chatGPT replies with:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AddressTest {
? ? @Test
? ? public void testAddressFields() {
? ? ? ? Address address = Address.builder()
? ? ? ? ? ? ? ? .street("123 Main St")
? ? ? ? ? ? ? ? .city("Anytown")
? ? ? ? ? ? ? ? .state("CA")
? ? ? ? ? ? ? ? .zip("12345")
? ? ? ? ? ? ? ? .country("USA")
? ? ? ? ? ? ? ? .build();
? ? ? ? // Test street field
? ? ? ? assertEquals("123 Main St", address.getStreet());
? ? ? ? // Test city field
? ? ? ? assertEquals("Anytown", address.getCity());
? ? ? ? // Test state field
? ? ? ? assertEquals("CA", address.getState());
? ? ? ? // Test zip field
? ? ? ? assertEquals("12345", address.getZip());
? ? ? ? // Test country field
? ? ? ? assertEquals("USA", address.getCountry());
? ? }
}
which is great ???
Then I pasted it to IntelliJ and it worked fine. Great job chatGPT! ??
Check it in the video ??[4min]:
?
SOFT SKILL: Asking questions??
The big secret it's not the person who's speaking that controls the conversation but the person who's asking the right questions. Such a person has a lot more power.
And if you want to really engage with people, if you want to empathize, if you want to connect: create some questions in advance get used to questions about them, about their interests.
?
POLL: Lombok: FOR or AGAINST ????
?