您的位置:首页 > 编程语言 > Java开发

Core java 第九版第三章基本程序结构

2016-12-16 16:22 495 查看
Talk is cheap, show me the code.

类型存储需求取值范围
int4字节-2147483648 ~ 2147483647(正好超过20亿)
short2字节-32768 ~ 32767
long8字节-9223372036854775808 ~ 9223372036854775808
byte1字节-128 ~ 128
float4字节大约+(-)3.40282347E + 38F(有效位数为6-7位)
double8字节大约+(-)1.79769313486231570E + 208(有效位数为15位)
boolean1字节true/false
char2字节UTF-16编码,不建议使用
java没有无符号类型unsigned,并且每种数据类型所占字节数与平台无关,也就是说上面的表在任何平台上都适用。

java中不区分变量的声明与定义。

Java中,>>>表示在高位填0,>>表示在高位填符号位,<<在低位填0,java消除了C++中的移位含糊性。

java中的数学运算一般采用Math类即可,如果要保证所有平台上取得一样的计算结果就应该使用StrictMath类。数学运算都是采用的double数据类型。

Math.sqrt(x)

Math.pow(x, a)


java中的强制类型转换比C++中简单,直接用括号即可。

double a = 9.9;

int b = (int)Math.round(a);  //Math.round返回long型


java中字符串是保存在字符串常量池中的,尽可能实现了字符串的复用,当不再被变量使用的字符串就会自动被当做垃圾回收。

java中判断字符串相等不能用==,而应该用equals方法,若要忽略大小写则使用equalsIgnoreCase方法。

String s1 = "hello", s2 = "lee";

System.out.println((s1 == s2) + " " + (s1.equals(s2)));


java中代码点(code point)是指一个字符对应的编码值,一个代码单元是指一个字节,一个字符对应一个代码点,但一个代码点可能对应有多个代码单元。

java中字符串使用String,只读操作使用String很方便,但如果要对一个字符串进行操作,最好使用StringBuilder。String类定义了查找字符和子串非常方便的方法,但是没有定义增删修改的方法,只有一个replate方法可以改动字符串。StringBuilder定义了很多设置字符和子串的方法,可以实现对字符串在任意位置增加删除修改字符或者子串的方法。

String常用方法:

char charAt(int index)

int indexOf(int 代码点)

int codePointAt(int index)

int indexOf(String str, int fromIndex)

bool startsWith(String str)

bool endsWith(String str)

int length()

String subString(int beginIndex, int endIndex)  //不包括endIndex

String subString(int beginIndex)

int compareTo(String str)

bool equals(String str)

bool equalsIgnoreCase(String str)

String toLowerCase()

String toUpperCase()

String replace(charSequence oldString, charSequence newString)

String trim()  //去掉字符串首尾的空格


StringBuilder常用方法:

int length()

StringBuilder append(char C)

StringBuilder append(String str)

StringBuilder appendCodePoint(int cp)

void setCharAt(int index, char C)

StringBuilder insert(int offset, String str)

StringBuilder insert(int offset, char C)

StringBuilder delete(int beginIndex, int endIndex)  //不包括endIndex

String toString()


标准输入输出。标准输入需要使用java.util.Scanner类从System.in中读入,标准输出只需使用System.out对象中的方法即可,一般使用System.out.print或者System.out.println,如果需要格式化输出,可以使用System.out.printf(“%8.2f”, x)。注意使用除了java.lang中的对象以外所有的对象都需要引用对应的包名。

import java.util.*;

String line, word;

int a;

Scanner in = new Scanner(System.in);

line = in.nextLine();

word = in.next();

a = in.nextInt();


float a = 23424.23234f;

System.out.printf("%3.2f", a);


java中提供了表达任意精度的整数类型BigInteger和任意精度的浮点数类型BigDecimal,并提供了进行基本运算的方法,注意没有重载运算符。但注意使用时一定要包含包java.math.*。

import java.math.*;

BigInteger的常用方法:

static BigInteger valueOf(long x)

BigInteger add(BigInteger other)

BigInteger substract(BigInteger other)

BigInteger multiply(BigInteger other)

BigInteger divide(BigInteger other)

BigInteger mod(BigInteger other)

int compareTo(BigInteger other)


import java.math.*;

BigDecimal的常用方法:

static BigDecimal valueOf(long x)

static BigDecimal valueOf(long x, int scale)  //返回x/10^scale的浮点数

BigDecimal add(BigDecimal other)

BigDecimal substract(BigDecimal other)

BigDecimal multiply(BigDecimal other)

BigDecimal divide(BigDecimal other)

int compareTo(BigDecimal other)


java中的数组只能通过new的方式来创建,声明和创建时不要求指定固定长度。java数组可以直接赋值给另一个数组,但是这样赋值两个数组变量引用的是同一个数组,一个数组值发生变化另一个也会随之发生变化。java.util.Arrays类提供了很多处理数组的方法。

int a[];  //只声明了一个数组变量,但没有定义

int[] b = new int[10];

int c = 10;

int[] d = new int[c];  //可以不指定具体长度

int[] e = d;

e[1] = 12;   //d[1]也会变成12


import java.util.Arrays;

int[] a = new int[]{1, 2, 3};

int[] b = Arrays.copyOf(a, 2 * a.length);  //拷贝a的元素到b中,两者互不影响,常用来扩展数组长度


C++中命令行参数是从程序名开始,而java中命令行参数是不包含java或者程序名。

C++中命令行参数:

test.exe -h 1   //argc = 3, argv[0]=test.exe, argv[1]=-h, argv[2]=1

java中命令行参数:

java Test -h 1   //args[0]=-h, args[1]=1


java中生成随机数很简单,利用Math.random()即可生成一个从0到1的数,包含0不包含1。

java.util.Arrays提供了很多很方便的方法。比如Arrays.sort(type[] a)采用快速排序对数组a进行了排序; Arrays.toString(type[] a)将数组a转化成字符串,格式如”[1,2,3]”,逗号分隔,没有空格;Arrays.copyOf(type[] a, int length),拷贝数组并指定新数组长度; Arras.binarySearch(type[] a, type b)以二分搜索在数组a中查找b。

import java.util.*;

static String Arrays.toString(type[] a)

static type[] Arrays.copyOf(type[] a, int length)

static type[] Arrays.copyOfRange(type[] a, int start, int end)

static void Arrays.sort(type[] a)

static int Arrays.binarySearch(type[] a, type b)

static int Arrays.binarySearch(type[] a, int start, int end, type b)

static void Arrays.fill(type[] a, type v)

static boolean Arrays.equals(type[] a, type[] b)  //大小相同,各元素相等返回true,否则返回false


C++中的数组长度是固定的,没有对应的方法能获取数组的长度,只能通过遍历获取,但是vector是可以采用size()方法来获取vector的长度的。java中数组的长度是可以获得的,可以通过成员变量length获取,注意不是方法,而是成员变量。

int[] a = new int[3]{1,3,4};

System.out.println(a.length);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java