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.

30天代码day1Data Types的更多相关文章

  1. 30行代码搞定WCF并发性能测试

    [以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main()         {               List< ...

  2. 30行代码让你理解angular依赖注入:angular 依赖注入原理

    依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...

  3. 30行代码实现Javascript中的MVC

    从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...

  4. Tensorflow快餐教程(1) - 30行代码搞定手写识别

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...

  5. 10分钟教你用python 30行代码搞定简单手写识别!

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...

  6. JAVA_SE基础——30.构造代码块

    黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...

  7. 30天代码day4 Class vs. Instance

    Class A blueprint defining the charactaristics and behaviors of an object of that class type. Class ...

  8. 30天代码day2 Operators

    Operators These allow you to perform certain operations on your data. There are 3 basic types: Unary ...

  9. 30 行代码实现 JS 中的 MVC

    一连串的名字走马观花式的出现和更迭,它们中一些已经渐渐淡出了大家的视野,一些还在迅速茁壮成长,一些则已经在特定的生态环境中独当一面舍我其谁.但不论如何,MVC已经并将持续深刻地影响前端工程师们的思维方 ...

随机推荐

  1. sqlserver 数据库关于存储xml字段里内容的查找与替换

    1.传送门 :   http://www.cnblogs.com/GuoPeng/archive/2009/12/11/1621527.html 2.复制原帖的修改部分: 修改:@xml . modi ...

  2. 关于人人网的form查找和打印

    from lianxi import sessionimport json# urlurl = 'http://www.renren.com/ajaxLogin/login?1=1&uniqu ...

  3. lamba匿名函数与内置函数/递归

    max 循环出的参数给func,func的返回值给key,然后比较那个返回值max(salary,key=func) 匿名函数:max(salary,key=lamba x:salaries[x]) ...

  4. java接口特性

    java接口特性 (1)接口可以被实例化 ,常作为类型使用 (2)实现类必须实现接口的所有方法 ,抽象类除外 (3)实现类可以实现多个几口   java中的多继承 (4)接口中的变量都是静态变量

  5. _net之美 记录笔记

    第一章: Valpoint vPoint1=new Valpoint(); Console.WriteLine(vPoint1.x); 1.=号左边ValPoint vPoint1,在线程上创建一个V ...

  6. Python_Mix*内置函数

    数学运算(7个) abs()求数值的绝对值 divmod()返回两个数值的商和余数 max()返回可迭代对象中的元素中的最大值或者所有参数的最大值 min()返回可迭代对象中的元素中的最小值或者所有参 ...

  7. ES6中var/let/const的区别

    let的含义及let与var的区别: let 声明的变量只在它所在的代码块有效: 如下: for (let i = 0; i < 10; i++) { console.log(i); } con ...

  8. java基础3(二)

    基础加强 类加载器 A.类加载器获取classpath下任意内容 注意: 可以通过ClassLoader可以获取classpath下的所有内容. SpringMVC 1.整体架构 流程图 2.流程细节 ...

  9. 嵌入式Linux系统的构成和启动过程

    转自:http://blog.csdn.net/weiganyi/article/details/11561859 在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机 ...

  10. 怎么获取红米6 Pro的root权限

    红米6 Pro能有啥方法获得ROOT超级权限?做开发的人知道,android设备有ROOT超级权限,如果手机获得root相关权限,就能够实现更强大的功能,举例子,做开发的人部门的营销部门的妹子,使用一 ...