I've started using the .match(Regex) method in my java program, but for now I'm just using a string (String regexString = new String("[^a-zA-Z0-9][^a-zA-Z0-9]*"); which is what I have so far, as an example). I know however I can use an actual regex (Regex pattern = new Regex() & the Pattern class then compile it (?) some how).
Is there an advantage to using Regex as a class and not just a string, in java? I'm quite used to bash scripting and there regexes are just 'strings' in the loosest sense, and there is no ability/need for a separate class, so I'm struggling to see where there is one here.
new String("...");. Just use"..."instead:String regexString = ".*";. It's unnecessary and inefficient to explicitly create a newStringobject.".*", aStringobject is created. But if you donew String(".*"), a secondStringobject is created which copies the content of the literal string, which is unnecessary. Strings are immutable in Java. Java has a string pooling mechanism, so that if you use the same string literal multiple times, there will be only oneStringobject which is shared.