[Java] Introduction to Java Programming 笔记: Chapter 2. 基础

  • 每个Java程序必须有一个main作为程序执行的入口。
  • System.out指向输出,System.in指向输入, 从键盘获得输入的方法:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
double x = input.nextDouble(); // 读取一个double
  • 计算3个数的平均数的例子, ComputeAverage.java:
import java.util.*;
public class ComputeAverage {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter three numbers: ");
        double num1 = input.nextDouble();
        double num2 = input.nextDouble();
        double num3 = input.nextDouble();
        System.out.println("The average of three numbers: " + (num1 + num2 + num3) / 3);
    }
}
  • 下面的两种写法,没有任何性能差别:
import java.util.Scanner;
import java.util.*;
  • Java的标识符可以有$字符,但是不要使用$,因为这个字符依照惯例仅用于自动生成的源代码,除此之外,标识符的命名规则和C,C++一样。

  • 变量的声明和初始化:
    int i = 1, j = 2;

  • 声明常量的语法如下, C++ 里的const 改成了关键字final

final datatype CONSTANTNAME = value;
  • 常量声明的同时必须初始化,例如:
final double PI = 3.14159; 
  • 命名惯例:
    变量,方法小写,连接起来的第二个单词首字母大写
    类名首字母大写
    常量名所有字母大写,单词之间使用下划线连接,例如: PIMAX_VALUE.
public static void main(String[] args) {
    double miles = 100;
    final double KILOMETERS_PER_MILE = 1.609;
    double kilometers = miles * KILOMETERS_PER_MILE;
    System.out.println(kilometers);
}
  • Java 有 6种数字数据类型: byte, short, int, long, float, double, 位数为 8, 16, 32, 64, 32-bit IEEE 754, 64-bit IEEE 754, 全部都是有符号,可表示正数,也可表示负数。前4种表示整数,后两种表示浮点,doublefloat更精确,一般应选double为数据类型。

  • 从键盘读取数字,Scanner 对象的6种方法:

MethodDescription
nextByte()读取byte整型.
nextShort()读取short整型.
nextInt()读取int整型.
nextLong()读取long整型.
nextFloat()读取float类型值.
nextDouble()读取double类型值.

- %运算符可用于负数,结果的符合与左操作数相同(dividend)。
- 指数运算: Math.pow(a, b)
- 整形字面值:如果表示的数太大,超过int表示范围,则在数字后面加L或l,表示为long类型,通常用大写的L,小写的l很容易和数字1看混淆。
- 在数字前加0b或0B表示为二进制数,加0表示8进制数,加0x或0X表示16进制数。

System.out.println(0B1111); // Displays 15
System.out.println(07777); // Displays 4095
System.out.println(0XFFFF); // Displays 65535
  • 浮点字面值默认情况下看作double,而非float,例如 5.0 就被看作double,如果要指定数值是float还是double,可以在数字后面加 f, F, 或 d,D,例如 100.2f, 100.2D, doublefloat 更精确。
  • 科学计数: 50.534 -> 5.0534E+1, float 或 double 之所以被称为浮点,是因为内部以科学计数法存储,小数点移动到了新的位置(float)。
  • 为提高可读性,Java 允许在字面值之间加下划线,但是只能是在两个数的中间。45_ 或 _45都不对,如下, 但是这一个语法只有 1.7 及以上才支持。
long ssn = 232_45_4519;
long creditCardNumber = 2324_4545_4519_3415L;
  • How many accurate digits are stored in a float or double type variable? I dont know.
  • 显示当前时间,这时间和电脑上的一模一样:
public class ShowCurrentTime {
    public static void main(String[] args) {
        long total = System.currentTimeMillis();
        long totalSeconds = total / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        long currentHour = (totalHours + 8) % 24;
        System.out.println("Current Time: " + currentHour + ":"
                + currentMinute + ":" + currentSecond);         
    }
}
// output: Current Time: 15:9:29
  • x /= 4 + 5.5 * 1.5; 等同于 x = x / (4 + 5.5 * 1.5); /= 最后执行。

  • i++ 读作 i plus plus ,i— — as i minus minus, 先加后减这些,和 C/C++ 里的完全一样。实际中 int k = ++i + i; 这样的写法不是好的写法,看起来麻烦,容易出错。

  • Java 里的6种数值类型的表数范围有的窄,有的宽。如果将窄类型的值赋给宽类型的值,Java会自动进行类型转换,叫做拓宽类型(widening a type), 反过来缩窄类型(narrowing a type)则需要进行显式数据类型转换,其他的和C/C++ 一样。

int i = 1;
byte b = i; // 错误,要求显式转换
  • 关于近似值,浮点近似存储,但整数精确存储,因此整数计算能得到精确的结果。
This book is a brief version of Introduction to Java Programming, Comprehensive Version, 8E. This version is designed for an introductory programming course, commonly known as CS1. This version contains the first twenty chapters in the comprehensive version. This book uses the fundamentals-first approach and teaches programming concepts and techniques in a problem-driven way. The fundamentals-first approach introduces basic programming concepts and techniques before objects and classes. My own experience, confirmed by the experiences of many colleagues, demonstrates that new programmers in order to succeed must learn basic logic and fundamental programming techniques such as loops and stepwise refinement. The fundamental concepts and techniques of loops, methods, and arrays are the foundation for programming. Building the foundation prepares students to learn object-oriented programming, GUI, database, and Web programming. Problem-driven means focused on problem-solving rather than syntax. We make introductory programming interesting by using interesting problems. The central thread of this book is on solving problems. Appropriate syntax and library are introduced to support the writing of a program for solving the problems. To support teaching programming in a problemdriven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. In order to appeal to students in all majors, the problems cover many application areas in math, science, business, financials, gaming, animation, and multimedia. 精美文字版pdf电子书。
本书是Java语言的经典教材,中文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发中遇到的问题。您手中的这本是其中的基础篇,主要介绍了基本程序设计、语法结构、面向对象程序设计、继承和多态、异常处理和文本I/O、抽象类和接口等内容。本书可作为高等院校程序设计相关专业的基础教材,也可作为Java语言及编程开发爱好者的参考资料。 作者:(美国)粱勇(Y.Daniel Liang) 译者:戴开宇 梁勇(Y.Daniel Liang),现为阿姆斯特朗亚特兰大州立大学计算机科学系教授。之前曾是普度大学计算机科学系副教授。并两次获得普度大学杰出研究奖。他所编写的Java教程在美国大学Java课程中采用率极高。同时他还兼任Prentice Hall Java系列丛书的编辑。他是“Java Champion”荣誉得主,并在世界各地为在校学生和程序员做JAVA程序设计方法及技术方面的讲座。 戴开宇,复旦大学软件学院教师,工程硕士导师。中国计算机学会会员。博士毕业于上海交通大学计算机应用专业,2011~2012年在美国佛罗里达大学作访问学者。承担多门本科专业课程、通识教育课程以及工程硕士课程,这些课程被评为校精品课程,上海市重点建设课程,IBM—教育部精品课程等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值