Java官方笔记5数字和字符串

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Numbers Number的子类: 另外还有BigDecimal和BigInteger,用于高精度计算,AtomicInteger和AtomicLong用于多线程应用。 我们有时候需要用包装类而非基本数据类型,理由如下: 方法入参类型为Object,只能传入对象 使用包装类提供的常量,比如MIN_VALUE和MAX_VALUE 使用包装类的方法来做类型转换 format import java.util.Calendar; import java.util.Locale; public class TestFormat { public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); // --> "461012" System.out.format("%08d%n", n); // --> "00461012" System.out.format("%+8d%n", n); // --> " +461012" System.out.format("%,8d%n", n); // --> " 461,012" System.out.format("%+,8d%n%n", n); // --> "+461,012" double pi = Math.PI; System.out.format("%f%n", pi); // --> "3.141593" System.out.format("%.3f%n", pi); // --> "3.142" System.out.format("%10.3f%n", pi); // --> " 3.142" System.out.format("%-10.3f%n", pi); // --> "3.142" System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416" Calendar c = Calendar.getInstance(); System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006" System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am" System.out.format("%tD%n", c); // --> "05/29/06" } } DecimalFormat ...

Java官方笔记4类和对象

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 创建类 定义类Bicycle: public class Bicycle { // the Bicycle class has // three fields public int cadence; public int gear; public int speed; // the Bicycle class has // one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has // four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } } 创建子类MountainBike继承类Bicycle: ...

Java官方笔记3Java语言基础

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 变量 Instance Variables (Non-Static Fields) 实例变量(非静态变量) 一个类可以创造多个实例,实例中的变量叫做实例变量,相互独立。 Class Variables (Static Fields) 类变量(静态变量) 对比来看,类变量就是类的变量,一个类只能有一份,不能复制,使用static关键字来定义类变量。 代码: public class MyVar { static int classVar = 1; // 类变量(静态变量) int instanceVar = 2; // 实例变量 } 对比Python来看就很清楚: class MyVar: class_var = 1 def __int__(self): instance_var = 2 Local Variables 局部变量 局部变量是由代码位置决定的,前面的实例变量和类变量,是放到Field位置,而局部变量是放到Method里面。也就是说,局部变量是放在方法的花括号里面的变量,并且只能在方法内部访问,不能被其他地方访问,这就是所谓的局部。 public class MyVar { static int classVar = 1; // 类变量(静态变量) int instanceVar = 2; // 实例变量 void method() { int localVar = 3; // 局部变量 } } Parameters 参数 ...

Java官方笔记2面向对象编程基本概念

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 对象Object 对象有2个特征:状态state和行为behavior。我们可以用真实世界的对象来做类比,这样有助于理解面向对象编程,比如狗的状态(名字、颜色、品种)和行为(叫、抓、摇尾巴),自行车的状态(当前档位、当前踏板节奏、当前速度)和行为(切换档位、切换踏板节奏、踩刹车)。 软件对象也是由状态和行为组成: 状态,在软件对象中,叫做字段fields,或变量variables。 行为,在软件对象中,叫做方法methods,或函数functions。 所谓的数据封装data encapsulation,就是字段只在内部,不对外暴露,仅由方法进行操作,对象与对象之间通过方法来相互访问。 Java是纯粹的面向对象编程语言。 类Class 类,可以视为对象的模版,基于类来创造出同一类对象。 比如自行车的类: class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } } 类也是有2个特征:字段和方法。比如Bicycle类有字段cadence、speed、gear和方法changeCadence()、changeGear()、speedUp()等。 ...

Java官方笔记1编写运行Java程序

你可能已经迫不及待想安装Java,写个Java程序跑起来了。但是在这之前,有些概念需要提前了解,因为Java跟C、C++和Python都有点不一样。