Java core technology Volume notes (b) of the basic Java programming structure

Java naming conventions

1. The difference in size.
2. Name must begin with a letter, you can be followed by any combination of numbers or letters or underscore length, but can not use Java reserved word.
3. Name the hump using principles:

  • Class or interface name: begin with a capital, after each word capitalized
  • Method and variable names: the beginning of the second word capitalized
  • Constants Name: Constant names are all uppercase letters

Note

Java language annotations are three ways //, / * + * / / ** * /
// generally used for single-line comments, if you want to multi-line comments // Each line must have.
/ * * / Are generally used for multi-line comments, because the nearest match the principles and procedures that may have / *, so be careful not to nest.
/ ** * / is used to generate documentation comments

type of data

A total of eight basic types (primitive type), of which there are four kinds of integer, floating-point type of two kinds, one kind is used to represent the character type char Unicode character encoding units and 1 to represent the true value of the boolean type.

Integer

Here Insert Picture Description
There is a long integer value L or a suffix (e.g. 4000000000L). Hexadecimal value has a prefix 0x or 0X (0xCAFE). There is a 0 prefix octal e.g., corresponding to 010 of 8 octets. Obviously, octal notation than confusing, so it would be best not to use octal constant. Binary prefixed 0b or 0B

Floating-point type

Here Insert Picture Description
a float behind the suffix f or F, no suffix defaults to double

char type


a char data type is employed UTF - 16 encoded Unicode code point represents the code units

boolean boolean
boolean type has two values: true, false, used to determine the logical condition. Integer Boolean value and not interchangeable.

variable

Naming variables

  1. Made up by letters and numbers start with a letter, and you can not use Java reserved words as variable names
  2. Hump ​​naming rules: the class name capitalized; method and variable names the second word capitalized.

Use variables

  1. 变量在使用前都需要声明一个变量类型,可以是基本数据类型也可以是引用类型。

  2. 声明变量后需要给变量赋值完成初始化才能正确运行。可以在任何位置初始化,但是良好的编程习惯是在靠近声明的地方初始化。

常量


常量通常由final修饰,常量名全大写。一般常量都定义在main方法外面由static final修饰,方便其他类或其他方法进行调用。如果要在其他类使用则要增加public权限。

运算符

Java中有+ - * \ % 四种运算符。当参与 \(除)运算时,如果两个参数都为整数则为整数运算,求得的值为得到的整数值,小数值去掉,否则表示浮点运算,值为所求完整的值;整数的求余运算(%)也叫取模,是求两个参数相除的余数,如果能整除则求余为0。另外整数除于0会出现异常,浮点数除以0会得到无穷大或NaN。

数学函数与常量


Java的Math类里提供了很多很方便的等直接使用的数学函数方法和常量,例如求开方的sqrt(),幂运算pow()还有π和e的近似值PI,E。

数值类型之间的转换


基本数剧类型里面除了布尔类型都能够相互转换,但转换的时候有可能会丢失精度,下面这张图表示合法的数值转换,实线不会丢失精度,虚线会。
The solid line is not lost, will be dashed
两个不同类型的操作数进行运算时,会转换成相同类型进行运算。Here Insert Picture Description

 

强制类型转换

如果想让上图中的类型从箭头的一段转换成开始第一端则要使用强制类型转换,例如从double类型转成int类型则要进行强制类型转换,x的值会变成1.如果要四舍五入则可以对x使用Maht.round方法(long类型)再转成int类型。如果超出了类型的最大值则会出现一个在范围内意想不到的值。

double x = 1.888 int i = (int) x

综合赋值和运算符


类似于 += 、*= 、= 、-=这些运算符在左等号在右的二元运算符是一种很方便的简写方式,例如x+=4 就为 x = x + 4。

 

自增与自减运算符

自增和自减的运算符为n++、++n和 n–、--n,运算符在前面的会先完成运算再使用,而运算符在后面的则会先使用操作数的值在完成自增或自减。

关系和boolean运算符 char类型进行比较时,转成Unicode码里的int型数值进行比较
Here Insert Picture Description
Here Insert Picture Description
另外还有三元操作符?:,表达式为:condition ? expression1 : expression 2,意思是如果条件为true,则返回表达式1的值,否则返回表达式2的值。

 

括号与运算符优先级

如果有括号则先计算括号内的表达式,没有则使用下图的优先级计算
priority

字符串

  1. String是不可变字符串类型,它可以声明一个字符串变量,变量的值为一个双引号和双引号里面的内容。String类型的字符串不可以改变,创建好后就存储在字符串常量池中,如果要改变这个字符串变量,则需要使用拼接,把这个字符串的值指向另一个在字符串常量池中常见的字符串值。

  2. 字符串变量进行比较时不能使用==,而要使用equals方法。

字符串常用的API

  1. char charAt ( int index ),返回字符串在index索引下的字符
  2. int codePointAt ( int Index )返回指定索引下的Unicode值
  3. offsetByCodePoints ( int startlndex , int cpCount )返回从startIndex索引下位移cpCount位之后的索引
  4. new String ( int [ ] codePoints , int offset , int count )用数组中从 offset 开始的 count 个码点构造一个字符串。

5. boolean equalsIgnoreCase ( String other )
Here Insert Picture Description

输入输出

输入


读取输入需要构建Scanner类型的对象并且与标准输入流System.in关联。

Scanner in = new Scanner (System.in)

然后要读取输入一行内容则用in.nextLine()方法;要读取输入一个单词则用in.next();
要读取输入一个整数则用in.nextInt();要读取输入一个浮点数则用in.nextDouble()。
如果需要输入的是类似于用户名和密码等保密内容则需要使用Console类。

输出


System.out.print and ordinary standard output System.out.println

System.out.printf output format (), the figure is printf conversion operations may also be added various flags, as FIG.
Here Insert Picture Description

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Coding-and-Teaching/p/11222043.html