BFOIT Intro to Programming Class - Type Mismatch

The BFOIT Introduction to Programming Class
Example of Type Mismatch Error With Method Arguments


Introduction

The Java compiler verifies that there are no type conflicts in programs.  The following examples illustrate correct and mismatched type of arguments to methods.


println( ) Accepts Arguments of Many Types

The following two figures show that println( ) knows what to do with the value 1, both as a number (of type int) and as a String.  The program compiles with no errors.  And, when I ran it, it output two lines displaying 1 on each.  In fact, when displayed, you can't tell that they were of different types in the program.


  class ones
  {
     public static void main( String[] args )  
     {
        System.out.println( 1 );
        System.out.println( "1" );
     }

  } // end class ones
    
Source with println( ) Invoked With Two Different Types


  C:\My Documents\Intro_to_Programming>javac ones.java  

  C:\My Documents\Intro_to_Programming>java ones
  1
  1
    
Output From Compiling and Running It


Using Methods Which Accept Arguments of One Type Correctly

In the following two figures, you can see that when methods that only accept arguments of a single specific type are invoked properly, the output matches my first example.  First, I declared two new methods (print_string( ) and print_int( )) that each will only take specific type.  Then, in main( ), I invoke the methods properly.


  class ones
  {
     static void print_string( String text )
     {
        System.out.println( text );
     }

     static void print_int( int number )
     {
        System.out.println( number );
     }

     public static void main( String[] args )  
     {
        print_int( 1 );
        print_string( "1" );
     }

  } // end class ones
    
Source with Methods Accepting Specific Types, Invoked Properly


  C:\My Documents\Intro_to_Programming>javac ones.java  

  C:\My Documents\Intro_to_Programming>java ones
  1
  1
    
Output From Compiling and Running It


Trying to Improperly Invoke Methods Which Accept Arguments of One Type

Finally, checkout what happens when I swap the arguments.  In the following source, I've attempted to invoke print_int( ) with the String ("1") and print_string( ) with the int 1.  As you can see, the Java compiler catches both of the mistakes.


  class ones
  {
     static void print_string( String text )
     {
        System.out.println( text );
     }

     static void print_int( int number )
     {
        System.out.println( number );
     }

     public static void main( String[] args )
     {
        print_int( "1" );
        print_string( 1 );
     }

  } // end class ones
    
Source with Methods Accepting Specific Types, Bad Invocations


  C:\My Documents\Intro_to_Programming>javac ones.java
  ones.java:15: print_int(int) in ones cannot be applied to (java.lang.String)
        print_int( "1" );
        ^
  ones.java:16: print_string(java.lang.String) in ones cannot be applied to (int)  
        print_string( 1 );
        ^
  2 errors
    
Output From Compiling and Running It