12

I just came across a C# question tagged with . I was curious, so I went to the tag, and found a bunch of questions from different languages, including C#, Java, C++, php, etc. But the tag wiki only talks about C++, as if this is specific to that language. I'm wondering what the rationale behind that is, or if the wiki needs to be edited to include other languages.

Here's an excerpt of the tag wiki to demonstrate what I'm talking about.

Use this tag for questions about dynamic allocation in C++ and questions about overloading operator new. The tag may also be relevant for questions about constructing objects in existing memory.

Edit: A Google search on new operator brings up pretty much exclusively C++ results. If you append C# onto the search terms, you get the Microsoft documentation for the new Operator. Searching with Java instead gives you the Java documentation on creating objects. Note that it does refer to new as both a key word and an operator. Javascript gives you the MDN web docs for the new operator. I searched some other languages such as php, however I just got programming-related SO questions with "new operator" in the titles.

So I think there is some truth to the fact that it might exist in different languages. My results are by no means extensive, and I'm sure some other people will be able to point me towards some other language examples.

5
  • 2
    Programmers ask questions in proportion to how much trouble the operator causes in their programs. It is about 8.5 times more trouble in [c++] than in [c#], [java] and [javascript], judging from the tag distribution. Sounds about right, heap corruption is a nasty problem. That inevitably gives the wiki a c++ lisp. Commented Dec 1, 2017 at 8:08
  • 2
    It only references c++ probably because a c++ developer wrote it. You can submit an edit (? maybe not at your rep now) that expands the definition to include other languages that uses the new object initialization pattern.
    – user1228
    Commented Dec 1, 2017 at 18:44
  • @Will Suggested edits can be submitted by anyone, iirc.
    – wizzwizz4
    Commented Dec 2, 2017 at 13:24
  • 1
    @Will "You can submit an edit (? maybe not at your rep now)"
    – wizzwizz4
    Commented Dec 4, 2017 at 18:21
  • Submitted an edit and had it approved :) Tag wiki still needs more work imo, but maybe by someone a bit more capable than I
    – Lauraducky
    Commented Dec 4, 2017 at 22:00

2 Answers 2

7

We should edit the wiki to include the other languages. The new keyword in those languages looks like the new keyword in C++, because C++ is a popular language that influenced other languages.

C++ new is a keyword. I can't declare a variable int new;, just as I can't declare int using; or int while; in C++. There is an operator keyword that overloads operators, as in operator + or operator <<. C++ also uses operator to overload new, as operator new. So we also say that C++ new is an operator. The new keyword and the new operator are the same thing; there is no difference.

Other languages, like Java, may simplify new. Java has no overloading of new, so there is no operator new syntax to overload it. Java new is still an operator, because the operator looks the same in Java and C++:

Thing *x = new Thing(y);  // C++
Thing x = new Thing(y);   // Java

probably has more C++ questions than Java questions, because C++ has more complications (like overloading and placement new) that don't exist in Java. I see 7 highlighted questions under the tag info; 5 are C++ but 2 are other languages:

  1. Why should C++ programmers minimize use of 'new'?
  2. Do the parentheses after the type name make a difference with new?
  3. Passing arguments to C# generic new() of templated type
  4. Using "Object.create" instead of "new"
  5. Why does the use of 'new' cause memory leaks?
  6. C++ new int[0] -- will it allocate memory?
  7. Why would one replace default new and delete operators?

All 7 questions have language tags, so we know if the question is about C++ or some other language. This allows C++ to share with other languages. JavaScript new also sets the prototype of the object, which raises questions that don't exist in C++ or Java. Stack Overflow isn't only for C++.

Perl also had the new operator:

use Math::BigInt;
my $bn = new Math::BigInt('42');

This new isn't a keyword, but it looks like the operator in C++. This syntax has some problems and Perl now discourages it, but people do ask questions about it.

0
-2

At a bare minimum, in Java, new is a keyword, not an operator. I have no experience in the other languages, so I won't speak to them.

This does raise the question of the validity of the tag at all, really. But for now I don't see adding more languages to this list as a "good" idea.

5
  • new being a keyword, not an operator in Java is at the very least debatable The new keyword is a Java operator and includes new and the new operator
    – Oleg
    Commented Dec 1, 2017 at 8:38
  • 3
    The authoritative source about the Java Language, the Java Language Specification, does not include new in its list of operators. Makoto's premise is therefore entirely correct. However, I am doubtful the argument generalizes to other languages. For instance, the EcmaScript spec calls new an operator.
    – meriton
    Commented Dec 2, 2017 at 0:16
  • @meriton The JLS contradicts itself in chapters 3 and 15 regarding what is and isn't an operator. Chapter 15 not having an explicit reference to new as an operator doesn't mean that it's not an operator. Based on everything else it's as much of an operator as other operators discussed in that chapter.
    – Oleg
    Commented Dec 2, 2017 at 5:49
  • 2
    @Oleg: Chapter 3 is crystal clear, and I fail to see what part of chapter 15 "contradicts" it. If you mean to say that the mere presence in chapter 15 implies new to be an operator, the same reasoning would apply to method invocation expressions ... are you seriously calling () an operator?
    – meriton
    Commented Dec 2, 2017 at 23:25
  • @meriton Chapter 3 Operator table doesn't include Operator instanceof that's a contradiction. Yes method invocation can also be seen as an operator.
    – Oleg
    Commented Dec 3, 2017 at 2:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .