-9

Supposed that, I have a class named of RequestType. In Java, code tends not to have any check for a new object being a null reference:

RequestType request = new RequestType();
// if (request == null)   // missing

But C++ code tends to check the allocation:

RequestType* request = new RequestType();
if (nullptr == request)      // check

Why do we need to check whether requestType is nullptr in C++, but can just use it without such a check in Java?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
JosanSun
  • 339
  • 2
  • 12
  • 4
    Uhm, I don't really know how to answer this... because they are completely different languages? C++ gives you a lot more control over the memory than Java does, they are just different choices in how the languages are designed, better suited for different uses. That control also means you have to be more careful. Be careful, just because some of the syntax and keywords are similar, they can mean completely different things. With your example code you actually don't need the `nullptr` check in C++ anyway, as it will throw an exception if it fails, rather than returning `nullptr`. – BoBTFish Sep 18 '18 at 07:03
  • 3
    Because in Java, the `new` operator can never ever return `null`. – Andreas Sep 18 '18 at 07:03
  • https://stackoverflow.com/questions/550451/will-new-return-null-in-any-case – Biffen Sep 18 '18 at 07:06
  • 5
    You don't. If the new operator is unable to allocate memory, it will throw an exception in both languages. – Raedwald Sep 18 '18 at 07:06
  • Possible duplicate of [Will new return NULL in any case?](https://stackoverflow.com/questions/550451/will-new-return-null-in-any-case) – Alan Birtles Sep 18 '18 at 07:17
  • 4
    your premise is wrong in two ways. First in C++ after `RequestType* requestType = new RequestType();` the pointer `requestType` cannot be `null`. Second in Java as well as in C++ you do need to check for null (for pointers in C++ and for references in java) when you dont know if there is a valid object. And last but not least, C++ and Java are two completely different languages, their similarities in syntax are just to confuse ppl to believe they would have similarities beyond that – 463035818_is_not_an_ai Sep 18 '18 at 07:28
  • 1
    ***the pointer requestType cannot be null*** Maybe @JosanSun is using an old compiler. I remember VS used to return NULL on allocation failure a 2 decades ago.. https://stackoverflow.com/questions/550451/will-new-return-null-in-any-case – drescherjm Sep 18 '18 at 08:12
  • 2
    @drescherjm - All C++ standards require a new expression to throw an exception on failure, so it cannot yield NULL. Pre-standard C++ (e.g. while the standard was in evolving draft before being ratified in 1998) did not have that requirement. That's why older compilers did support that behaviour (VC6 was a bit controversial, as it was released in 1998, and notable for incomplete support of the standard). There is also a `new (std::nothrow)` variant which CAN yield a null pointer, but that wasn't part of the question. – Peter Sep 18 '18 at 09:51
  • @drescherjm Yeah. I learned C++ using VC6.0 in 2009. Then I usally new an Object and then check whether the pointer is NULL or not? Even, I am using VS2015, I still keep the check. Recently, I learned JAVA. I found no one did that check. Because, the check makes my project ugly. So I also want to drop the check. Now, I know the check is useless. ^_^ – JosanSun Sep 18 '18 at 16:14

2 Answers2

2

Your premise is mistaken (perhaps informed by mediocre samples).

In both languages, the new operator will either succeed or throw an exception (java.​lang.​OutOfMemoryError or std::​bad_alloc, respectively). So there's never a need to check a newly-allocated object like that. (Note here that I'm talking about Standard C++ - some ancient pre-Standard compilers would return null instead of throwing).

When a function receives an argument outside of its control, a defensive programmer will normally check Java references and C++ pointers, both of which can be null. It's less common to be so defensive with C++ references, as the implicit contract is that we don't create null references in C++.


Summary

  • A newly allocated object can never be null in Java:

    Request request = new Request();
    // request cannot be null
    

    Nor in C++:

    Request* request = new Request();
    // request cannot be null
    
  • A function argument or return value might be null in Java:

    void processRequest(Request request) {
         // request might be null
         String body = request.body();
         // body might be null
    

    And in C++:

    void processRequest(const Request *request) {
         // request might be null
         const char *body = request->body();
         // body might be null
    
    void processRequest(const Request& request) {
         // &request assumed non-null
         std::string body = request.body();
         // body can't be null (it's not a pointer)
    
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
-3

In Java, following statement creates object explicitly and it will never be null until or unless it is a default constructor.

RequestType requestType = new RequestType();

However, if you are receiving this object as a method parameter, or in a return statement or your constructor is doing some logic while creating this object, then there is a possibility, it can be null.

So, In java as well, You need to check if your object is null or not.

Incredible
  • 341
  • 2
  • 4
  • 17
  • Your answer is confusing - can you improve it? When you create an object with `new`, you will never get `null` back in Java, so it's not necessary to check if the variable is `null` immediately after doing `new SomeClass()`. (What do you mean with "until or unless it is a default constructor"?). – Jesper Sep 18 '18 at 07:13
  • until or unless it is "not" a default constructor. Sorry, for typo.. . . In case of default constructor it will always return an object. . . However, If in constructor you have added logic to create this object and it fails to execute or throw exception, then new RequestType() will return null. – Incredible Sep 18 '18 at 07:46
  • 2
    *However, If in constructor you have added logic to create this object and it fails to execute or throw exception* - Then it will throw an exception and the execution will stop. Unless you have a try-catch, but that's another scenario. – BackSlash Sep 18 '18 at 07:48
  • @Incredible That is not true - `new` will never return `null`, no matter how the constructor is implemented or whether it somehow fails to execute. It always either returns a reference or throws an exception, but never returns `null`. – Jesper Sep 18 '18 at 08:07