Java Number Max Value max(final String string1, final String string2)

Here you can find the source of max(final String string1, final String string2)

Description

Returns the shortest string.

License

Apache License

Parameter

Parameter Description
string1 the string 1
string2 the string 2

Return

the shortest string

Declaration

public static String max(final String string1, final String string2) 

Method Source Code

//package com.java2s;
/*/*  w  w  w . j ava  2  s  . c  o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns the shortest string. In case one string is null, the other string will be returned
     *
     * @param string1 the string 1
     * @param string2 the string 2
     * @return the shortest string
     */
    public static String max(final String string1, final String string2) {
        if (string1 == null) {
            return string2;
        } else if (string2 == null) {
            return string1;
        }
        return string1.length() > string2.length() ? string1 : string2;
    }
}

Related

  1. max(final int a, final int b)
  2. max(final Iterable numbers)
  3. max(final Iterable values)
  4. max(final NUMBER_TYPE n1, final NUMBER_TYPE n2)
  5. max(final NUMBER_TYPE number1, final NUMBER_TYPE number2)
  6. Max(float a, float b)
  7. max(float a, float b)
  8. max(float a, float b)
  9. max(float a, float b)