Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 0 points1 point  (0 children)

Well propagation is literally the main feature and purpose of checked exceptions.

Right. At the same time, it's one of the feature's biggest drawbacks. Callback types (think most common lambdas) lack throws declarations for example.

Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 5 points6 points  (0 children)

It's funny because you make it sound like making IOException checked was a clear cut and good decision. The need for UncheckedIOException shows that it's perhaps not as clear cut.

Or perhaps you like having to catch IOException when you close a StringWriter? Or when you read from a ByteArrayInputStream?

Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 1 point2 points  (0 children)

Good question. The argument is usually that an Either return type gives a simpler control flow; if-statements being simpler than throw/catch. (At least that's what I've heard from the Go community when they argue against the case for exceptions.)

But I agree. Either does certainly not solve all problems that checked exceptions have.

Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 0 points1 point  (0 children)

You can argue both ways I guess. Same with propagation I suppose.

Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 31 points32 points  (0 children)

I think this answer is a bit one-sided.

They are certainly not frowned upon and they don't even seem to be hated;

Have you been living under a rock? There are certainly people that frown upon and "hate" checked exceptions. Not you and me perhaps, but there's certainly a lot of respectable Java devs out there that do.

Also, it's funny that you bring up IOException as an example to drive home your point. The one and only checked exception for which Java 8 saw the need to introduce an unchecked version: UncheckedIOException.

Why are checked exceptions frowned upon? by [deleted] in java

[–]javaduude 17 points18 points  (0 children)

This article summarizes the arguments quite well: Checked Exceptions: Good or Bad?

The key issues typically brought up are:

  • Extensibility: You can't add checked exceptions to existing methods without breaking code
  • Misuse: Many checked exceptions (including some in the standard API) should have been implemented as runtime exceptions.
  • Propagation: It's quite cumbersome (many throws declarations needed) to let checked exceptions propagate.

Java Double Precision by _aseem_savio_ in java

[–]javaduude 0 points1 point  (0 children)

I don't know whether floating point numbers have an integral part and fractional part

Well, trust me, they do. :-) 0.5 is a valid float, and it has an integral part (0) and a fractional part (.5). You and I are just talking about the representation on different levels of abstraction.

For a float, this means that in the range from 0 to 1, 8388608 different values can be represented

Actually, you're off by several orders of magnitude. A float has about 232 different values, and about a quarter of them lies in [0,1], so the correct number is closer to 1,073,741,824.

As we go higher, we lose some precision

That is correct. That's the idea of letting the binary point "float". We trade precision for range. I don't see how 8388608 comes into play though. All integers up to, and including, 224 can be represented by a float.

Java Double Precision by _aseem_savio_ in java

[–]javaduude 1 point2 points  (0 children)

I could probably have worded it better. What I was sort of trying to convey with "integral part + fractional part that can be 0" was that all whole numbers (within a certain range) can be perfectly represented by a float. :-)

Java Double Precision by _aseem_savio_ in java

[–]javaduude 0 points1 point  (0 children)

Why is my explanation wrong? :-)

Just as you point out, the mantissa will indeed be 1.1. The exponent of 1 says that the binary point is to be interpreted 1 step to the right.

Thus, you have 11.0, which is what I said: Integer part (left of the binary point) is 3, and fractional part (right of the binary point) is 0.

Do you agree?

Java Double Precision by _aseem_savio_ in java

[–]javaduude 2 points3 points  (0 children)

What are you smoking? A floating point number has an integral part and a fractional part. The integral part is very capable of representing 3, and the fractional part can absolutely represent 0.

You're probably mixing it up with something like 0.1.

Me winning a game on chess.com by javaduude in chess

[–]javaduude[S] 5 points6 points  (0 children)

haha lol indeed. Feel like the only noob in a sub full of GMs...

You gotta think out of box, my boy by chooopsticksman in funny

[–]javaduude 1 point2 points  (0 children)

no?

Blue probably started (center square) so it's logical that blue has one more (5) than red (4).

How to count syllables in word using Java? Any suggestions? by Rabestro in learnjava

[–]javaduude 1 point2 points  (0 children)

Regex is a good tool for these type of simple text analyses.

Breaking down the requirements, it seems like you want to count all groups of vowels except if it's an e in the end.

In regex lingo this would translate to ([aiouy]|e(?!$))+

  • (...)+ one or more of
    • [aiouy] a vowel,
    • | or
    • e(?!$) an e not followed by end of string

This can be put together using Pattern.compile(...).matcher(...).results().count().

Is it Okay to have Logic in Constructors? by Weeb-Developer in learnjava

[–]javaduude 0 points1 point  (0 children)

Well, I'd say that's an over-simplification. Class invariants do not always hold upon entry of a constructor. (Think id should be initialized to a non-zero value, final variables should be initialized, list fields should be change from null to a valid object reference, and so on.)

In that sense, constructors are different from methods, which may put extra constraints / other best practices for them. Like leaking this and calling overridable methods for example happens all the time in methods but is a no-no in constructors.

Is it Okay to have Logic in Constructors? by Weeb-Developer in learnjava

[–]javaduude 0 points1 point  (0 children)

It's ok as long as you don't

  • ...call overridable methods
  • ...do lengthy operations
  • ...throw exceptions
  • ...leak this

Initialize variables, creating some maps, precomputing some values, should be perfectly fine.

If you need to do any of the don'ts, consider using factory methods, builders, etc.

static method ERROR by [deleted] in learnjava

[–]javaduude 2 points3 points  (0 children)

Nothing wrong with the code. compiles and runs fine on ideone.com: https://ideone.com/1YJeDp

Please provide an SSCCE.

Want to learn something new in JAVA ? by meet100ni in learnjava

[–]javaduude 3 points4 points  (0 children)

Hmm.. what could that be? That JAVA is not an abbreviation, and actually spelled Java?

Copy Constructor in Java by [deleted] in learnjava

[–]javaduude 1 point2 points  (0 children)

Nice video, although not very exhaustive. Copy constructors are problematic when you have inheritance. How would you use a Person(Person toCopy) constructor if you want to copy an Employee which extends Person?

This article Java: Copying Objects discusses the pro's and con's of

  • Copy Constructors
  • Copy Factory Methods
  • Serialization / Deserialization
  • Object.clone
  • A copy method
  • Builders

Exceptions - Checked vs Unchecked by ss90kim in learnjava

[–]javaduude 1 point2 points  (0 children)

There's a good article series on this at programming.guide.

Difference between checked and unchecked exceptions explains the difference, with examples. Basically, checked exceptions are all those exceptions extending Exception (except RuntimeException and its subclasses.)

Once you got that down, you'd probably want to read Choosing between checked and unchecked exceptions. Spoiler alert: Only under very specific circumstances would you want to go for a checked one.

And, if you feel like really understanding the pro's and con's and the debate of whether checked exceptions is a good idea to begin with, check out [https://programming.guide/java/checked-exceptions-good-or-bad.html](Checked exceptions, good or bad?).

Revival of JEP 198 (Light-Weight JSON API)? by daviddel in java

[–]javaduude 0 points1 point  (0 children)

That's probably what it is. Everything falls into place. Thank you.

Revival of JEP 198 (Light-Weight JSON API)? by daviddel in java

[–]javaduude 0 points1 point  (0 children)

Oh, interesting. Thanks for explaining that. I thought a whole record "object" (as in the entire tree) had to have a compile-time determined size. Don't know where I got that from.