The difference between Java syntax and dart syntax

Constructor

Default constructor

Java

  • In Java, if you do not declare a constructor yourself, the class will automatically generate a parameterless constructor. If a parameterized constructor is declared, there will be no default parameterless constructor. At this time, the parameterless constructor needs to be explicitly declared.

dart

  • In Dart, if you do not declare a constructor yourself, the class will automatically generate a default constructor (anonymous parameterless function). The default constructor has no parameters and will call the parameterless constructor of the parent class.
  • If a constructor is declared (with or without parameters), the default constructor will no longer exist unless you declare it explicitly.

Multiple constructors

Java

The number of constructors in Java is determined by parameters, and different constructors will be created according to different parameters.

In Java, a class can have at most one parameterless constructor and one or more parameterized constructors.

dart

The number of constructors in Dart is determined by named constructors.

After declaring the constructor, dart can use named constructors to declare multiple constructors, with or without parameters, and it does not matter whether the parameters are the same. If you do not use named constructors, in dart, the class will only have one constructor

class IsCase{

  late int i ;
  late int j ;

  IsCase(this.i, this.j) : super(90);

  /* Named constructor */
  IsCase.forName(this.i,this.j):super(80);
  IsCase.forSet()){
    i = 100;
    j = 900;
  }
}

Inherit from parent class

Java

In Java, a subclass will automatically inherit the parameterless constructor of the parent class. Unless the parent class only has a parameterized constructor, the subclass will not automatically inherit any constructor.

dart

In Dart, if there is no anonymous parameterless constructor in the parent class, you need to manually call other constructors of the parent class. After the current constructor colon (:) and before the function body, declare the call to the parent class constructor.

class IsCase extends FaCase{

  late int i ;
  late int j ;

  IsCase(this.i, this.j) : super(90);

  /* Named constructor */
  //The name of the constructor ClassName.identifier
  IsCase.forName(this.i,this.j):super(80);
  IsCase.forSet():super(70){
    i = 100;
    j = 900;
    super.a = 9999;
  }
}

class FaCase{
  int a = 100;

  FaCase(this.a);

  void faPrint(){
    print("Variables in FaCase=$a");
  }
}

Inheritance

Similarities

  • Both Java and Dart have single inheritance, and a class can implement multiple interfaces.
  • They all use the extends keyword

Differences

  • The support for multiple inheritance is different. Java has single inheritance, but it can use interfaces to achieve effects similar to multiple inheritance.
  • In Dart, you can use mixins to implement multiple interfaces. By using the with keyword, a class can use one or more mixins and inherit their methods and properties. By using the with keyword, a class can use one or more mixins and inherit their methods and properties.

Abstract class

Common ground

  • An abstract class cannot be instantiated, but its subclasses can
  • Abstract classes, like ordinary classes, have member variables, constructors and methods.
  • Abstract methods in Java and Dart have no concrete implementation. Subclasses of abstract classes must rewrite all abstract methods. In Dart, abstract variables also need to be rewritten. Abstract variables in Dart are essentially abstract getter and setter methods, which require specific implementation in subclasses.

Differences

  • There are no abstract variables in Java, but there are in dart. Abstract variables are defined in Dart by adding the abstract keyword before the variable.
  • Abstract methods in Java must be added with the abstract keyword, but abstract methods in Dart do not need to be added, as long as there is no specific implementation.

Interface

  • There is no interface keyword in Dart to define an interface, but both ordinary classes and abstract classes can be implemented as interfaces, both using the implements keyword.
  • Java can define interfaces and use the implements keyword to implement multiple interfaces.

The difference between inheritance and implementation

  • Inheritance will only override abstract methods in abstract classes
  • The implementation will override variables and methods (abstract, non-abstract) in abstract classes

Exception

Catch exception

try / on / catch / on…catch and finally blocks

tryblock

Package code that may cause exceptions

catch block

Catching an exception prevents the exception from being passed on (unless the exception is rethrowed). You can handle the exception by catching the exception opportunity. Use the on block when you need to specify the exception type. If no exception type is specified, all exceptions will be caught.

  • catch block captures exception objects and processes them.
  • The catch() function can specify 1 to 2 parameters. The first parameter is the thrown exception object, and the second parameter is the stack information (a StackTrace object).
  • If only partial handling of an exception is required, the exception can be rethrown using the keyword rethrow.

on block

  • on…catch block: on and catch can be used at the same time or separately. Use on to specify the exception type, use catch to capture the exception object, and only capture the exception type following on.

finally block

  • Regardless of whether an exception is thrown, the code in finally will be executed. If catch does not match the exception, the exception will be thrown again after finally execution is completed.
  • After any matching catch has completed, execute finally
try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print('cannot be divided by zero');//output: cannot be divided by zero
  }catch (e) {
    print(e);
  }finally{
    print("Always executed, regardless of exceptions");
  }
  • In this code, the on statement specifies to capture exceptions of the IntegerDivisionByZeroException type and execute the corresponding processing statements.
  • If a divide-by-zero exception occurs, the program executes the print statement in the on statement and skips the catch statement.
  • If other types of exceptions occur, the catch statement will capture and execute the corresponding processing statement.
  • The finally statement will always execute regardless of whether an exception occurs.

Custom exception

Every exception type in Dart is a subtype of the built-in class Exception. Dart can create custom exceptions by extending existing exceptions.

// Custom exception
class numNot implements Exception {
  String errMsg() => "Number cannot <100";
}

Throws an exception

Use throw

void checkErro(int a){
  if(a <100){
    throw numNot();
  }
}

The meaning of the above code: when the size of parameter a <100, a custom exception is thrown.

Complete code:

void main() {

  //Exception example 1
  errorCase1();


  //Exception example 2
  try{
    checkError(23);
  }on numNot catch(e){
    print("Error message:${e.errMsg()}");
  }

}

//Exception example 1
void errorCase1(){
  int x = 12;
  int y = 0;
  int res;
  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print('cannot be divided by zero'); // output: cannot be divided by zero
  } catch (e) {
    print(e);
  } finally {
    print("Always executed, regardless of exceptions");
  }
}

void checkError(int a){
  if(a <100){
    throw numNot();
  }
}

// Custom exception
class numNot implements Exception {
  String errMsg() => "Number cannot <100";
}