Here you can find the source of tryParseInt(String num)
public static Integer tryParseInt(String num)
//package com.java2s; /*//from ww w. java2s .c o m * Copyright 2017 Google Inc. * * 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 { public static Integer tryParseInt(String num) { if (num.length() > 11 || num.length() == 0) { return null; } int i = 0; boolean negative = false; if (num.charAt(0) == '-') { if (num.length() == 1) { return null; } negative = true; i = 1; } // long to prevent overflow long number = 0; while (i < num.length()) { char c = num.charAt(i); if (c < '0' || c > '9') { return null; } number = number * 10 + (c - '0'); i++; } if (negative) { if (-number < Integer.MIN_VALUE) { return null; } else { return (int) (-number); } } else { if (number > Integer.MAX_VALUE) { return null; } return (int) number; } } }