Why in programming are the braces not arranged next to the text they enclose? [closed] - coding-style

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
(12193170: not the same question (or not the right answers for that matter...))
So basically why this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
And not this:
public class HelloWorld
{public static void main(String[] args)
{System.out.println("Hello World");}}`

The main reason for using a particular formatting ( position braces and indentation) is for visibility.
Your example is too simple and you could argue that both versions are easy to read (although first version look simpler to me). When you have hundreds of lines of code, you really appreciate having your code properly formatted.
But you could use other formatting styles if you prefer, for example:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

Related

How to deal with xml and json as request in Java 17 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
#XmlRootElement(name="addressRequest")
#XmlAccessorType(XmlAccessType.FIELD)
#JsonTypeName("addressRequest")
#JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME)
public class ValidateAddressRequest {
private boolean checkInLocalDb = false;
private boolean saveInLocalDb = true;
private String vendor;
private String consigneeName;
private String buildingName;
}
public class AddressRestController {
#PostMapping (path = "/validateAddress", consumes = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public ValidateResponse validateAddress(#RequestBody #Valid ValidateRequest validateRequest){
//some code
}
}
So the above code is working in Java 1.8
But when I migrated the code to Java 17 and Spring boot 3.0.2 json the request is also working but when I post a XML request it is giving JSON parse error: Could not resolve subtype.
After many efforts i think #JsonTypeInfo is the issue , but don't have any idea how to change that so that both(xml & json) can work.

Load data from the database to mat-table when click Next page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using angular 7 with spring boot, and i am use mat-table, i don't want load for example all the data from database in one load, i want to load data just when click on next page in mat-table.
and This image for my mat-table.
how i can do that in angular 7 and spring boot together, if anybody have solution please give me it, thanks.
In JpaRepository of Spring-data-jpathere is a Pageable concept, in your repo implement JpaRepository instead of CrudRepository (It internally extends PagingAndSortingRepository)
You could do something like this,
Repo
public class FooRepository extends JpaRepository<FooClass, Long> {
}
Service
#Service
public class FooService {
#Autowired
private FooRepository fooRepo;
public Page<FooClass> fetchPagedFoo(Integer page, Integer size) {
return fooRepo.findAll(PageRequest.of(page, size));
}
}
Controller
#RestController
public class FooController {
#Autowired
private FooService fooService;
#GetMapping("/foo")
public ResponseEntity getPagedFoo(
#RequestParam(value = "pageIndex", defaultValue ="0") Integer page,
#RequestParam(value = "size", defaultValue = "10") Integer size) {
return ResponseEntity.ok(fooService.fetchPagedFoo(page, size));
}
}
In above if you pass 0, 10 then first page 10 rows will be returned, Page will return total number of data as well with this you could do Pagination.

How to use 'make_ptr<T>()' while T is a class with private construction? [duplicate]

This question already has answers here:
How do I call ::std::make_shared on a class with only protected or private constructors?
(19 answers)
Closed 7 years ago.
I was working with singleton pattern and shared_ptr.I was trying to make the code like this:
class A{
private:
static std::shared_ptr<A> instance;
A();
public:
static std::shared_ptr<A> creatInstance();
};
std::shared_ptr<A> A::creatInstance(){
if(!instance){
instance=std::make_shared<A>();
}
return instance;
}
But I got a compiler error.
Any thoughts?
I tried to make make_shared be a friend function of class A,but it didn't work.
friend shared_ptr<A> make_shared<A>();
It seems that std::make_shared< A > requires public constructor of A.
Here is a few solutions of this problem - How do I call ::std::make_shared on a class with only protected or private constructors?

In Java compiler,The print in System.out.print can be defined as identifier or Keyword? [duplicate]

This question already has answers here:
In Java compiler,which type can be defined as identifier ( ID ) or Keyword (reserved word)?
(2 answers)
Closed 7 years ago.
I have studied about java which have listed 50 Java keywords. There is a homework of Lex, the goal is to recognize the word is keywords, IDs, symbols, operators. But there is one more little problem is the code below, is print in System.out.print() an ID or keyword?
public class HelloWorld {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c;
int a = 5;
c = add(a, 10);
if (c > 10)
System.out.print("c = " + -c);
else
System.out.print(c);
System.out.print("Hello World");
}
}
print is the name of a method in the java.io.PrintStream class, hence an ID. Keywords are those which generally turn blue or another colour when you type them in most IDEs.
For more information: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
System is a final class from java.lang package.
out is the reference of PrintStream class and a static member of System class.
print is a method of PrintStream class.
//the System class belongs to java.lang package
class System {
public static final PrintStream out;
//...
}
//the Prinstream class belongs to java.io package
class PrintStream{
public void print();
//...
}
Have a look at this too.. https://docs.oracle.com/javase/7/docs/api/java/lang/System.html

Regarding Dependency Injection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
interface Car {
public void tyres();
}
class BMW implements Car {
public void tyres() {
System.out.println("BMW");
}
}
class HMW implements Car {
public void tyres() {
System.out.println("HMW");
}
}
public class DependencyInjection {
private Car car;
public void draw() {
this.car.tyres();
}
public void setCar(Car car) {
this.car = car;
}
public static void tyres1(Car c) {
c.tyres();
}
public static void main(String[] args) {
Car car = new BMW();
tyres1(car);//point 1
DependencyInjection dep=new DependencyInjection();
dep.setCar(car);//point2
dep.draw();
}
}
I want some clarification what is advantage we have creating dependency injection at point 1 and point2.Please explain me in detail as i am new to spring???
This is not a Spring specific design principle, and it's hard to grasp in the snippet you've provided which does not have notable separated pieces, but becomes more helpful as systems grow.
By removing the reference to a concrete class you eliminate "type coupling" which means that the internal class can remain "closed": it won't have to change if the implementation changes of the involved class and the client code can use different ways to adapt as needed without the inner class needing to change or be aware of the implementation. This also helps ensure that the method body within the class is focusing on its role and its specific job rather than being getting tangled in with any other implementation (which helps in clearly defining what should be considered part of an API).
You should also read about the concept of SOLID classes:
http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
The most immediate benefit in my experience is that it also allows for far more complete, isolated unit tests which is very important if that is part of your development process (which is recommended also as systems grow).
Actually what Dependency Injection does that part is done by your code:
Car car = new BMW();
Dependency Injection feature helps you to mentioned dependency as configuration and Framework actually does the rest Injection of Object to references.

Resources