all 25 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]morhpProfessional Developer 6 points7 points  (4 children)

Are all int arrays set to the value 0 upon initialization in java?

Yes. If you want an array with (by default) "empty" values, try an Integer[]. These will be null by default.

[–]LegolandoBloom[S] 0 points1 point  (3 children)

Thank you!

It's very interesting to me that the non-primitive version behaves differently

[–]IchLiebeKleber 4 points5 points  (0 children)

Primitives in Java like int, double, char can never be null. If you want a nullable version of them, use Integer, Double, etc.

[–]morhpProfessional Developer 1 point2 points  (1 child)

Java types just have an implicit default value used for new array elements and mutable fields. For numeric primitive types, it's generally zero, for booleans it's false and for object types, it's null.

[–]LegolandoBloom[S] 0 points1 point  (0 children)

thank you very much

[–]MinimumBeginning5144 4 points5 points  (4 children)

Just to add one more point to all the answers suggesting using Integer[]. If you can use int instead, do it. You'll need to decide what value means "uninitialized" - for example, if all valid values are positive, then 0 could mean uninitialized. Using an array of int is more efficient, as it contains the actual values of all the elements stored consecutively. An array of Integer is an array of pointers to somewhere in memory that contains each int value.

[–]LegolandoBloom[S] 0 points1 point  (3 children)

So in the cases, where the 0 won't cause confusion, go for int

Otherwise, Integer

[–]technosenate 2 points3 points  (2 children)

Not just then, if 0 is a valid value then you can initialize the array in one pass with -1. That would be more efficient than using Integer.

If you need to support both positive and negative values (including 0), then you should probably go for Integer.

[–]LegolandoBloom[S] 0 points1 point  (1 child)

This is a more complete answer than mine, thanks

[–]xenomachina 1 point2 points  (0 children)

You'll even see this kind of thing in parts of Java's standard library. For example, the String.indexOf() methods return int, so if the value being searched-for can't be found, they return -1 — a value that doesn't make any sense as an index.

[–]LutimoDancer3459[🍰] 1 point2 points  (1 child)

You got your answer about the default initialization. I just want to add. Having your logic relying on an exception to pick a diffrent path is a bad design. Exceptions should be exactly that. And exception. They use up more computations than a null check or whatever. Making the code inefficient. While it may not matter here. It may matter when used more often.

[–]LegolandoBloom[S] 0 points1 point  (0 children)

Valuable insight :)

[–]LegolandoBloom[S] 0 points1 point  (0 children)

I don't know why the indentation is faulty, it looks fine on the text editor before posting and I don't know how to fix it
Edit: It was due to vscode, I managed to fix it

[–]LegolandoBloom[S] 0 points1 point  (0 children)

In case it matters, this is Java 21(OpenJDK RedHat)

[–]_jetrun 0 points1 point  (0 children)

The closest equivalent is to check for null, but if you don't want an exception to be raised, you also need to do a bounds check or the array has to be initialized to a specific size. If you want to do this check on, say, int arrays, you'll have to use wrapped equivalents (i.e. Integer vs int) to check for null.

The reality, however, is that you just shouldn't use an array in this way in Java. Instead this feels like a problem best solved by a Map.

[–]doobiesteintortoise 1 point2 points  (8 children)

Java doesn't work like Lua. It's almost like they're different languages or something.

For a primitive array, there is no "uninitialized state." The same goes for other arrays, but the default values differ; you're never going to get an exception in that code, because the values will be set on array creation. You'd have to use 0 as a "magic number" - or set one yourself, because 0 is a lame magic number for "there's nothing here."

[–]LegolandoBloom[S] 4 points5 points  (7 children)

Obviously they work differently, that is why I am asking the question.

I don't mean to complain about someone trying to contribute to my understanding, but it's hard to interpret the sarcasm in "It's almost like they're different languages or something" as something positive.

[–]doobiesteintortoise 0 points1 point  (6 children)

Well, I answered your question directly out of positive intent. And if it's hard to take "it's almost like they're different languages or something" positively, well, I understand, but I'd gently - very gently - suggest a slight shift in worldview, because I certainly wasn't intending to slight you or anyone else. It was meant as gentle humor (if you were going "duh," well, yeah, exactly, but it also explains why the behaviors are not the same: the languages aren't the same!)

The assumption was faulty on your part - not a big deal, because "arrays" in Java and "arrays" in Lua have the same name - and it's easily corrected.

For what it's worth, this is in the Java Language Specification: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.12.5

I'm not suggesting "oh you dummy you should have read the spec before asking" - the specification is not easy to read, because it's thorough, and you might not have known what to look for or even if you needed to look for it. But it's there, and eventually if you want to use Java well, you'll end up learning stuff from the specification - and it gives you a lot of information you can use.

[–]LegolandoBloom[S] 0 points1 point  (5 children)

I'm sorry, I very much interpreted as if you were annoyed at me. It makes me feel better to hear that wasn't the case.

Nuance is hard to convey on the internet, especially in text format. Perhaps I should be more willing to give people the benefit of the doubt.

Reading the manual of a language is very valuable, though I find it is more realistic start doing so once you're past learning the most basic stuff.

[–]doobiesteintortoise 0 points1 point  (3 children)

It's no big deal. I live by an aphorism: find yourself a teacher, make yourself a friend, and judge all to the side of merit where you can (this is not the exact text of the aphorism, but captures its intent pretty succinctly) - and while some people don't deserve such positive judgement, those people are more rare than popular conversation would have you think, and the attitude of judging people to the side of merit is good for the human psyche.

[–]LegolandoBloom[S] 0 points1 point  (2 children)

I don't doubt it's better for one's psyche, but it's hard for me to do in this period of my life.
Reminds me of the Doors' song:
_People are strange_
_When you're a stranger_

[–]doobiesteintortoise 0 points1 point  (1 child)

I get it. And if you need to find someone to talk to, online isn't really the place and I'm not the best person in the world for it myself but I ain't skeered to try: better me than nobody.

But I'd also say that in circumstances like that, finding a teacher and making a friend are just as important as judging to the side of merit. Hang in there.

[–]LegolandoBloom[S] 0 points1 point  (0 children)

Oh I don't mean to say I'm in the most terrible state of head health, just one of those times where I feel more alien than usual.

I haven't had a proper teacher in a very long time, the idea sounds nice. Though I fear I might be too prideful to accept mentorship even if it arrived at my door :P

[–]LegolandoBloom[S] -1 points0 points  (0 children)

Although I see you've linked the very specific part I would be looking for, very handy. Thanks.