Java function definition with brackets syntax [duplicate] - java

This question already has answers here:
Java Syntax: byte f()[] vs. byte[] f()
(4 answers)
Closed 3 years ago.
Was looking in the source for ByteArrayOutputStream, and I saw this function:
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}
Where is this syntax documented? I mean the [] in front of the function. Is this the same as in declaring a regular array where the bracket can go after the name of the array or before, but in this case, the bracket can go after the function name?
String[] args;
Vs
String args[];
Edit: 2018-05-22
I found even more uses of this crazy syntax here: 10 things you didn't know about Java
#3 is where they make mention of all the ways the above syntax can be exploited

In JLS Sec 8.4:
MethodDeclarator:
Identifier ( [FormalParameterList] ) [Dims]
...
The declaration of a method that returns an array is allowed to place some or all of the bracket pairs that denote the array type after the formal parameter list. This syntax is supported for compatibility with early versions of the Java programming language. It is very strongly recommended that this syntax is not used in new code.

Related

String Array Assignment in Java [duplicate]

This question already has answers here:
Arrays constants can only be used in initializers error
(5 answers)
Closed 4 years ago.
Code:
String Foo[];
Foo={"foo","Foo"};
Error at Line 2: Illegal Start of expression
The code works if I say:
String Foo[]={"foo","Foo"};
Why does this happen, and how should I do the required without generating an error? This also happens with other data types.
Would appreciate if you could explain in layman terms.
{"foo","Foo"} is an array initializer and it isn't a complete array creation expression:
An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.
Java Specification
Use new String[] {"foo","Foo"} instead.
You have to initialize the string array:
String foo[] = new String[]{"foo, "Foo"}; Or
String foo[] = {"foo, "Foo"};
Modern IDEs give error for not initializing the array objects. You can refer more details here:
http://grails.asia/java-string-array-declaration

In Jsp,what's the difference between ${param.name} and ${param[name]} [duplicate]

This question already has answers here:
Difference Between dot operator and brackets in jsp el
(3 answers)
Closed 6 years ago.
In Jsp,what's the difference between ${param.name} and ${param[name]}?
I've tested that they can both get the parameter I set from a servlet,but I want to find out the difference between them.e.g,in what sittuaton,one can work while another one doesn't?
Thanks in advance.
All the following ways are evaluated same in standard JSP EL:
${param.myvar}
${param[name]} // here `name` is another EL variable with value `name = "myvar"`
${param["myvar"]}
${param['myvar']}
If you're referring to a variable, then you can use 2nd one. Otherwise if you're directly accessing through value, then you can use 3rd or 4th one.
According JSP specification:
The EL follows ECMAScript in unifying the treatment of the . and [] operators.
expr-a.identifier-b is equivalent to expr-a["identifier-b"]; that is, the identifier
identifier-b is used to construct a literal whose value is the identifier, and then the []
operator is used with that value.

What is "int..." in Java method's parameter? [duplicate]

This question already has answers here:
What is the ellipsis (...) for in this method signature?
(5 answers)
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
I came across a method definition:
void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
The "int..." seems to indicate an indeterminate number of integer parameters but the variable is used as an array inside the function. Can someone please explain?
As you already stated correctly this java notation is to make the method accept a variable amount of (in this case) int parameter.
To handle this variable amount of variables you can access it like an array.
This functionality is introduced in java 5.
See also here:
https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
You are right in deducing that the ellipses indicate that this method is variadic.
When you have a variable number of potential arguments, you need some way to iterate over them - otherwise they aren't very useful in practice. Java and C# happen to have the array indexing syntax. C and C++ happen to have the va_start, va_arg and va_end macros. Different languages may have something else.
The reason why Java has the array syntax specifically is probably because it happens to match the way they are actually implemented: as a simple array parameter replaced at compile time.

Expressions vs Literals && Expressions vs Statements [duplicate]

This question already has answers here:
Expression Versus Statement
(21 answers)
Closed 7 years ago.
What's the significant difference between Expressions vs Literals && Expressions vs Statements in Java? I'm aware that an expression represents a simple value or a set of operations that produces a value. However, literals can also be a simple value which makes it a little confusing to make difference between them. Could smb explain the differences? Thanks in advance!
A literal is a notation for representing a fixed value in source code. For example: 'a', "a", Object.class, and 1 are all literals. Their value is known at compile-time, and they are expressions, since they evaluate to a single value.
Statements are complete units of execution. Most statements terminate with a semicolon. For example: new Object(); is a statement, while new Object() is an expression that returns a reference to a newly created object. Examples of statements that don't terminate with a semicolon are blocks and control flow statements.

How do I make a mathematical function f(x) for any generic f? [duplicate]

This question already has answers here:
Method for evaluating math expressions in Java
(8 answers)
Closed 9 years ago.
I tried searching for it via google and here but I'm not finding questions to what I mean (search engines don't understand the context by which I mean function).
Essentially I want to do the following
double f(String function, double a){
return function.Function(a);
}
What would happen is the string function is of the form "x^2+2" it would likely be converted somehow to "x.pow(2) + 2" and then x is replaced by a and the result of the function is returned.
Is there any Java class or method that does what I said (or simple way to do it)? Or any code from another source that does what I said or a variant.
I don't have to code what I said, I just need f(x) to solve root finding problems for any function string passed as input. I thought Java would have such a method somewhere but I can't find it.
So, in Java you have an essential problem because you cannot directly convert a String to a mathematical expression. Your options are as follows:
Search for a library that can convert a particularly formatted string to a mathematical expression.
Parse the string yourself. String parsing is difficult and error prone, and the Java around this would be difficult.
Use Scala, which would allow you to directly compose functions to pass into your function, rather than trying to do the expensive conversion from a human-readable string to a machine-interpretable function. Note that Scala is interoperable with Java, but has a bit of a learning curve. Other functional languages can handle this as well, but may lack interoperability.

Categories