Think about the JAVA = JAVAscript problem (needed in the future)

Purpose of this article

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.

code

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>
*/

Run with JAVA

To be honest, it can be executed by pasting it on a site called paiza. Move! Hello, world! Wow!

Run with JAVAscript

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!

poem

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.

It's a lie

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.

A little commentary

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.

comment

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.

Code rewriting and eval

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.

one point

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!

apology

Part 1

I was copying and the Java source was an older version. fixed. I'm sorry.

Part 2

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

Think about the JAVA = JAVAscript problem (needed in the future)
Think about the differences between functions and methods (in Java)
About the confusion seen in startup Java servers
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Learn about the spec while shortening FizzBuzz written in Java
[For beginners] About the JavaScript syntax explained by Java Gold
Access the network interface in Java
Guess the character code in Java
Pass the i18n locale to JavaScript
About the symbol <%%> in Rails erb
Specify the java location in eclipse.ini
Java, JavaScript, C # (difference in assignment)
Unzip the zip file in Java
About the current development environment (Java 8)
Parsing the COTOHA API in Java
Expression used in the fizz_buzz problem
Think about the 7 rules of Optional
About Eclipse environment (Java, Liberty, JavaScript)
About file copy processing in Java
The road from JavaScript to Java
Call the super method in Java
[Java] java.lang.NoClassDefFoundError: Addressing the problem of falling in org / jsoup / safety / Whitelist
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
About returning a reference in a Java Getter
Get the result of POST in Java
Guess about the 2017 Java Persistence Framework (3) Reladomo
Java reference to understand in the figure
Call Java method from JavaScript executed in Java
Try using the Stream API in Java
Call the Windows Notification API in Java
About the procedure for java to work
[Creating] A memorandum about coding in Java
About the new Java release model @ Seki Java (2018/07/20)
I tried the new era in Java
Organized memo in the head (Java --Array)
Try calling the CORBA service in Java 11+
Use "Rhino" which runs JavaScript in Java
About Records preview added in Java JDK 14
What is the main method in Java?
How to get the date in java
Continued Talk about writing Java in Emacs @ 2018
About the number of threads of Completable Future
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
Java vs. JavaScript: What ’s the Difference?
Just skip Masakari to "programming languages that will be needed in the future"
Impressions and doubts about using java for the first time in Android Studio
About the meaning of type variables, E, T, etc. used in generics used in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the description order of Java system properties
ChatWork4j for using the ChatWork API in Java
Organized memo in the head (Java --Control syntax)
Try scraping about 30 lines in Java (CSV output)
The intersection type introduced in Java 10 is amazing (?)
The story of learning Java in the first programming
Measure the size of a folder in Java
About var used in Java (Local Variable Type)