为了账号安全,请及时绑定邮箱和手机立即绑定

From Java to Dart

标签:
Java

前言

帮助你快速入门 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中表示访问权限的private、public关键字
凡以“_”(下划线)开头的符号(变量、类、函数等等)都是包内可见的,否则是包内外都可见的

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));



作者:i校长
链接:https://www.jianshu.com/p/c33b54ce27ad


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消