前言
幫助你快速入門 Dart
Print to Console
Java
System.out.print("Hello, World");
System.out.println("Hello, World");
Dart
print('Hello, World!');
Constants and Variables
Java
String name="LiLy";
final String name="LiLy";
int lineCount;? 默認值是 0
Dart
var name = 'LiLy'? ?OR??String name="LiLy";
final name = 'LiLy';? OR? final String nickname = 'Bobby';
int lineCount;? 默認值是 null
Verify if value is null
Java
if (text != null) {
? ?int length=text.length();
}
Dart
if (text != null) {
? ?int length=text.length();
}
Concatenation of strings
Java
String firstName = "A";
String lastName = "B";
String name = "My name is: " + firstName + " " + lastName;
Dart
String firstName = "A";
String lastName = "B";
String name = "My name is: ${firstName} ${lastName}"?
String name = "My name is: " + firstName + " " + lastName;??
New line in string
Java
String text = "First Line\n" + "Second Line\n" + "Third Line";
Dart
String text = "First Line\nSecond Line\nThird Line";
Ternary Operations
Java
String text = x > 5 ? "x > 5" : "x <= 5";
String message = null;
System.out.print(message != null ? message : "");
Dart
String text = x > 5 ? "x > 5" : "x <= 5";
String message = null;?
print(message != null ? message : "");
Bitwise Operators
Java
final int andResult = a & b;
final int orResult = a | b;
final int xorResult = a ^ b;
final int rightShift = a >> 2;
final int leftShift = a << 2;
final int unsignedRightShift = a >>> 2;
Dart
final int andResult = a & b;?
final int orResult = a | b;?
final int xorResult = a ^ b;?
final int rightShift = a >> 2;?
final int leftShift = a << 2;
最后一個不知道诀艰,官方文檔也沒查到,誰要是知道可以留言,謝謝??
Check the type and casting
Java
if (object instanceof Car) {
}
Dart
if (object is Car) {?
?}
Multiple conditions
Java
if (score >= 0 && score <= 300) {
?}
Dart
同上
Multiple Conditions (Switch case)
Java
int score = // some score;
String grade;
switch (score) {
?case 10:
?case 9:
? ? ? ? ? ? ?grade = "Excellent";
? ? ? ? ? ? ?break;
?case 8:
?case 7:
?case 6:
? ? ? ? ? ? grade = "Good";
? ? ? ? ? ? break;
?case 5:
?case 4:
? ? ? ? ? ? grade = "OK";
? ? ? ? ? ? break;
?case 3:
?case 2:
?case 1:
? ? ? ? ? ? grade = "Fail";
? ? ? ? ? ? break;
?default:
? ? ? ? ? ? ?grade = "Fail";
?}
Dart
同上
For-loops
Java
?for (int i = 1; i <= 10 ; i++) { }
?for (int i = 1; i < 10 ; i++) { }
?for (int i = 10; i >= 0 ; i--) { }
?for (int i = 1; i <= 10 ; i+=2) { }
?for (int i = 10; i >= 0 ; i-=2) { }
?for (String item : collection) { }
?for (Map.Entry entry: map.entrySet()) { }
Dart
for (int i = 1; i <= 10 ; i++) { }
for (int i = 1; i < 10 ; i++) { } ?
for (int i = 10; i >= 0 ; i--) { } ?
for (int i = 1; i <= 10 ; i+=2) { }? ?
for (int i = 10; i >= 0 ; i-=2) { }?
for (String item in collection) { }? OR?collection.forEach((item)=>print(item));
map.forEach((k, v) {
? ? print(' $k and? $v');
});
Collections
Java
final List listOfNumber = Arrays.asList(1, 2, 3, 4);
final Map keyValue = new HashMap();
? ? ? ? ? ? ? ? ? map.put(1, "Amit");
? ? ? ? ? ? ? ? ? map.put(2, "Ali");
? ? ? ? ? ? ? ? ? map.put(3, "Mindorks");
?// Java 9
final List listOfNumber = List.of(1, 2, 3, 4);
final Map keyValue = Map.of(1, "Amit", 2, "Ali", 3, "Mindorks");
Dart
final List listOfNumber = [1, 2, 3, 4];
final Map keyValue = Map();
keyValue[1]="Amit";
keyValue[2]="Ali";
keyValue[3]="Mindorks";
Splitting arrays
Java
String[] splits = "param=car".split("=");
String param = splits[0];
String value = splits[1];
Dart
var splits = "param=car".split('=');
String param = splits[0];?
String value = splits[1];
Defining methods
Java
void doSomething() {
?// logic here
?}
int? getInt(){
? ? ? ? return 2
}
Dart
同上 OR
int? getInt() => 2
Constructors
Java
public class Utils {
? ? private Utils() {
? ? ? ? ? ? // This utility class is not publicly instantiable
? ? }
? ? public static int getScore(int value) {
? ? ? ? ? ? ?return 2 * value;
? ? ?}
?}
Dart
class Utils {? ? ?
? ? ? Utils() {? ? ? ? ? ? ?
? ? ? ? ?// This utility class is not publicly instantiable? ? ?
? ? ? ? }? ? ?
? ? ? static int getScore(int value) {? ? ? ? ? ? ?
? ? ? ? ? ?return 2 * value; ? ?
? ? ? }
?}
Dart中有沒有C++或Java中表示訪問權(quán)限的private卓起、public關(guān)鍵字
凡以“_”(下劃線)開頭的符號(變量稀余、類成艘、函數(shù)等等)都是包內(nèi)可見的,否則是包內(nèi)外都可見的
Defining uninitialized objects
Java
Person person;
Dart
var person;
Enum
Java
public enum Direction {?
? ? ? A,B,C
}
Dart
enum Direction { A,B,C }
Sorting List
Java
List profiles = loadProfiles(context);
Collections.sort(profiles, new Comparator() {
? ? ?@Override
? ? ?public int compare(Profile profile1, Profile profile2) {
? ? ? ? ? ? ? ? ?if (profile1.getAge() > profile2.getAge()) return 1;
? ? ? ? ? ? ? ? ?if (profile1.getAge() < profile2.getAge()) return -1;
? ? ? ? ? ? ? ? ?return 0;
? ? ? ? }
});
Dart
List?profiles = loadProfiles(context);?
profiles.sort((a , b)=>a.age.compareTo(b.age));
總結(jié)
整體比較下來义起,還是特別容易學(xué)的,基本一致师崎。有任何問題默终,歡迎留言一起探討。