src/share/classes/java/lang/Character.java

Print this page




2554     private static class CharacterCache {
2555         private CharacterCache(){}
2556 
2557         static final Character cache[] = new Character[127 + 1];
2558 
2559         static {
2560             for(int i = 0; i < cache.length; i++)
2561                 cache[i] = new Character((char)i);
2562         }
2563     }
2564 
2565     /**
2566      * Returns a <tt>Character</tt> instance representing the specified
2567      * <tt>char</tt> value.
2568      * If a new <tt>Character</tt> instance is not required, this method
2569      * should generally be used in preference to the constructor
2570      * {@link #Character(char)}, as this method is likely to yield
2571      * significantly better space and time performance by caching
2572      * frequently requested values.
2573      *




2574      * @param  c a char value.
2575      * @return a <tt>Character</tt> instance representing <tt>c</tt>.
2576      * @since  1.5
2577      */
2578     public static Character valueOf(char c) {
2579         if(c <= 127) { // must cache
2580             return CharacterCache.cache[(int)c];
2581         }
2582         return new Character(c);
2583     }
2584 
2585     /**
2586      * Returns the value of this <code>Character</code> object.
2587      * @return  the primitive <code>char</code> value represented by
2588      *          this object.
2589      */
2590     public char charValue() {
2591         return value;
2592     }
2593 




2554     private static class CharacterCache {
2555         private CharacterCache(){}
2556 
2557         static final Character cache[] = new Character[127 + 1];
2558 
2559         static {
2560             for(int i = 0; i < cache.length; i++)
2561                 cache[i] = new Character((char)i);
2562         }
2563     }
2564 
2565     /**
2566      * Returns a <tt>Character</tt> instance representing the specified
2567      * <tt>char</tt> value.
2568      * If a new <tt>Character</tt> instance is not required, this method
2569      * should generally be used in preference to the constructor
2570      * {@link #Character(char)}, as this method is likely to yield
2571      * significantly better space and time performance by caching
2572      * frequently requested values.
2573      *
2574      * This method will always cache values in the range '&#92;u0000'
2575      * to '&#92;u007f'", inclusive, and may cache other values outside
2576      * of this range.
2577      *
2578      * @param  c a char value.
2579      * @return a <tt>Character</tt> instance representing <tt>c</tt>.
2580      * @since  1.5
2581      */
2582     public static Character valueOf(char c) {
2583         if(c <= 127) { // must cache
2584             return CharacterCache.cache[(int)c];
2585         }
2586         return new Character(c);
2587     }
2588 
2589     /**
2590      * Returns the value of this <code>Character</code> object.
2591      * @return  the primitive <code>char</code> value represented by
2592      *          this object.
2593      */
2594     public char charValue() {
2595         return value;
2596     }
2597