#difference between int[] x and x[]
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
In Java, the difference between int[] x and x[] is primarily a matter of syntax. Both declarations are used to create an array of integers in Java.
int[] x is the traditional way to declare an integer array in Java. It follows the standard syntax where the data type (int) is followed by square brackets [] indicating that it is an array. The variable name (x) comes after the data type and is followed by optional square brackets if declaring multiple arrays at once.
On the other hand, x[] is a less common but still valid way to declare an integer array in Java. The variable name (x) comes before the data type (int) and square brackets [], which can be confusing for some programmers not familiar with this syntax.
Despite their syntactical differences, both int[] x and x[] are functionally equivalent when declaring an integer array in Java. They can be used interchangeably and will produce the same result.
In conclusion, while int[] x follows the standard syntax for declaring arrays in Java, x[] offers a less common but still valid alternative. It ultimately comes down to personal preference or coding style which syntax you choose to use when working with arrays in Java.
Both are valid, but the former is a lot clearer.
well in some cases there is actually a difference
but it is not recommend to use that anyways
oh ok
Technically you can even go fully cursed mode with int[] x[];
yes, but thats a 2d array
similar to int[][] x or int x[][]
and the same as int[] x[] I think
tldr don't use this syntax
void m(int[] x[]) { ... } >:-)
Still appalling that's legal. Please don't ever use it (or the old c-style array declaration at all for that matter).
Note that int x[] isn't accepted in some new places in Java, it exists only to allow some old code-bases to recompile without changes.
You should always use int[] x. Think of it when answering the question "What is the type of x". The answer is int[] not int or [] so the placement in int x[] makes no sense in Java.
in what places is it not accepted?
other than maybe third-party linters that complain about it being C style
IIRC one of them is when supplying lambda arg types
And the JDK discussion boiled down to "we could support it, but no one should be writing new code using that syntax"
got an example?
But it's been a few years so I don't recall if that's the only case.
i dont recommend using the old-school way, but its hard to believe Java would disallow it
i mean, maybe in the future, as some big code-breaking update. but on a case-by-case, seems strange
(int foo[]) -> { ...} is unsupported if I'm remembering correctly.
Since I never use that old syntax I can't be certain.
vs (int[] foo) -> {...} which is legal.
let me try 👀
that's legal
Consumer<int[]> c = ((int a[]) -> { });```
im pretty sure something like this is legal
@knotty basin if you wanna try the code above
It is not accepted by records
Ahh... THat's where it was.. I can probably find the dev-list discussion on it now I know where they restricted it.
they are fading it out
its a legacy construct overall
they obviously keep it on old stuff for backwards compatibility
also see brians comment on this one:
Oh it'll also not be supported in the planned array patterns - but I think those are a ways from any firm syntax yet.
https://mail.openjdk.org/pipermail/amber-spec-experts/2020-July/002249.html
and deeper in the thread from Brian Goetz:
As we've added new places or ways to declare variables (e.g., var,
record components), we've been trying to not propagate this syntax
mistake. So I agree that this is the right behavior; I think the change
just got lost in the shuffle.
notepad++ java dev?
does it support java syntax & imports
oh ok
why not
IntelliJ and other waaaaay better
yeah but wouldnt notepad++ be liter and faster
Just use jshell -_- @knotty basin
Well yeah, but I was testing smth else there that doesn’t work with jshell
it's a command, enter it in your terminal
does it come with java
yes
can u like compile code in cmd witi t
an example about what
just enter jshell in your console
@brisk tusk so ?
enter 2+2
Brackets on the type in the declaration apply to all variables within the statement:java int[] x, y, z; // All three of these are 1D arrayswhereas brackets on the variable name apply to just that variable:java int x[], y, z; // Only x is a 1D array, y & z are plain ints.It's also syntactically valid to mix and match the brackets as others have pointed out:java int[] x[], y, z; // x is 2D, y & z are 1D
Each pair of brackets (whether on the type or the identifier) represents an array type, so they can each individually be annotated with appropriate annotations:java int @A[] @B[] x;
Also, the brackets on the identifier in the variable declaration denote that that variable is an array of whatever the type of the declaration is. This means that the brackets on an identifier apply to, or "after," the type of the entire declaration, which is important for annotations:java int @A [] x @B [];is the same as:java int @B [] @A [] x;
Also, similar to variables, you can put brackets at the end of a method's parameter list:java int test(int a, int b) [] // weird { ... }
Also, nice about me.
@brisk tusk
OHH
thanks for the info this makes ALOT of sense
yes i was very impresed that it compiled 🤣 😂
im gonna chnage it to a 3d array
yea so you just enter valid java expressions and it will execute them
you need to put them in classes or methods
whenever you enter something, it will reply with for example here in the screenshot $1, it's in reality a variable that you can use
you can also tab for autocompletion, you can check doc, there are shortcuts for auto imports, many other stuff, and there are many commands like /save filename to save the code into a file, or /open filename to loadcode from a file, so let's say you want to do some code that you can reuse in jshell, you save it and then /open when you need it
I’m glad it helped. 