Note: We cannot guarantee the accuracy of the content. Please note that this is written with the intention of alleviating the psychological anxiety of MVEL beginners who feel lonely due to lack of information in Japanese.
In short, Java is a scripting language. It's Java, not JavaScript. I dared to say it because it is important.
I think it's a fairly minor language, but it's actually open source. For the time being, it seems that issues are raised regularly and maintenance is also being done.
greet = "Hello "; //String type
name = "MVEL"; //String type
return greet + name; //result:"Hello MVEL"
--The line delimiter is a semicolon (;) --Dynamic typing eliminates the need to declare variable data types
MVEL has the property of returning the last evaluated value as a return value. So, return can be omitted, but I think it's kinder to add it for readability.
Most of the following common operators can be used.
--Comparison operators (==,! =,>,> =, <, <=) --Arithmetic operators (+,-, \ *, /,%, + =,-=, * =, / =,% =) -Logical operator (&&、||)
Others include contains, is instanceof, ~ =.
//Determine if the character string contains "n"
"Hello" contains "Hmm" //Result: true
//Judgment whether it is a numerical value
value = "100" //String type
value is instanceof Integer //Result: false
//Judge whether it is half-width alphanumeric characters
"Hello" ~= '^[A-Za-z0-9]+$' //Result: true
operator | Description |
---|---|
contains | True if the value on the left side contains the value on the right side |
is instanceof | True if the data type of the variable on the left side is the data type on the right side |
˜= | True if the value on the left side matches the regular expression on the right side |
hoge == null;
hoge == nil; //Synonymous with null
MVEL treats null and nil as synonymous as null values. You can use either one, but it may be safer to use null for Java.
//Change usage fee depending on age
if (age >= 60) {
System.out.println("Senior discount rate: 1,200 yen");
}
else if (age >= 20) {
System.out.println("General charge: 1,800 yen");
}
else {
System.out.println("Student discount fee: 1,000 Yen");
}
When the conditional expression is true, the processing inside {} is executed. If you want to set multiple conditions, connect them with else if ().
Although it is a small detail, the else statement is written on the next lower line with a line break according to the way of writing the official document.
age >= 15 ? "Yes" : "No";
In this way, conditional branching with the ternary operator is also possible.
For iterative processing, in addition to the basic loop syntax (for, while, until, do while, do until), you can use foreach, which is similar to the extended for statement added in Java 1.5.
//FizzBuzz problem
for (i = 0; i < 100; i++) {
if (i % 15 == 0) {
System.out.println("FizzBuzz");
}
else if (i % 3 == 0) {
System.out.println("Fizz");
}
else if (i % 5 == 0) {
System.out.println("Buzz");
}
}
Like i ++, increment can be used without any problem.
//Extract the elements of the list fruits one by one and output them to the console
fruits = ["Apple", "Grape", "pineapple"];
count = 0;
foreach (item : fruits) {
count++;
System.out.println(count + ". " + item);
}
If you want to display the index with foreach, it seems that you have to prepare the variable for the index yourself.
//Output a character string to the console (when the output destination is the standard setting)
def hello() {
System.out.println("Hello, world"); //Result: Hello, world
}
//Calculates the area of a triangle and returns a message
function calcTriangleArea(bottom, height) {
area = bottom * height / 2; //Int type
message = "The bottom is" + bottom + "The height is cm" + height + "The area of the cm triangle is" + area + "It is ㎠."; //String type (variable is automatically Int type=>Convert to String type)
return message;
}
You can use def and function for MVEL function definition. There is no difference between the two, but since def is used in the official document, let's unify it to def.
As an aside, when I first saw the MVEL source, I misunderstood it as Ruby.
Use Java libraries to handle dates in MVEL. There are Date and Calendar in the date library, but this time we will use Calendar because we want to perform date and time calculation processing.
import java.util.Calendar; //Import Calendar class
//Create a calendar instance
Calendar calendar = Calendar.getInstance();
today = calendar.getTime(); //Get the current date and time
calendar.set(2020, 4, 23, 23, 59, 59); //Set sale end date (April 23, 2020 23:59:59)
endOfSaleDay = calendar.getTime();
if (today >= endOfSaleDay) {
System.out.println("20%OFF sale has ended.");
}
The above is the process of determining whether the discount sale has ended. Since the sale end date setting is a destructive change, if you then getTime (), it will be the current date and time at the set date and time.
What is MVELer in the first place! I would like to define it as the responsibility of the statement. That said, it feels like PHPer for PHP users and Rubyist for Ruby users, but strictly speaking, it refers to MVEL users who meet the following conditions.
-~~ Those who are fully aware of the range of influence ~~ ―― ~~ Those who can actively communicate so as not to cause degreasing ~~ --Those who love and enjoy the language MVEL
MVEL users who meet these requirements should be prepared to be ** MVEler ** and be proud to call themselves MVELER.
--You can use most of the methods and libraries in Java ――The more you use it, the better it becomes.
Recommended Posts