Partager via


Using Value Types Corresponding to Primitive Types 

Visual J# allows you to directly use the primitive class types corresponding to each primitive type that can be found in the .NET Framework (System.Int32 for int). The only restriction is that you need to use a cast to assign a primitive type to its corresponding class type. Similarly you need a cast to assign a primitive type to System.Object. These restrictions are necessary so as not to violate the semantics of the Java language. The casting of the primitive type to the corresponding value type is not transitive.

Example

// vjc_valuetypes2.jsl
import System.*;
public class MyClass
{
   public static void main(String [] args)
   {
      Int32 int32 = (Int32) 10;
      int i = (int) int32;
      Console.WriteLine(i);
      System.Object obj1 = int32;   // Ok. int32 is a value type.
      Console.WriteLine(obj1);
      System.Object obj2 = (Int32) 10;   // Will also work.
      Console.WriteLine(obj2);

      // The following statements will not compile,
      // a primitive type cannot be assigned to a reference type:
      // System.Int32 int32 = 10;
      // System.Object obj = 10;
   }
}

Output

10
10
10

The .NET Framework supports some primitive types that are not part of the Java language. You can use these types by using the corresponding value type from the .NET Framework as described above, and the Visual J# compiler generates the right code for taking care of the conversions. The following table gives a list of such types:

.NET Framework primitive type not supported by Visual J# Corresponding value type in .NET Framework

unsigned short or uint16

System.UInt16

unsigned int or uint32

System.UInt32

unsigned long or uint64

System.UInt64

native int

System.IntPtr

native unsigned int

System.UIntPtr

See Also

Reference

Syntax for Targeting the .NET Framework
Property and Event Exposure