elaborate:详细说明

Data Types
Java categorizes data into different types, and only certain operations
can be performed on a particular type of data.

Data type: A set of values together with a set of operations on those values.
Primitive Data Types
 There are three categories of primitive data types:

• Integral, which is a data type that deals with integers, or numbers
without a decimal part (and characters)
• Floating-point, which is a data type that deals with decimal numbers
• Boolean, which is a data type that deals with logical values
Integral data types are further classified into five categories: char, byte, short, int, and long.

Which data type you use depends on how big a number your program needs to deal with.

In the early days of programming, computers and main memory were very expensive.
Only a small amount of memory was available to execute programs and manipulate data.
As a result, programmers had to optimize the use of memory. 
Table 2-2 gives the range of possible values associated with the five integral data types and
the size of memory allocated to manipulate these values.

java中字符编码采用的是Unicode,所以是16bit而不是c语言里面的8bit

boolean DATA TYPE
The data type boolean has only two values: true and false. The memory allocated for the boolean data type is 1 bit.

FLOATING-POINT DATA TYPES:float or double, the memory allocated for them respectively are 4 byte and 8 byte.
In java, by default, float-point refers to double.

关于负数的取余:http://ceeji.net/blog/mod-in-real/

总结

我们由此可以总结出下面两个结论:

  1. 对于任何同号的两个整数,其取余结果没有争议,所有语言的运算原则都是使商尽可能小
  2. 对于异号的两个整数,C++/Java语言的原则是使商尽可能大,很多新型语言和网页计算器的原则是使商尽可能小。

Character arithmetic: Java allows you to perform arithmetic operations on char data.

class String
A string is a sequence of zero or more characters. Strings in Java are enclosed in double
quotation marks . To process strings effectively, Java provides the

class String. The class String contains various operations to manipulate a string. Technically speaking, the class String is not a primitive type.

A string that contains no characters is called a null or empty string. 
Every character in a string has a specific position in the string. The position of the first
character is 0, the position of the second character is 1, and so on. The length of a string
is the number of characters in it.

Strings and the Operator +
One of the most common operations performed on strings is the concatenation operation,
which allows a string to be appended at the end of another string. The operator +
can be used to concatenate (or join) two strings as well as a string and a numeric value or a
character.

The complete description of this class can be found at the Web site http://java.sun.com/javase/7/docs/api/.

In Java, you can use a named constant to instruct a program to mark those memory locations in which data is constant throughout program execution. Named constant: A memory location whose content is not allowed to change during program execution.

The syntax to declare a named constant is:              static final dataType IDENTIFIER = value;             //注意static是可选的(optional)

The reserved word final specifies that the value stored in the identifier is fixed and cannot be changed. 

 Notice that the identifier for a named constant is in uppercase letters. This is because Java programmers

typically use uppercase letters for a named constant. (If the name of a named constant is a
combination of more than one word, called a run-together-word, then the words are
separated using an underscore; see the next example.)

To put data into variables from the standard input device, Java provides the class Scanner. Using this class, we first create an input stream object and associate it with the standard input device. The following statement accomplishes this:

static Scanner console = new Scanner(System.in);


 The object console reads the next input as follows:
a. If the next input token can be interpreted as an integer, then the expression:
console.nextInt()
retrieves that integer; that is, the value of this expression is that integer.
b. If the next input token can be interpreted as a floating-point number, then
the expression:
console.nextDouble()
retrieves that floating-point number; that is, the value of this expression
is that floating-point number. 
c. The expression:
console.next()
retrieves the next input token as a string; that is, the value of this
expression is the next input string. 

d. The expression:
console.nextLine()
retrieves the next input as a string until the end of the line; that is, the
value of this expression is the next input line. (Note that this expression
also reads the newline character, but the newline character is not stored
as part of the string.)
While scanning for the next input, the expressions console.nextInt(),
console.nextDouble(), and console.next() skip whitespace characters. Whitespace
characters are blanks and certain nonprintable characters, such as newline and tab.

System.in is called a standard input stream object and is designed to input data from the
standard input device. However, the object System.in extracts data in the form of
bytes from the input stream. Therefore, using System.in, we first create a Scanner
object, such as console, as shown previously, so that the data can be extracted in a
desired form.

Reading a Single Character
Suppose the next input is a single printable character, say, A. Further suppose that ch is a
char variable. To input A into ch, you can use the following statement:
ch = console.next().charAt(0);
where console is as declared previously.

Increment and Decrement Operators

Output

The syntax to use the object System.out and the methods print and println is:
System.out.print(expression);
System.out.println(expression);
System.out.println();

Packages, Classes, Methods, and the import Statement

Only a small number of operations, such as arithmetic and assignment operations, are explicitly defined in Java. Many of the methods and identifiers needed to run a Java program are provided as a collection of libraries, called packages. A package is a collection of related classes. Moreover, every package has a name. 

In Java, class is a broadly used term. The term class is used to create Java programs, either application or applet; it is used to group a set of related operations; and it is used to allow users to create their own data types.

 This and other mathematical methods are contained in the class Math. The name of the package containing the class Math is java.lang.

The package java.util contains the class Scanner. This class contains the methods
nextInt, nextDouble, next, and nextLine for inputting data into a program. 
To see the complete definitions of the (predefined) Java classes, such as String, Math,
and Scanner, as well as the class hierarchy, you can visit the Web site http://
java.sun.com/javase/7/docs/api/.

In skeleton form, a Java application program looks like the following:
import statements if any
public class ClassName
{
named constants and/or stream objects declarations
public static void main(String[] args)
{
variable declaration
statements
}
}
Notice that the heading of the method main contains the reserved word static. The
statements to declare the named constants and the input stream objects are placed outside
the definition of the method main. Therefore, to use these named constants and stream
objects in the method main, Java requires that you declare the named constants and the
input stream objects with the reserved word static.

Chapter 2 Basic Elements of JAVA的更多相关文章

  1. [Basic] The most basic things about java

    [Basic] The most basic things about java // */ // ]]>   [Basic] The most basic things about java ...

  2. HTTP基本认证(Basic Authentication)的JAVA实例代码

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息. 但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证. 下面来看看一看这个认证的工作过程: 第一步: 客户端发送 ...

  3. JVM Specification 9th Edition (4) Chapter 3. Compiling for the Java Virtual Machine

    Chapter 3. Compiling for the Java Virtual Machine 内容列表 3.1. Format of Examples 3.2. Use of Constants ...

  4. HTTP基本认证(Basic Authentication)的JAVA示例

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息.但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证.下面来看看一看这个认证的工作过程:第一步:  客户端发送ht ...

  5. HTTP基本认证(Basic Authentication)的JAVA演示样例

    大家在登录站点的时候.大部分时候是通过一个表单提交登录信息.可是有时候浏览器会弹出一个登录验证的对话框.例如以下图,这就是使用HTTP基本认证.以下来看看一看这个认证的工作过程:第一步:  clien ...

  6. [C++ Basic]C++与Java的主要区别

    1.编译运行 java是解释性语言,java程序在运行时类加载器从类路经中加载相关的类,然后java虚拟机读取该类文件的字节,执行相应操作.而C++编译的 时候将程序编译成本地机器码.一般来说java ...

  7. LeetCode算法题-Minimum Moves to Equal Array Elements(Java实现)

    这是悦乐书的第233次更新,第246篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第100题(顺位题号是453).给定大小为n的非空整数数组,找到使所有数组元素相等所需的 ...

  8. LeetCode算法题-Remove Linked List Elements(Java实现)

    这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> ...

  9. Chapter 06—Basic graphs

    三. 柱状图(Histogram) 1. hist():画柱状图 ·breaks(可选项):控制柱状图的小柱子的条数: ·freq=FALSE:基于概率(probability),而非频率(frequ ...

随机推荐

  1. JS学习笔记Day16

    一.匀速运动 保证速度不让用户提供,将速度写到函数中 speed = target-obj.offsetLeft>0 ? 正速度 :负速度 二.缓冲运动 var speed=(target-ob ...

  2. [SDOI2006] 保安站岗

    题目链接 第一遍不知道为什么就爆零了…… 第二遍改了一下策略,思路没变,结果不知道为什么就 A 了??? 树形 DP 经典问题:选择最少点以覆盖树上所有点(边). 对于本题,设 dp[i][0/1][ ...

  3. Self-organizing Maps及其改进算法Neural gas聚类在异常进程事件识别可行性初探

    catalogue . SOM简介 . SOM模型在应用中的设计细节 . SOM功能分析 . Self-Organizing Maps with TensorFlow . SOM在异常进程事件中自动分 ...

  4. elk 中kafka启动脚本和配置文件

    kafka启动脚本和配置文件 # more kafka #!/bin/sh # Init script for kafka ### BEGIN INIT INFO # Provides: kafka ...

  5. CMDB服务器管理系统【s5day91】:资产采集相关问题

    资产采集唯一标识和允许临时修改主机名 class AgentClient(BaseClient): def exec(self): obj = PluginManager() server_dict ...

  6. module 'pip' has no attribute 'main'

    摘录自:http://www.cnblogs.com/Fordestiny/p/8901100.html 问题分析: 问题解决: 找到安装目录下 helpers/packaging_tool.py文件 ...

  7. DirectX11--HLSL中矩阵的内存布局和mul函数探讨

    前言 说实话,我感觉这是一个大坑,不知道为什么要设计成这样混乱的形式. 在我用的时候,以row_major矩阵,并且mul函数以向量左乘矩阵的形式来绘制时的确能够正常显示,并不会有什么感觉.但是也有人 ...

  8. vue-resource的使用,前后端数据交互

    vue-resource的使用,前后端数据交互 1:导入vue与vue-resource的js js下载:   https://pan.baidu.com/s/1fs5QaNwcl2AMEyp_kUg ...

  9. 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges、DBContextTransaction、TransactionScope)

    一. 什么是事务 我们通俗的理解事务就是一系列操作要么全部成功.要么全部失败(不可能存在部分成功,部分失败的情况). 举一个事务在我们日常生活中的经典例子:两张银行卡(甲.乙),甲向乙转钱,整个过程需 ...

  10. Spring-Boot项目部署到单独tomcat运行

    前言: 本文是对学习SpringBoot过程中的笔记,拿最简单的项目进行部署,大家可以进行类比,文章最后会提供部署前和部署后的github地址,用代码做的笔记,可能会很乱,有兴趣的同学可以参考 正文: ...