2- Dart Basics for Java Developers

image 2

Play Store Application link – Java to Dart in 10 Steps – App on Google Play

Welcome back! Now that you have a basic understanding of Dart, let’s dive into the Dart basics. If you know Java, you’ll find many similarities in Dart, but with some modern twists. We’ll cover Dart’s syntax, control structures, and functions.

1. Language Syntax

Basic Syntax and Structure

Dart’s syntax is designed to be clean and easy to read. Here’s a simple comparison between Java and Dart:

  • Java: public class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello, World!");
    }
    }
  • Dart: void main() {
    print('Hello, World!');
    }

In Dart, the main function is the entry point of the application, similar to Java’s main method, but you don’t need to define a class for it. This makes Dart code more concise.

Data Types and Variables

Dart, like Java, is strongly typed. Here’s how you declare variables and data types in Dart:

  • Java: int age = 30; String name = "John";
  • Dart: int age = 30; String name = "John";

Dart supports several data types like int, double, String, bool, and more. You can also use var for type inference if you don’t want to specify the type explicitly:

  • Dart: var age = 30; // Dart infers that age is an int
    var name = "John"; // Dart infers that name is a String

Operators and Expressions

Operators in Dart are quite similar to Java. You have arithmetic operators, comparison operators, and logical operators:

  • Java:javaCopy codeint sum = 10 + 5; boolean isEqual = (5 == 5);
  • Dart:dartCopy codeint sum = 10 + 5; bool isEqual = (5 == 5);

You’ll also find Dart’s operators and expressions familiar from your Java experience.

2. Control Structures

If-Else Statements

Dart uses if, else if, and else statements, just like Java:

  • Java: if (age > 18) {
    System.out.println("Adult");
    } else {
    System.out.println("Not an adult");
    }
  • Dart: if (age > 18) {
    print("Adult");
    } else {
    print("Not an adult");
    }

Switch Statements

Dart’s switch statement is similar to Java’s, allowing you to handle multiple conditions:

  • Java: switch (day) {
    case 1: System.out.println("Monday");
    break;
    case 2: System.out.println("Tuesday");
    break;
    default: System.out.println("Weekend");
    }
  • Dart: switch (day) {
    case 1: print("Monday");
    break;
    case 2: print("Tuesday");
    break;
    default: print("Weekend");
    }

Loops (for, while, do-while)

Loops in Dart are also similar to Java:

  • Java For Loop: for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
  • Dart For Loop: for (int i = 0; i < 5; i++) {
    print(i);
    }
  • Java While Loop: int i = 0; while (i < 5) {
    System.out.println(i);
    i++;
    }
  • Dart While Loop: int i = 0; while (i < 5) {
    print(i);
    i++;
    }
  • Java Do-While Loop: int i = 0;
    do {
    System.out.println(i);
    i++;
    } while (i < 5);
  • Dart Do-While Loop: int i = 0;
    do {
    print(i);
    i++;
    } while (i < 5);

3. Functions

Defining Functions

Functions in Dart are similar to Java but with a more concise syntax:

  • Java: public static int add(int a, int b) {
    return a + b;
    }
  • Dart: int add(int a, int b) {
    return a + b;
    }

Function Parameters and Return Types

In Dart, you define parameters and return types just like in Java:

  • Java: public static int multiply(int a, int b) { return a * b; }
  • Dart: int multiply(int a, int b) { return a * b; }

Anonymous Functions and Arrow Functions

Dart also supports anonymous functions and arrow functions, similar to Java’s lambda expressions:

  • Java Lambda: Function<Integer, Integer> square = x -> x * x;
  • Dart Arrow Function: var square = (int x) => x * x;

Arrow functions in Dart are a shorthand for functions with a single expression.

Leave a Reply

Your email address will not be published. Required fields are marked *