Questions tagged [java]

For questions about Java's design, or languages which are closely related to Java

Filter by
Sorted by
Tagged with
2 votes
1 answer
245 views

What if all static methods in Java or C# could be extension methods just by default?

C# 3.0 introduced extension methods to "enhance" behavior of existing types without interface bloating to avoid modifying/breaking existing interfaces. This was the first time I learned of ...
terrorrussia-keeps-killing's user avatar
1 vote
3 answers
309 views

Is there any way a Java-like language could implement immutable primitive arrays without incurring performance penalties?

I asked: What prevents Java from having immutable primitive arrays? a while back and got an answer: Because immutable primitive arrays would typically require checking some immutable flag every time a ...
user16217248's user avatar
  • 7,535
16 votes
2 answers
2k views

How do lexers/parsers distinguish between nested generics and bitshifts?

The lexical grammar of Java has a special case for the the > character. Normally, tokens are formed based on the longest-match rule, so that an input string of <...
kaya3's user avatar
  • 17.3k
9 votes
1 answer
2k views

Why can you implement a Monoid type in Java or C#, but not Monad or Functor?

Haskell and some other functional languages have Monad and Monoid types (and Semigroup, Functor, Applicative, and many others), and lists, trees, Maybe, and other types subtype all or some of these. ...
Michael Homer's user avatar
  • 11.7k
14 votes
4 answers
3k views

When would a Java style enum be better than a C++ style enum, and vice versa?

In Java, enum values behave like objects (since they are basically syntactic sugar for objects) and they can have their own class methods. In C++, however, enums are essentially an alias for an ...
FireTheLost's user avatar
  • 1,583
6 votes
3 answers
797 views

Why might a language avoid reallocation?

In Java, the size of arrays are immutable. However, to my understanding, they are still allocated on the heap, because Java allocates almost everything on the heap. Even then, Java arrays are still ...
user16217248's user avatar
  • 7,535
8 votes
1 answer
94 views

How can it be determined that a cast or type test is unchecked?

In languages like Java that use type erasure for generics, some casts are considered unchecked. One example is casting a value of Object to ...
MI3Guy's user avatar
  • 181
7 votes
1 answer
148 views

What are the advantages and disadvantages of package declarations?

Java-family languages often use a package declaration at the top of a source file, to group source files into modules/packages independently of their file system ...
Ginger's user avatar
  • 2,579
11 votes
5 answers
391 views

What are the advantages of having first-class functions versus having only anonymous class with fancy sugars?

In Java, there are "SAM-interfaces" which are used as "types" for lambda expressions. In Haskell, there are dedicated types for functions. In Rust, there are no types for functions,...
ice1000's user avatar
  • 2,384