[Java] Introduction to Java Programming 笔记:Chapter 3. 选择

博客介绍了Java代码相关要点,声明时需初始化,否则编译器会有赋值疑问;调用方法终止程序有正常和异常之分;表达式值类型有规定,后值须为常量;介绍异或、短路操作符等,还提及运算符优先级和结合性,最后介绍Java调试器JDB。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 类似下面的代码, x在声明时必须初始化,否则编译器会怀疑在输出x值时,有可能没有赋值,因为中间的if不一定100%执行。
public static void main(String[] args) {
    double x = 0;  // 必须初始化
    if (true) {
    // ....
    }
    System.out.println(x);
}
  • 调用方法 System.Exit(Status)终止程序,Status0 表示正常终止,非零表示异常终止。

  • switch表达式的值的类型只能为 short, int, byte, String, char 5 种, case后的值必须为常量,不能含有变量如x + 1, break 一般不省略,如果是故意省略,最好在代码中加注释说明。

  • 异或操作符 ^, 当且仅当左右操作符不同时为truep1 ^ p2等价于p1 != p2

  • ||&& 称为短路操作符或懒惰操作符 (short-circuit or lazy operators),先对左操作数求值,再决定是否对右操作数求值。

  • (x > 0 && x < 10) 等价于 ((x > 0) && (x < 10))
    Is (x > 0 || x < 10) 等价于 ((x > 0) || (x < 10))
    Is (x > 0 || x < 10 && y < 0) 等价于(x > 0 || (x < 10 && y < 0))
    实际中应使用简洁的写法,不应加那么多括号。

  • 运算符的优先级看起来和C++基本相同,或许会细微差别。赋值运算符的结合性从右到左,因为从左到右没有意义,其他多数都是从左到右。
  • Java 的调试器叫JDB,是一个独立的Java程序,IDE都集成了调试器。
本书是Java语言的经典教材,中文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发中遇到的问题。您手中的这本是其中的基础篇,主要介绍了基本程序设计、语法结构、面向对象程序设计、继承和多态、异常处理和文本I/O、抽象类和接口等内容。本书可作为高等院校程序设计相关专业的基础教材,也可作为Java语言及编程开发爱好者的参考资料。 作者:(美国)粱勇(Y.Daniel Liang) 译者:戴开宇 梁勇(Y.Daniel Liang),现为阿姆斯特朗亚特兰大州立大学计算机科学系教授。之前曾是普度大学计算机科学系副教授。并两次获得普度大学杰出研究奖。他所编写的Java教程在美国大学Java课程中采用率极高。同时他还兼任Prentice Hall Java系列丛书的编辑。他是“Java Champion”荣誉得主,并在世界各地为在校学生和程序员做JAVA程序设计方法及技术方面的讲座。 戴开宇,复旦大学软件学院教师,工程硕士导师。中国计算机学会会员。博士毕业于上海交通大学计算机应用专业,2011~2012年在美国佛罗里达大学作访问学者。承担多门本科专业课程、通识教育课程以及工程硕士课程,这些课程被评为校精品课程,上海市重点建设课程,IBM—教育部精品课程等。
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电子书。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值