Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Groovy replace java.lang.String.length() with size()?

Wikipedia's current article about the Groovy programming language explains that "Most valid Java files are also valid Groovy files" and gives the following examples, first of Java code:

for (String it : new String[] {"Rod", "Carlos", "Chris"})
    if (it.length() <= 4)
        System.out.println(it);

Then the same in Groovy:

["Rod", "Carlos", "Chris"].findAll{it.size() <= 4}.each{println it}

Notice in the first example that we used the perfectly ordinary Java method, java.lang.String.length(). In the second example this method has been mysteriously replaced with a call to a method called size(). I have verified that the second example is valid Groovy code and has the correct behaviour.

java.lang.String does not have a method called size(). Groovy does not subclass String for its own purposes:

String s = ""
Class c = s.getClass()
println c.getName() // "java.lang.String"

nor does it somehow add extra methods to the String object:

// [...]
for (def method : c.getMethods()) {
    println method.getName()
}
// prints a whole bunch of method names, no "size"

and yet still this code somehow works:

// [...]
println s.size() // "0"

I can't find any Groovy documentation to explain this.

  • Where does size() come from?
  • Why does it not appear on the object?
  • Why was it added?
  • What was wrong with length() and why is it not preferred?
  • What other extra methods have been added to java.lang.String?
  • What about other standard classes?
like image 755
qntm Avatar asked Jul 15 '15 14:07

qntm


People also ask

How do I find the size of a String in Groovy?

Syntax − The length of the string determined by the length() method of the string. Parameters − No parameters. Return Value − An Integer showing the length of the string.

Can the length of a String change Java?

String are immutable in Java. You can't change them.

How do I change strings in Groovy?

In this case, you would use: Change_1 = Log_file_1. replaceAll(/\/,'/'); Slashy strings also support interpolation, and can be multi-line.

How do I limit the size of a String in Java?

The indexing is done within the maximum range. It means that we cannot store the 2147483648th character. Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.


1 Answers

Groovy adds lots of methods to strings and all sorts of other classes. All of the convenience methods are part of why Groovy is great.

java.lang.String implements java.lang.CharSequence, and that's where it gets all (most of) the magic from. size(), for example. Groovy adds a size() method to most objects that can have something you'd consider a size so that you can use a consistent method across the board. length() is still entirely valid, Groovy doesn't remove this.

To see some of the methods Groovy adds, check out the GDK, and particularly CharSequence and Collection.

like image 89
doelleri Avatar answered Oct 21 '22 17:10

doelleri