What is the different between Method and Function in JAVA? [duplicate] - java

This question already has answers here:
What's the difference between a method and a function?
(41 answers)
Closed 7 years ago.
I was wondering What is the different between Method and Function in JAVA?

it the same, function or methods are just different ways to call it, but in my long experience with java i commonly hear function from my fellow developers the last time i heard anyone say 'method' was back when i still used C++

Related

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

Security for String immutability [duplicate]

This question already has answers here:
How does Java strings being immutable increase security?
(3 answers)
Closed 8 years ago.
Im trying to understand how String immutability increases the security. I had searched and found many cases but it does not give real practical example.
Here is one such example -
boolean connect(string s){
if (!isSecure(s)) {
throw new SecurityException();
}
//here will cause problem, if s is changed before this by using other references.
causeProblem(s);
}
In the above case the connect method could be called with any valid String
For ex:- connect("DB2") or connect("ORACLE") and the method will be executed accordingly.
Can someone elaborate more on this how the security is enhanced?
Excuse if its more basic question.
There are also caveats in the opposite direction, for example some API avoid using String for passwords and prefer char[] (which can be erased in memory immediately after use, whereas a String may stick around for quite a while).

String.length() vs Array.length [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is String.length() a method?
Java - Array's length property
Was there a specific design/performance reason as to why String has a method for length but Array has a variable length?
There is no Array class in Java (other than in reflections). Arrays are "primitives" of a sort in Java and play by different rules from declared classes.
Certainly a length() method could have been defined on arrays, but the designers wanted to keep length as a property rather than a pseudo-method. (In part this may have made it easier for early Java implementations.) The reasons are somewhat buried in history.
(A better question is why Java couldn't decide whether to call the concept "length", "count", or "size" -- I always end up trying all three before I hit on the right one for an aggregating class.)

How does setOut method change the value of System.out which is final in System class [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
java: “final” System.out?
Can someone please help me in understanding how the setOut method works?
It can change the current pipe lining of System.out from Monitor to PrintStream object, but how is it changing reference of out?
System#setOut internally calls a native method to redirect out. In other words: voodoo magic ;)
Since Java is open source, let's have a look:
http://www.mavenjava.com/sun/jdk/1.6.0/src/java/lang/System.java.html#System.setOut%28java.io.PrintStream%29
It calls out to a native method to set the output - the JVM basically bypasses the "final" check.

JUnit assertArrayEquals [duplicate]

This question already has answers here:
Comparing arrays in JUnit assertions, concise built-in way?
(8 answers)
Closed 8 years ago.
I've written a method that returns an int[]. I want to create a test that ensures that when I run my method, passing, say, 10 to the method, that it returns an int[]{0,1,3,5,7}.
How can I do this? Maybe I'm just missing the proper syntax or should be using a different assert method...?
If you are using JUnit4, you could use the org.junit.Assert class:
Assert.assertArrayEquals(new int[]{0,1,3,5,7}, someMethod(10));

Categories

Resources