Swift syntax and Java comparison

Source: Internet
Author: User

1. Defining Constants and variables

A. Swift syntax

Constant:

10

Variable:

1

B. Java syntax

Constant:

 Public Final int Maxnumber= 10;

Variable:

 Public String name = "Morden";

It is important to note that Swift has type notation (type annotation): If you want to add a type callout, you need to precede the constant or variable name with a colon and a space, followed by the type name. But because the Swift compiler can make type-safety decisions, it is generally simple for code,

You can do this without typing, and there are references to type callouts. The syntax for adding type callouts is as follows:

Constant:

10

Variable:

" Morden "

Constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs. You cannot start with a number, but you can include a number elsewhere in a constant and variable name.

2. Output syntax for swift and Java

Swift:

Print ("helloWorld")

Java

System.out.println ("Hello World");

3. Notes

Swift and Java annotations are roughly the same, and can be single-line comments and multiline comments. The only difference is that Swift supports multiple lines of comments that can be nested in other multiline annotations.

/** * This is the end of the first multiline comment * /

4. Semicolon

Java requires a semicolon for each line of code, and Swift does not require each line to have a semicolon, except in one case: when writing multiple independent statements within the same line, each statement requires a semicolon-delimited

Let apple:string  "Apple" ; Print ("helloWorld");

5. Type Callout

Type callout: In swift, it is not always important to explicitly specify a type when declaring constants and variables. If you do not explicitly specify a type, Swift uses type inference (inference) to select the appropriate type. With type inference, the compiler can automatically infer the type of an expression when compiling code. Both Swift and Java are a type-safe language. If you declare that the variable is a string type, and then assign the value, the int type is passed, the compiler will error. But in grammar, the two languages are different.

The Java General Declaration variable, the type is placed in front, the following code:

String name = "Morden";

Swift will put the type in the following code:

String = "Morden"

And sometimes, when we declare a variable without a type, the compiler infers the variable type itself, which is the type callout

In swift, the specific type of callout code is as follows:

42
Number会被推测为 Int 类型
3 0.14159

The original value Anothernumber does not explicitly declare a type, and a floating-point literal appears in the expression, so the expression is inferred as a Double type.

6. Numeric literals

Swift and Java have many similarities and differences in numeric literals, and only decimal and 16 binary representations are the same.

Swift:

    • A decimal number with no prefix
    • A binary number, prefixed by0b
    • An octal number, prefixed by0o
    • A hexadecimal number, prefixed by0x

Java:

    • A decimal number with no prefix
    • A binary number, Java does not have a binary representation, generally 16 binary representation
    • An octal number, prefixed by0
    • A hexadecimal number, prefixed by0x

In addition, the swift numeric class literal can include additional formatting to enhance readability. Both integers and floating-point numbers can be added with an additional 0 and underlined, without affecting the literal:

000123.456== 1_000_000.000_000_1

Reference Links:

Http://special.csdncms.csdn.net/the-swift-programming-language-in-chinese/Introduction/template/chapter2/01_ The_basics.html

Swift syntax and Java comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.