Core Java For The Impatient-阅读笔记1.基本的编程结构

Core Java For The Impatient

1.基本的编程结构

1.Key Points

  1. 在Java,所有的方法必须在类声明。非静态方法只能在所属类的对象上调用。
  2. 静态方法不是通过对象调用。程序从静态的main方法开始执行。
  3. Java有8个基本数据类型;5个整型、2个浮点型、1个布尔型。
  4. 字符串对象是字符序列,是UTF-16编码中的Unicode编码点的序列。
  5. 使用System.out对象,终端窗口显示输出。绑定System.in的Scanner可以读取终端输入
  6. 数组和集合用来收集相通类型的元素。

2.容易忽视的点

  • 编译和运行:javac命令将Java源代码编译成中间代码(字节码),并将他们保存到类文件。Java命令启动JVM,JVM加载类文件并执行字节码。(一旦编译完成,可以在任何JVM上运行)。

  • Main方法System.out是对象,名为PrintStream类的实例,其有println和print等方法,为实例方法。命令行参数String[] args ,程序调用 java Greeting -g cruel world args[0]为-g, cruel和world 也是数组参数。

  • 部分数据类型

类型存储需求范围(包含该值)
int4byte-2147 483 648 - 2147483647(超过20亿) 2^32 - 1
long8byte-264-264-1
short2byte-32768-32767
byte1byte-128-127
float4byte±3.40282347E+38F(6-7位有效数字)
double8byte±1.79769…(15位有效数字)
  • int最实用,MIN_VALUE和MAX_VALUE表示最小最大。特别多(超过32位,比如人口),用long,不够用用BigInteger。byte和short,当存储空间珍贵的底层文件处理,大数组处理。Byte.toUnsignedInt(b)方法,可以获得0-255的数值。

  • 以前内存稀缺,默认四个字节float,现在默认double精度。

  • Java中,整型的范围不依赖于程序所运行的机器。(原则是面向字节码),C/C++,依赖编译程序的处理器。

  • Double.POSTIVE_INFINITY正无穷,否则负无穷。NaN非数值。

  • char型:描述Java使用的UTF-16字符编码中的“编码单元”。

  • 1_000_000_000 下划线阅读更容易,编译器会直接删除。

  • 0.0009765625 = 2^-10 可以写成0x1.0p-10,在十六进制可以使用p,而不是e,即使数字用十六进制书写,指数还是用十进制。

  • 非数值被认为是彼此不同的,是性质上属于而不是数字相等。if(x == Double.NaN) 不可用,而是if(Double.isNaN(x)),isInfinite() 判断是否正负无穷大,浮点数不适合金融计算,有误差。【因为浮点数在计算机使用二进制系统表示,1/10没有精确的二进制表示,如果需要精确,用BigDecimal类】

  • 变量:java强类型语言,某个具体类型的值。在方法中声明变量,必须初始化,否则编译错误。

  • 常量:final赋值不能变化,方法外声明用static,System类声明了一个常量public static final PrintStream out,可以在任何地方使用System.out,少有的几个常量没有大写的情况之一。一组相关常量,定义枚举 enum Weekday{MON, TUE, WED,THU,FRI,SAT, SUN}; 使用 Weekday startDay = Weekday.MON;

  • 算术操作,注意点:+比<<(算术移位)优先级高,3 + 4 <<5 的运算顺序是(3+4)<< 5,操作符-=是右结合,i-=j-=k的意思是i -= (j-=k)

  • 基本运算:整数除以0会导致异常,如果没有捕获异常,程序会终止。浮点数除以0或产生无限值或者NaN(正负无穷),不会导致异常;判断奇偶数%2,但是如果n为负数,n%2 = -1,当操作数为负数,谨慎使用%。

1.经典余数法:
for (int i = -2; i < 3; i++) {
    if (i % 2 == 1 ) {
        System.out.println(i + "为奇数");
    } else if (i % 2 == 0) {
        System.out.println(i + "为偶数");
    } else {
        System.out.println(i + "i % 2=" + i % 2);
    }
}
弊端:操作数是负数,偶数正常==0,奇数则为-1,一般无法判断。
结果:
-2为偶数
-1i % 2=-1
0为偶数
1为奇数
2为偶数
2.用二进制与1的方法判断  
if ((i & 1) == 1) {
    System.out.println(i + "为奇数");
} else  {
    System.out.println(i + "为偶数");
} 
结果:
-2为偶数
-1为奇数
0为偶数
1为奇数
2为偶数
  • 数字类型转换:double和float和long具有单个强制另一个数字转化同类型的能力,其余都为int类型。比如 J + 1 为74,char next = (char) ('J' + 1); cast转换如果越界只有最后一个值保存下来。

  • 操作符n = n | 0xF 将最低四位设置为1,n = n^0xF对n进行反转,参数比特位反转:~0XF= 1....10000 >> 以0替换前面的位,而>>>以符号位扩展为最前面的位。

//0xF = 15
int a = 0x6;     // 0110
int b = a | 0xF; // 1111  15
int c = a ^ 0xF; // 1001  9
int d = ~ 0xF;   //  1000  16
d >> 1;	  // -8
d >>> 1; // 2147483640
  • 大数 :valueOf转化,操作大数要用方法调用。

  • 字符串顺序比较:first.compareTo(second),如果前者排在后者的前面,返回负整数,后面则为正整数,否则为0。【两个字符的unicode之差】;字符串数字转换Integer.toString(n,2);// 设置基数2-n-2;

  • case:标签整数、char、byte、short、int或包装类,文字串,枚举类型值。长忘记加break,可以编译时加配置。javac -Xlint:fallthrough xxpagckage/XXClass.java @SuppressWarning(“fallthrough”)这样不会为该方法产生警告(注解给编译器或者其他工具提供信息)

  • 数组:数组和列表复制等

Arrays.copyOf(primes,primes.length);  // 新长度,可截取
ArrayList<String> copiedFriends = new ArrayList<>(friends);// 将元素传递,但是两者不会影响。(深拷贝)
ArrayList<String> copiedFriends = new ArrayList<>(Arrays.asList(names));// 变成列表
  • 二位数组:杨辉三角
private static int[][] YongHuiTri(int n) {
    int[][] tri = new int[n][];
    for(int i = 0; i < n; i++) {
        tri[i] = new int[i + 1];
        tri[i][0] = 1; tri[i][i] = 1;
        for(int j = 1; j < i; j++) {
            tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
        }
    }
    return tri;
}

二维数组快速输出:System.out.println(Arrays.deepToString(yongHuiTri));

The release of Java SE 8 introduced significant enhancements that impact the Core Java technologies and APIs at the heart of the Java platform. Many old Java idioms are no longer required and new features like lambda expressions will increase programmer productivity, but navigating these changes can be challenging., , Core Java® for the Impatient is a complete but concise guide to Java SE 8. Written by Cay Horstmann—the author of Java SE 8 for the Really Impatient and Core Java™, the classic, two-volume introduction to the Java language—this indispensable new tutorial offers a faster, easier pathway for learning the language and libraries. Given the size of the language and the scope of the new features introduced in Java SE 8, there’s plenty of material to cover, but it’s presented in small chunks organized for quick access and easy understanding., , If you’re an experienced programmer, Horstmann’s practical insights and sample code will help you quickly take advantage of lambda expressions (closures), streams, and other Java language and platform improvements. Horstmann covers everything developers need to know about modern Java, including, - Crisp and effective coverage of lambda expressions, enabling you to express actions with a concise syntax, - A thorough introduction to the new streams API, which makes working with data far more flexible and efficient, - A treatment of concurrent programming that encourages you to design your programs in terms of cooperating tasks instead of low-level threads and locks, - Up-to-date coverage of new libraries like Date and Time, - Other new features that will be especially valuable for server-side or mobile programmers, Whether you are just getting started with modern Java or are an experienced developer, this guide will be invaluable for anyone who wants to write tomorrow’s most robust, efficient, and secure Java code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值