How to get the name of the containing function [duplicate] - java

This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?

Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html

Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)

Related

What is C# equivalent of Java's ClassName.class.getSimpleName() [duplicate]

This question already has answers here:
C# getting its own class name
(11 answers)
Closed 7 years ago.
I am trying to rewrite Java code to C# and I am facing the problem that C# has not got this Java method. Can you please give me the C#'s equivalent of this method or some other way to get the class name.
you can do it like this:
typeof(ClassName).Name

Java plus equals or equals plus operator? [duplicate]

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 7 years ago.
Is += the same as =+?
I can't find any reason for why the plus sign is reversible.
For what reasons would I need to use one of the other? Where can i find docs on this i tried searching but didnt see the use of both.
It's not the same.
x+=5 is equivalent to x=x+5.
x=+5 (or x=(+5)) is equivalent to x=5.

difference between printf and println in java? [duplicate]

This question already has answers here:
Is there a good reason to use "printf" instead of "print" in java?
(4 answers)
Closed 8 years ago.
Just came to know that java does has a method named printf, then what is the difference between printf & println?
System.out.println(); is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf(); would be used.
Check out this link for more information.

inserting c++ code in java [duplicate]

This question already has answers here:
Calling C++ functions from Java
(3 answers)
Closed 8 years ago.
Is there a way to insert some c++ code in java ?
for some reason my code which is:
ArRobotPacket pkt;
pkt.setID(ArCommands::SIM_SET_POSE);
pkt.uByteToBuf(0); // argument type: ignored.
pkt.byte4ToBuf(x);
pkt.byte4ToBuf(y);
pkt.byte4ToBuf(th);
pkt.finalizePacket();
robot.getDeviceConnection()->write(pkt.getBuf(), pkt.getLength());
translated to java, will not function, the write will actually send a packet but doesn't effect the program the way it should
This code is from
http://robots.mobilerobots.com/MobileSim/download/current/README.html#mapobjs
One way is to use Java Native Interface: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/

java calculate pathname difference [duplicate]

This question already has answers here:
How to construct a relative path in Java from two absolute paths (or URLs)?
(23 answers)
Closed 6 years ago.
Is there an open source library that, given ...
/a/b/c
/a/b/c/d/e
would return ../..
or for that matter given
/a/b/c/d
/a/b/c/e
would return ../d
?
If you don't mind passing by converting your Strings into URI then this latter one has the method relativize which should do exactly what you want, take a look here.

Categories

Resources