This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 8 years ago.
There is one line of C++ code shown below.
return ::as_Register(value() >> 1);
I just want to know what's the meaning of the '::' which has nothing before it.
Is it the C++ syntax? Can there be nothing before '::'? Such as return ::myMalloc(size)?
The code is from jdk8 openjdk/hotspot/src/cpu/x86/vm/vmreg_x86.inline.hpp.
I am deeply studying the JDK.
A :: references the global namespace:
void bar();
namespace some_namespace
{
void bar();
void foo()
{
// writing bar() would call some_namespace::bar()
// but if we want to call the global bar() we have to write:
::bar();
}
}
It's C++. It means that you import this function from other namespaces.
Example:
namespace foo {
void bar();
}
void bar();
namespace foo {
void foobar()
{
bar(); // Means foo::bar()
::bar(); // Means bar() outside foo namespace
}
}
Related
This question already has answers here:
Java 8 method reference unhandled exception
(6 answers)
Closed 1 year ago.
I have two functions throwing Exception:
public void foo() throws Exception {
// something
}
public void bar() throws Exception {
// something
}
If I combine those function calls using curly braces in lambda expression, it needs try/catch to handle exception.
public void someFunction() throws Exception {
someList.forEach(e -> {
// Show compile error and need try/catch
foo();
bar();
});
}
However, if I combine in for-loop, it is fine.
public void someFunction() throws Exception {
for (SomeElement e : someList) {
foo();
bar();
}
}
I though try/catch needed because of new closure created (using bracket), but in for-loop, it does not require.
I solved it by just using for loop, but I wonder why it happens.
List#forEach expects a Consumer.
Consumer is a (single-abstract-method) interface that looks like this (simplified):
#FunctionalInterface
public interface Consumer<T>{
void accept(T t);
}
As you see, accept does not throw any checked exceptions.
The lambda expression is an implementation of that interface so it cannot throw any exceptions, no matter what the other code does.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
What are the rules for evaluation order in Java?
(5 answers)
Closed 2 years ago.
public class Bar {
private Foo m_foo;
private int getNumbers(){
m_foo=new Foo();
return 5;
}
public void test1(){
m_foo.print(getNumbers());
}
}
public class Foo {
public void print(int x){
System.out.println(x);
}
}
public class Main {
public static void main(String args[]){
new Bar().test1();
}
}
The NullPointerException occurs in test1()call, but I can't understand the reason behind. Isn't the m_foo supposed to be instantiated in the getNumbers() which should get evaluated first?
NullPointer occurs at
m_foo.print(getNumbers());
because m_foo is initialized in getNumbers method, and it is never got called before calling test1.
Java executes statement in a sequence from left to right. So first
it will check for m_foo object. Ideal way to do this in a constructor
of Bar.
public Bar(){
m_foo=new Foo();
}
This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 8 years ago.
Please explain why i'm getting "Method with String param" in output.
And when i remove the comments from display(Test x) method, it says "Reference to display is ambiguous".
class Test
{
int a;
int b;
}
public class TestIt
{
public static void display(String x)
{
System.out.println("Method with String param");
}
public static void display(Object x)
{
System.out.println("Method with Object param");
}
/*
public static void display(Test x)
{
System.out.println("Method with Test param");
}
*/
public static void main(String args[])
{
display(null);
}
}
Because null is a valid value for Object and String. You can cast,
display((String) null);
Will output
Method with String param
or
display((Object) null);
for
Method with Object param
Because when figuring out which method to call, the compiler picks the most specific method it can find that matches the argument. Both display(String) and display(Object) match a call to display(null), but display(String) is more specific than display(Object), so the compiler uses that. When you uncomment the display(Test) version, though, the compiler can't make a choice because both display(String) and display(Test) are equally specific.
For all the gory details, see ยง15.12 of the JLS.
This question already has answers here:
What does the arrow operator, '->', do in Java?
(6 answers)
Closed last year.
Today I met part of weird code which I do not understand.
What can mean something like this ()->
for example
method( ()-> System.out.println("Hello") );
another question is, what is an output of String:
[AB][CD]*EF+(X/Y)
of course there were not any instance of A, B etc.But I there were answers like
ABCDEFX,
ACEFXX,
ACEFXA,
I do not remember them
Can anybody help me?
Suppose you have an interface that declares one method:
public static interface MyFunctionalInterface {
void m1();
}
And you have a method that receives an object of that type as a parameter:
public void method(MyFunctionalInterface i) { ... }
You can implement that interface and use it immediately using anonymous inner classes like this:
method( new MyFunctionalInterface() {
public void m1() {
System.out.println("Hello");
}
});
In Java 8 you can replace that with a lambda expression such as the one you showed:
method( () -> System.out.println("Hello"); );
The empty parameters represent the m1() method, with no parameters.
Suppose the functional interface you were using had a method with one parameter (if your method had the form method2(ActionListener s) { ... } for example), then you would use:
method2( e -> System.out.println("Hello"); );
which would be the same as doing this:
method2( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}
});
There are many tutorials about Lambda expressions in Java 8. This one is a good quick-start.
This question already has answers here:
Using an arbitrarily defined method of an anonymous interface
(4 answers)
Closed 9 years ago.
I can imagine some very creative code in Java:
Object thing = new Object() {
public void speak() {
System.out.println("Hi!");
}
};
thing.speak();
Or even, to get the full closure effect, define a Function interface ... you get the idea?
Why doesn't this code work?
i believe you can do it like this :-
new Object() {
public void speak() {
System.out.println("Hi!");
}
}.speak();
may help you .
Not sure about the usefulness in this example, but some type of overriding method(s) on the original declaration is useful and because of it is overriding, you can call the methods. Otherwise in your case, just use the reflection as:
thing.getClass().getMethod("speak").invoke(thing);
and for the overriding method:
Object thing = new Object() {
public void toString() {
System.out.println("Hi! Me inside your mind!");
return "not today!";
}
};
thing.toString();