1. Data Type
Java has 8 basic data types:
integer types : byte, short, int, long
Floating point type: float, double
Character type: char
Boolean: boolean
Supplement: boolean is false by default
Types of |
description |
Ranges |
Byte |
8-bit signed integer |
-128 to 127 Any integer between |
Short |
16-bit signed integer
|
-32768~32767 Any integer between |
Int |
32-bit signed integer |
-2^31 to 2^31-1 Any integer between |
Long |
64-bit signed integer |
-2^63 to 2^63-1 Any integer between |
Float |
32-bit single precision floating point number |
3.402823e+38 ~ 1.401298e-45 |
Double |
64-bit double precision floating point number |
1.797693e + 308 ~ 4.9000000e-324 |
Char |
|
' \ u0000 ' ~ ' \ uffff ' |
2. Keywords and identifiers
Keywords:
Identifier:
Naming conventions.:
1. Java keywords and reserved words cannot be used, but keywords and reserved words can be included.
2. You can use 26 uppercase and lowercase letters, numbers 0-9, $ and _.
3. Numbers can be used, but not in the first place.
4. There is no limit to the length in theory, but the naming should best reflect its role, follow the "camel case", see the meaning of life.
Naming convention.:
1. Class and interface names. The first letter of each word is capitalized, including case. For example, MyClass, HelloWorld , Time, etc.
2. Method name. The first character is lowercase, and the rest of the first letters are uppercase, including case. Use underscores as little as possible. For example, myName, setTime, etc. This naming method is called camel case naming
3. Constant name. Constant names of basic data types use all uppercase letters, and words are separated by underscores. Object constants can be mixed case. For example, SIZE_NAME
4. Variable name. It can be written in both upper and lower case, the first character is lowercase, and the first letter of the word is uppercase for the separator between words. Don't use underscores and use dollar signs less. Naming variables is to be as clear as possible
3. Constant
Table of commonly used escape characters:
4. Variables
5. Data type conversion
JAVA data type conversion is divided into two types:
1. Automatic type conversion: <1>. The data type before conversion is compatible with the data type after conversion
<2> The data value range after conversion is larger than before conversion
int a=3;byte b=a;//报错,类型不匹配,不能从int转换为byte。byte的取值范围要小于int所以编译失败。
short a=3;float b=a;//成功,因为b的取值范围是float类型4个字节,a的取值范围是short类型2个字节。b的范围大于a所以成功
The above is the basic data type conversion problem involved in the assignment, then the following are some special cases that may be involved in the operation.
//第一种情况:表达式运算的类型转换
byte a=3;byte b=5;
byte z=a+b;//这个运算同样会编译失败,因为在运算期间变量a和b都被自动提升为int类型,表达式的结果也就变成了int型,所以我们需要进行强制类型转换。
byte z=(byte)(a+b);/正确代码。
//第二种情况:赋值运算符的类型转换
short s=3;
s+=3;//运行成功, 实际为 s=(short)(s+3);
//这是因为在使用赋值运算赋值的时候,java虚拟机会自动的完成强制类型的转换。
2. Forced type conversion:
//正常转换和丢失精度
short s = 199;
int i = s;// 199
double d = 10.24;
long ll = (long) d;// 10
//数据溢出
int ii = 300;
byte b = (byte)ii;
Conversion between string data and integer data:
Conversion method |
Function Description |
Byte.paraseByte (String s) |
Convert numeric string to byte data |
Short.paraseShort(String s) |
Convert numeric string into short integer data |
Integer.paraseInt(String s) |
Convert a string of numbers to integer data |
Long.paraseLong(String s) |
Convert numeric string to long integer data |
Float.paraseFloat(String s) |
Convert numeric string to single-precision floating-point data |
Double.paraseDouble(String s) |
Convert numeric string to double-precision floating-point data |
Boolean.paraseBoolean(String s) |
Convert string to boolean data |
6. Input data by keyboard:
// 接收从键盘输入数据的三种方法
import java.io.*;
import java.util.*;
public class InputDemo {
public static void main(String[] args) throws IOException {
// 1、使用System.in.read(byte[] )方法
byte buf[]= new byte[50]; // 用于存放输入的字节数组
System.out.println("请输入数据 :");
int len = System.in.read( buf ); // 接受并存入数组的字节数
String str = new String(buf, 0, len); //将字节数组转换为字符串
System.out.println("你输入的是: "+str);
// 2、使用BufferedReader指定输入流为System.in,然后使用BufferedReader的readLine()方法,
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入数据 :");
str = br.readLine(); //读取一行
System.out.println("你输入的是:" + str);
// 3、使用Scanner
Scanner scan =new Scanner(System.in);
scan.useDelimiter("\n");
System.out.println("请输入数据 :");
if (scan.hasNext()){
str = scan.next();
}
System.out.println("你输入的是:" + str); //扫描器
}// END MAIN
}