Java中数据类型分类 八大数据类型及取值范围
数据类型 | 大小 | 取值范围 |
byte | 1字节8位 | -128 ~ +127 |
int | 4字节32位 | -2147483648(-231) ~ + 2147483647(231-1) |
short | 2字节16位 | -32768 (-215) ~ + 32767 (+215-1) |
long | 8字节64位 | -263 ~ + 263-1 |
float | 4字节32位浮点数 | 1.4E-45 ~ 3.4E+38 , -1.4E-45 ~ -3.4E+38 |
double | 8字节64位浮点数 | 4.9E-324 ~ 1.7E+308, -4.9E-324 ~ -1.7E+308 |
Java中各种标识符命名规则
标识符只能用英文字母和数字组成,只有常量可以加上下划线
标识符只能以字母开头
类名、接口名首字母必须大写
变量名、方法名首字母必须小写
包名必须全部小写
常量名必须全部大写,并且多个单词使用下划线分割
标识符必须见名知意
标识符如果由多个单词组成时,必须使用驼峰命名(myInfo,studentName)
Java关键词
abstract | class | final | int | public | this |
assert | continue | finally | interface | return | throw |
boolean | default | float | long | short | throws |
break | do | for | native | static | transient |
byte | double | if | new | strictfp | try |
case | else | implements | package | super | void |
catch | enum | import | private | switch | volatile |
char | extends | instanceof | protected | synchronized | while |
Java常量
名称 | 举例 | 说明 |
整型常量 | 789 | 超过int取值范围的,必须在整数后面加大写的英文字母“L”或小写的英文字母“l”,才能作为long类型处理。 |
浮点型常量 | 3.4f //float -45.9 //double 3.4D//double | Java的浮点型常量默认是double,float需要在数字后面加大写的“F”或小写的“f” |
布尔常量 | true和false | 布尔常量只能为true和false |
null常量 | null | null常量只有null一个值,可以null常量赋值给任意类型的引用类型变量 |
基本算数运算符
算术运算符 | 说明 | 举例 |
+ | 加法运算符 | 5+3=8 |
- | 减法运算符 | 5-3=2 |
* | 乘法运算符 | 5*3=15 |
/ | 除法运算符 | 5/3=1 |
% | 取余运算符 | 5%3=2 |
++ | 自增运算符 | i=2 ; i++ 结果为3 |
-- | 自减运算符 | i=2 ; i-- 结果为1 |
i++和++i的区别
i = i++是先赋值再运算
i = ++i是先运算在赋值
Scanner的应用
在程序运行中,接收用户键盘输入的值。
import java.util.Scanner;
public class ShoppingText {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tshortPrice = 245;
System.out.println("请输入T恤的数量:");
int tshortQuant = sc.nextInt();
int shoesPrice = 570;
System.out.println("请输入网球鞋的数量:");
int shoesQuant = sc.nextInt();
int racketPrice = 320;
System.out.println("请输入网球拍的数量:");
int racketQuant = sc.nextInt();
System.out.println("商品 \t\t单价(元)\t个数");
System.out.println("T恤\t\t\t " + tshortPrice + "\t\t " + tshortQuant);
System.out.println("网球鞋\t\t " + shoesPrice + "\t\t " + shoesQuant);
System.out.println("网球拍\t\t " + racketPrice + "\t\t " + racketQuant + "\n");
double money = ((tshortPrice * tshortQuant) + (shoesPrice * shoesQuant)
+ (racketPrice * racketQuant)) * 0.8;
System.out.println("消费总金额:" + money);
}
}