Advanced usage (1a):    Checking for uppercase letters

  • According to the Unicode table, the Unicode for uppercase letters are:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      'A' 'B' .. 'Z'    65 66 .. 90              \u0041 \u0042 .. \u005A 

  • How to test if a char variable c contains an uppercase letter:

      if ( 65 <= c && c <= 90 )
         System.out.println("is an uppercase letter");
    
     Better:
    
      if ( 'A' <= c && c <= 'Z' )
         System.out.println("is an uppercase letter");
      

  • The Java library method Character.isUpperCase(c) does the same thing !

DEMO: demo/04-Math+String/03-adv-ops/IsUpperCase.java

Advanced usage (1b):    Checking for lower case letters

  • According to the Unicode table, the Unicode for lower case letters are:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      'a' 'b' .. 'z'    97 98 .. 122             \u0061 \u0062 .. \u007A 

  • How to test if a char variable c contains a lowercase letter:

      if ( 97 <= c && c <= 122 )
         System.out.println("is a lower case letter");
    
     Better:
    
      if ( 'a' <= c && c <= 'z' )
         System.out.println("is a lower case letter");
      

  • The Java library method Character.isLowerCase(c) does the same thing !

DEMO: demo/04-Math+String/03-adv-ops/IsLowerCase.java

Advanced usage (1c):    Checking for digit symbols

  • According to the Unicode table, the Unicode for digit symbols are:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      '0' '1' .. '9'    48 49 .. 57              \u0030 \u0031 .. \u0039 

  • How to test if a char variable c contains a digit symbol:

      if ( 48 <= c && c <= 57 )
         System.out.println("is a digit symbol");
    
     Better:
    
      if ( '0' <= c && c <= '9' )
         System.out.println("is a digit symbol");
      

  • The Java library method Character.isDigit(c) does the same thing !

DEMO: demo/04-Math+String/03-adv-ops/IsDigit.java

Advanced usage (2):    get the next symbol

  • The Unicode for the symbols are numbers:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      'A' 'B' .. 'Z'    65 66 .. 90              \u0041 \u0042 .. \u005A
      'a' 'b' .. 'z'    97 98 .. 122             \u0061 \u0062 .. \u007A

  • You can compute with numbers that are Unicodes:

      Example:   66 = 65 + 1 or: 'B' = 'A' + 1, and so on
    

  • How to get the symbol/character that follows the character c:

      char c;
    
      c = (char) (c + 1);   // We must cast because + result is int  

DEMO: demo/04-Math+String/03-adv-ops/NextSymbol.java

Advanced usage (3a):    convert uppercase letter → lowercase letter

  • The Unicode for the letters are:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      'A' 'B' .. 'Z'    65 66 .. 90              \u0041 \u0042 .. \u005A
      'a' 'b' .. 'z'    97 98 .. 122             \u0061 \u0062 .. \u007A
    
      Notice:  'a' - 'A' = 32
    
      97 (='a')  =  65 (='A') + 32   =  65 (='A') + ('a' - 'A')
      98 (='b')  =  66 (='B') + 32   =  66 (='B') + ('a' - 'A')
      and so on

  • How to convert an upper case character c to its lowercase:

      c = (char) (c - 'A' + 'a');   // Now c = Unicode for the lowercase

  • The Java library method c = Character.toLowerCase(c) does the same thing !

DEMO: demo/04-Math+String/03-adv-ops/ToLowerCase.java

Advanced usage (3b):    convert lowercase letter → uppercase letter

  • The Unicode for the letters are:

      Characters        Code value in decimal    Unicode value
     -------------------------------------------------------------------
      'A' 'B' .. 'Z'    65 66 .. 90              \u0041 \u0042 .. \u005A
      'a' 'b' .. 'z'    97 98 .. 122             \u0061 \u0062 .. \u007A
    
      Notice:  'A' - 'a' = -32
    
      65 (='A')  =  97 (='a') + -32   =  97 (='A') + ('A' - 'a')
      66 (='B')  =  98 (='b') + -32   =  98 (='B') + ('A' - 'a')
      and so on

  • How to convert an lower case character c to its upper case:

      c = (char) (c - 'a' + 'A');   // Now c = Unicode for the uppercase

  • The Java library method c = Character.toUpperCase(c) does the same thing !

DEMO: demo/04-Math+String/03-adv-ops/ToUpperCase.java

Quiz 1

Write a Java program that converts a character variable c to its upper case character only if c contains a lower case letter

I.e.: if the variable c does not contains a lower case letter, the program not change c

public class Quiz 
{
   public static void main(String[] args) 
   {
     char c = '4';
     
     if ( 'a' <= c && c <= 'z' )
         c = (char) (c - 32);
 
     System.out.println(c);     
   }
} 

Quiz 1

Write a Java program that converts a character variable c to its upper case character only if c contains a lower case letter

I.e.: if the variable c does not contains a lower case letter, the program not change c

public class Quiz 
{
   public static void main(String[] args) 
   {
     char c = '4';
     
     if ( 'a' <= c && c <= 'z' )
         c = (char) (c - 'a' + 'A');
 
     System.out.println(c);     
   }
} 

DEMO: demo/04-Math+String/03-adv-ops/Quiz1.java

Quiz 2

What is printed by each of the println( ) statement:

public class MyProg 
{
   public static void main(String[] args) 
   {
      System.out.println( 65 );              // Unicode 65 = 'A'
      System.out.println( (char) 65 );       // Unicode 65 = 'A'
      System.out.println( 'A'+1 );           // + always returns int !
      System.out.println( (char) ('A'+1) );
      
      System.out.println( 'A' < 'F' );
      System.out.println( 'X' < 'F' );
      System.out.println( 'A' < 'a' );
      System.out.println( 'Z' < 'a' );

      char c = 'x';
      c++;
      System.out.println(c);
   }
} 
  

Quiz 2

What is printed by each of the println( ) statement:

public class MyProg 
{
   public static void main(String[] args) 
   {
      System.out.println( 65 );    65        // Unicode 65 = 'A'
      System.out.println( (char) 65 );  A    // Unicode 65 = 'A'
      System.out.println( 'A'+1 );  66       // + always returns int !
      System.out.println( (char) ('A'+1) );  B
      
      System.out.println( 'A' < 'F' );   true  (65 < 70)
      System.out.println( 'X' < 'F' );   false (88 ≥ 70)
      System.out.println( 'A' < 'a' );   true  (65 < 97)
      System.out.println( 'Z' < 'a' );   true  (90 < 97)

      char c = 'x';
      c++;
      System.out.println(c);   y
   }
} 
  

DEMO: demo/04-Math+String/03-adv-ops/Quiz2.java