Indicates that JAVA = JAVAscript. For the time being, I also added a serious commentary at the end. Since there were some mistakes, I also added an apology at the end.
Paste the same code twice.
/*
<script>
var System = {out: {println: function(e){console.log(e);document.getElementsByTagName('body')[0].innerHTML=e;}}};
window.onload = function() {
eval(document.getElementById('code').innerHTML.replace(/[*]/g, '/').replace(/(public|static|void|String\[\] args)/g, ''));
}
</script>
<div id="code">
*/
class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*
new Main().main();
</div>
*/
/*
<script>
var System = {out: {println: function(e){console.log(e);document.getElementsByTagName('body')[0].innerHTML=e;}}};
window.onload = function() {
eval(document.getElementById('code').innerHTML.replace(/[*]/g, '/').replace(/(public|static|void|String\[\] args)/g, ''));
}
</script>
<div id="code">
*/
class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*
new Main().main();
</div>
*/
To be honest, it can be executed by pasting it on a site called paiza. Move! Hello, world! Wow!
Save the above code as ** html ** and double-click it to open it in a suitable browser. The verified environment was chrome. Hello, world! Wow!
Perhaps some browser screens have weird strings on them, but true guys don't care about that. If you're using some browsers, take a look at the developer tools. Hello, world! Wow!
For a true man, it's obvious that JAVA = JAVAscript. There are some things like JAVA and JAVA script are different, but that doesn't work in the wilderness of Mexico. The one who doesn't understand it is good at first, but eventually collapses in the Mexican wind that blows in the comment section, comforts with Babe who met at the bar, gets drunk with the pleasures of the moment, gives birth to a child, and says, "Nothing interesting. It was a lifetime without a baby, "he said, whispering at the bar ... and eventually he died of old age. THE END OF MEXICO .... But if you still have a true pioneering spirit, that's a different story. It's not too late now. Do JAVA = JAVAscript.
Java and JavaScript are different things. I'm sorry. Why do some people claim that they are different but the same? As for the background, https://qiita.com/pik/items/1c95d69debee4aedffea Is detailed.
Java and JavaScript are languages that are similar in comment format because they are derived from C, but on the other hand, the behavior of reserved words is different. In particular, the handling when placing tokens only in spaces is poor. So, I thought it would be impossible to write code that would work either way in a simple way, so I started to do something wrong.
First, by using HTML, we create an area where comments are made in Java but not in JS. In the Java world, this allows you to treat all your JavaScript code as comments. Since Java doesn't have macros, it's a little difficult to dynamically rewrite the code at runtime, so I'll make it normal Java code except for comments.
As mentioned above, there are a lot of reserved words that get in the way in the naive way, so rewrite the code. The key is eval and replace.
eval
eval is something that should not be done in terms of performance or security, and evaluates a string as JavaScript code.
replace
Since the reserved words and comments are in the way, replace them with regular expressions. Other than reserved words, some of them are troublesome, so I will erase them.
Actually, it's not impossible to put php in this code, for example, but I thought it would be a bit annoying to output only Hello, world!
I was copying and the Java source was an older version. fixed. I'm sorry.
It was good to replace once as follows. I'm sorry.
revised edition
/*
<script>
var System = {out: {println: function(e){console.log(e);document.getElementsByTagName('body')[0].innerHTML=e;}}};
window.onload = function() {
eval(document.getElementById('code').innerHTML.replace(/[*]/g, '/'));
}
</script>
<div id="code">
*/
class Main {
/**/ public static void
main(
/**/ String[] args
) {
System.out.println("Hello, world!");
}
}
/*
new Main().main();
</div>
*/
That's it!
Recommended Posts