• 30天代码day1Data Types


    Primitive Data Types

    Java has 8 primitive data types: byte, short, int, long, float, double, boolean, and char. For most challenges, you'll only need to concern yourselves with ints (e.g.: 1, 2, 3, etc.) and doubles (e.g.: 1.0, 2.5, 3.9, etc.). Another important data type we mentioned yesterday is the String class, whose objects are immutable(不可变) strings of characters.

    Scanner

    Yesterday, we discussed Scanner's next, nextLine, hasNext, and hasNextLine methods. Scanner also has readNext and hasNext methods for different data types, which is very helpful when you know exactly what type of input you'll be reading. The next methods scan for tokens (you can think of this as a word), and the nextLine methods reads from the Scanner's current location until the beginning of the next line. For example, nextInt() will scan the next token of input as an int, and nextDouble() will scan the next token of input as a double. You should only ever use 1 scanner object for your entire program.

    Each line of multi-line input contains an invisible separator indicating that the end of a line of input has been reached. When you use Scanner functions that read tokens (e.g.: next(), nextInt(), etc.), the Scanner reads and returns the next token. When you read an entire line (i.e.: readLine()), it reads from the current position until the beginning of the next line. Because of this, a call to nextLine() may return an empty string if there are no characters between the end of the last read and the beginning of the next line. For example, given the following input:

    a b c
    d e
    f
    g
    

    The breakdown below shows how a certain sequence of calls to a Scanner object, , will read the above input:

    1. A call to scan.next(); returns the next token, a.
    2. A call to scan.next(); returns the next token, b.
    3. A call to scan.nextLine(); returns the next token, c. It's important to note that the scanner returns a space and a letter, because it's reading from the end of the last token until the beginning of the next line.
    4. A call to scan.nextLine(); returns the contents of the next line, d e.
    5. A call to scan.next(); returns the next token, f.
    6. A call to scan.nextLine(); returns everything after f until the beginning of the next line; because there are no characters there, it returns an empty String.
    7. A call to scan.nextLine(); returns g.

    Note: a token (single word) 

    Additive Operator

    The + operator is used for mathematical addition and String concatenation (i.e.: combining two Strings into one new String). If you add the contents of two variables together (e.g.: a + b), you can assign their result to another variable using the assignment operator (=). You can also pass the result to a function instead of assigning it to a variable; for example, if a = 1 and b = 2, System.out.println(a + b); will print 3 on a new line.

  • 相关阅读:
    python的第三方库
    安装setuptools
    UnicodeDecodeError异常
    Puppeteer之爬虫入门
    python实时得到鼠标的位置
    下载ez_setup
    下载pywinauto
    linux环境下创建domain
    git常用操作
    maven添加本地jar到本地仓库
  • 原文地址:https://www.cnblogs.com/helloworld7/p/10454438.html
Copyright © 2020-2023  润新知