(FRONT) FRONT (2016)

Java Generic, Constraints, Narrowing and Widening conversions

1. Java generic

Generics allow you to write type-safe code that works with different types without needing to know the exact type beforehand. They introduce type parameters (like ) that act as placeholders for the actual types used in your code.

How to Use Generics:

Example in Java


   1:  import java.util.ArrayList;
   2:  import java.util.List;
   3:   
   4:  public class GenericExample {
   5:   
   6:      public static void main(String[] args) {
   7:          // Generic List of Integers
   8:          List<Integer> intList = new ArrayList<>();
   9:          intList.add(10);
  10:          intList.add(20);
  11:          intList.add(30);
  12:   
  13:          // Generic List of Strings
  14:          List<String> stringList = new ArrayList<>();
  15:          stringList.add("Hello");
  16:          stringList.add("World");
  17:          stringList.add("!");
  18:   
  19:          // Generic method to print any type
  20:          printList(intList);
  21:          printList(stringList);
  22:      }
  23:   
  24:      public static <T> void printList(List<T> list) {
  25:          for (T item : list) {
  26:              System.out.print(item + " ");
  27:          }
  28:          System.out.println();
  29:      }
  30:  }

Example in C#


   1:  using System;
   2:  using System.Collections.Generic;
   3:   
   4:  public class GenericExample {
   5:      public static void Main(string[] args) {
   6:          // Generic List of Integers
   7:          List<int> intList = new List<int>();
   8:          intList.Add(10);
   9:          intList.Add(20);
  10:          intList.Add(30);
  11:   
  12:          // Generic List of Strings
  13:          List<string> stringList = new List<string>();
  14:          stringList.Add("Hello");
  15:          stringList.Add("World");
  16:          stringList.Add("!");
  17:   
  18:          // Generic method to print any type
  19:          PrintList(intList);PrintList(stringList);
  20:      }
  21:   
  22:      public static void PrintList<T>(List<T> list) {
  23:          foreach (T item in list) {
  24:              Console.Write(item + " ");
  25:          }
  26:          Console.WriteLine();
  27:      }
  28:  }

Generics in JavaScript:


JavaScript doesn't have true generics in the same way that Java and C# do. However, TypeScript, a superset of JavaScript, introduces generics.

In JavaScript, you can achieve similar results using dynamic typing and functions that accept any type of argument.

How to Use Generics:


   1:  class GenericExample {
   2:      private items: T[] = [];
   3:      addItem(item: T): void {
   4:          this.items.push(item);
   5:      }
   6:      getItems(): T[] {
   7:          return this.items;
   8:      }
   9:  } 

Summary:

2. Type Conversions in General: Java vs. C#

,
  • Java is more strict about type conversions. It generally requires explicit casting when converting between incompatible types.
  • Java distinguishes between primitive types (like int, float, boolean) and reference types (like String, custom classes). Conversions between these categories are handled differently.
  • Java has built-in methods for some conversions (e.g., Integer.parseInt(), String.valueOf()) when dealing with primitive wrappers or strings.
  • C# is more flexible with type conversions. It supports both implicit and explicit conversions.
  • C# also distinguishes between value types (structs) and reference types (classes).
  • C# provides a wider range of built-in conversion methods (e.g., Convert.ToInt32(), ToString()).
  • C# allows user-defined conversions using the implicit and explicit keywords, which can be very powerful.
  • Narrowing and Widening Conversions
  • Widening Conversion (Implicit Conversion):
  • Definition: Converting a smaller data type to a larger data type without loss of information.
  • Java automatically performs widening conversions between compatible primitive types. For example, an int can be implicitly converted to a long because long can hold all the values of an int.
  • Widening conversions also occur between primitive types and their corresponding wrapper classes.
  • Widening conversions can also occur between reference types, but this is less common.
  • C# also automatically performs widening conversions. For example, an int can be implicitly converted to a long.
  • C# also allows user-defined widening conversions using the implicit keyword.
  • Widening conversions can occur between reference types, but this is less common.
  • Narrowing Conversion (Explicit Conversion):
  • Definition: Converting a larger data type to a smaller data type, potentially losing information.
  • Java requires explicit casting for narrowing conversions. For example, you must use (int) to convert a long to an int.
  • Narrowing conversions can also occur between primitive types and their corresponding wrapper classes.
  • Narrowing conversions can occur between reference types, but this is less common.
  • C# requires explicit casting for narrowing conversions. For example, you must use (int) to convert a long to an int.
  • C# also allows user-defined narrowing conversions using the explicit keyword.
  • Narrowing conversions can occur between reference types, but this is less common.

So, Key Differences Summarized


Here are some simple examples to illustrate the concepts:

Summary, C# offers more flexibility in type conversions, including implicit conversions and user-defined conversions. Java is more strict, generally requiring explicit casting. Both languages support widening and narrowing conversions, but the specific mechanisms differ. C# provides a broader range of built-in conversion methods.

3. C# generic constraints

In C#, when you define a generic method or class, you can specify constraints on the type parameters. These constraints allow you to restrict the types that can be used as type arguments. When you want to ensure that different types passed to a generic function share a common interface, you can use constraints to enforce that.

4. Java generic constraints

In Java, the mechanism for achieving something similar to C#'s generic constraints is called bounded type parameters. Here's a breakdown of how it works in Java:




Java context:



Comments ( )
Link to this page: http://www.vb-net.com/Java/Index04.htm
< THANKS ME>