2.1 数据类型
2.1.1 Java的数据类型
Java语言的数据类型可以分为两大类:基本数据类型和引用数据类型(复合数据类型)。
| 数据类型 | 特点 |
| — | — |
| | 由程序设计语言系统所定义的、不可再划分 |
| 基本数据类型 | 数据所占的内存大小固定,与软硬件环境无关 |
| | 在内存中存入的是数据值本身 |
| | 在内存中存入的是指向该数据的地址,不是数据本身 |
| 引用数据类型 | 往往由多个基本数据组成 |
Java定义了8个基本数据类型:字节型、短整型、整型、长整型、字符型、浮点型、双精度型、布尔型。
#mermaid-svg-PjhGKpsbrK1IAs21 .label{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);fill:#333;color:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .label text{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .node rect,#mermaid-svg-PjhGKpsbrK1IAs21 .node circle,#mermaid-svg-PjhGKpsbrK1IAs21 .node ellipse,#mermaid-svg-PjhGKpsbrK1IAs21 .node polygon,#mermaid-svg-PjhGKpsbrK1IAs21 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PjhGKpsbrK1IAs21 .node .label{text-align:center;fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .node.clickable{cursor:pointer}#mermaid-svg-PjhGKpsbrK1IAs21 .arrowheadPath{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-PjhGKpsbrK1IAs21 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-PjhGKpsbrK1IAs21 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-PjhGKpsbrK1IAs21 .edgeLabel rect{opacity:0.9}#mermaid-svg-PjhGKpsbrK1IAs21 .edgeLabel span{color:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-PjhGKpsbrK1IAs21 .cluster text{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-PjhGKpsbrK1IAs21 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PjhGKpsbrK1IAs21 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-PjhGKpsbrK1IAs21 .actor-line{stroke:grey}#mermaid-svg-PjhGKpsbrK1IAs21 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-PjhGKpsbrK1IAs21 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .sequenceNumber{fill:#fff}#mermaid-svg-PjhGKpsbrK1IAs21 #sequencenumber{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .messageText{fill:#333;stroke:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PjhGKpsbrK1IAs21 .labelText,#mermaid-svg-PjhGKpsbrK1IAs21 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-PjhGKpsbrK1IAs21 .loopText,#mermaid-svg-PjhGKpsbrK1IAs21 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-PjhGKpsbrK1IAs21 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-PjhGKpsbrK1IAs21 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PjhGKpsbrK1IAs21 .noteText,#mermaid-svg-PjhGKpsbrK1IAs21 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-PjhGKpsbrK1IAs21 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-PjhGKpsbrK1IAs21 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-PjhGKpsbrK1IAs21 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-PjhGKpsbrK1IAs21 .mermaid-main-font{font-family:“trebuchet ms”, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .section{stroke:none;opacity:0.2}#mermaid-svg-PjhGKpsbrK1IAs21 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-PjhGKpsbrK1IAs21 .section2{fill:#fff400}#mermaid-svg-PjhGKpsbrK1IAs21 .section1,#mermaid-svg-PjhGKpsbrK1IAs21 .section3{fill:#fff;opacity:0.2}#mermaid-svg-PjhGKpsbrK1IAs21 .sectionTitle0{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .sectionTitle1{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .sectionTitle2{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .sectionTitle3{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-PjhGKpsbrK1IAs21 .grid .tick text{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .grid path{stroke-width:0}#mermaid-svg-PjhGKpsbrK1IAs21 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-PjhGKpsbrK1IAs21 .task{stroke-width:2}#mermaid-svg-PjhGKpsbrK1IAs21 .taskText{text-anchor:middle;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .taskText:not([font-size]){font-size:11px}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-PjhGKpsbrK1IAs21 .task.clickable{cursor:pointer}#mermaid-svg-PjhGKpsbrK1IAs21 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PjhGKpsbrK1IAs21 .taskText0,#mermaid-svg-PjhGKpsbrK1IAs21 .taskText1,#mermaid-svg-PjhGKpsbrK1IAs21 .taskText2,#mermaid-svg-PjhGKpsbrK1IAs21 .taskText3{fill:#fff}#mermaid-svg-PjhGKpsbrK1IAs21 .task0,#mermaid-svg-PjhGKpsbrK1IAs21 .task1,#mermaid-svg-PjhGKpsbrK1IAs21 .task2,#mermaid-svg-PjhGKpsbrK1IAs21 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutside0,#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutside2{fill:#000}#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutside1,#mermaid-svg-PjhGKpsbrK1IAs21 .taskTextOutside3{fill:#000}#mermaid-svg-PjhGKpsbrK1IAs21 .active0,#mermaid-svg-PjhGKpsbrK1IAs21 .active1,#mermaid-svg-PjhGKpsbrK1IAs21 .active2,#mermaid-svg-PjhGKpsbrK1IAs21 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-PjhGKpsbrK1IAs21 .activeText0,#mermaid-svg-PjhGKpsbrK1IAs21 .activeText1,#mermaid-svg-PjhGKpsbrK1IAs21 .activeText2,#mermaid-svg-PjhGKpsbrK1IAs21 .activeText3{fill:#000 !important}#mermaid-svg-PjhGKpsbrK1IAs21 .done0,#mermaid-svg-PjhGKpsbrK1IAs21 .done1,#mermaid-svg-PjhGKpsbrK1IAs21 .done2,#mermaid-svg-PjhGKpsbrK1IAs21 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-PjhGKpsbrK1IAs21 .doneText0,#mermaid-svg-PjhGKpsbrK1IAs21 .doneText1,#mermaid-svg-PjhGKpsbrK1IAs21 .doneText2,#mermaid-svg-PjhGKpsbrK1IAs21 .doneText3{fill:#000 !important}#mermaid-svg-PjhGKpsbrK1IAs21 .crit0,#mermaid-svg-PjhGKpsbrK1IAs21 .crit1,#mermaid-svg-PjhGKpsbrK1IAs21 .crit2,#mermaid-svg-PjhGKpsbrK1IAs21 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-PjhGKpsbrK1IAs21 .activeCrit0,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCrit1,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCrit2,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-PjhGKpsbrK1IAs21 .doneCrit0,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCrit1,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCrit2,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-PjhGKpsbrK1IAs21 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-PjhGKpsbrK1IAs21 .milestoneText{font-style:italic}#mermaid-svg-PjhGKpsbrK1IAs21 .doneCritText0,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCritText1,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCritText2,#mermaid-svg-PjhGKpsbrK1IAs21 .doneCritText3{fill:#000 !important}#mermaid-svg-PjhGKpsbrK1IAs21 .activeCritText0,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCritText1,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCritText2,#mermaid-svg-PjhGKpsbrK1IAs21 .activeCritText3{fill:#000 !important}#mermaid-svg-PjhGKpsbrK1IAs21 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 g.classGroup text{fill:#9370db;stroke:none;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:10px}#mermaid-svg-PjhGKpsbrK1IAs21 g.classGroup text .title{font-weight:bolder}#mermaid-svg-PjhGKpsbrK1IAs21 g.clickable{cursor:pointer}#mermaid-svg-PjhGKpsbrK1IAs21 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PjhGKpsbrK1IAs21 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-PjhGKpsbrK1IAs21 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-PjhGKpsbrK1IAs21 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PjhGKpsbrK1IAs21 .dashed-line{stroke-dasharray:3}#mermaid-svg-PjhGKpsbrK1IAs21 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 .commit-id,#mermaid-svg-PjhGKpsbrK1IAs21 .commit-msg,#mermaid-svg-PjhGKpsbrK1IAs21 .branch-label{fill:lightgrey;color:lightgrey;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .slice{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-PjhGKpsbrK1IAs21 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-PjhGKpsbrK1IAs21 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PjhGKpsbrK1IAs21 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PjhGKpsbrK1IAs21 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PjhGKpsbrK1IAs21 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-PjhGKpsbrK1IAs21 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-PjhGKpsbrK1IAs21 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PjhGKpsbrK1IAs21 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-PjhGKpsbrK1IAs21 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-PjhGKpsbrK1IAs21 .edgeLabel text{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-PjhGKpsbrK1IAs21 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-PjhGKpsbrK1IAs21 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-PjhGKpsbrK1IAs21 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-PjhGKpsbrK1IAs21 .note-edge{stroke-dasharray:5}#mermaid-svg-PjhGKpsbrK1IAs21 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{–mermaid-font-family: ‘“trebuchet ms”, verdana, arial’;–mermaid-font-family: “Comic Sans MS”, “Comic Sans”, cursive}#mermaid-svg-PjhGKpsbrK1IAs21 .error-icon{fill:#522}#mermaid-svg-PjhGKpsbrK1IAs21 .error-text{fill:#522;stroke:#522}#mermaid-svg-PjhGKpsbrK1IAs21 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-PjhGKpsbrK1IAs21 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-PjhGKpsbrK1IAs21 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-PjhGKpsbrK1IAs21 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-PjhGKpsbrK1IAs21 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-PjhGKpsbrK1IAs21 .marker{fill:#333}#mermaid-svg-PjhGKpsbrK1IAs21 .marker.cross{stroke:#333} :root { --mermaid-font-family: “trebuchet ms”, verdana, arial;} #mermaid-svg-PjhGKpsbrK1IAs21 { color: rgba(0, 0, 0, 0.75); font: ; }
数据类型
基本数据类型
引用类型
整数型
字节型
整型
短整型
长整型
浮点型
单精度
双精度
字符型
布尔型
数组
类 包括对象
接口
2.2.2 常量与变量
在程序运行过程中,数据值不能改变的称为常量,数据值可以改变的称为变量。
在Java语言中,所有的常量和变量都要“先声明后使用”。
声明变量的格式:
数据类型 变量名1,变量名2,…;
常量的声明在前面加一个关键词:final
例如:
final int DAY = 10; //DAY的值在整个程序中保持不变,将其声明为常量
习惯上,常量名用大写字母,变量名用小写字母。
2.1.3 变量赋值
在Java中赋值用(=)表示,所谓赋值就是把赋值号右边的数据或运算结果赋给左边的变量。一般格式为:
变量 = 表达式
例如:
int x = 5; //指定x为整型变量,并赋初值5
2.1.4 关键字
关键字就是Java语言中已经规定了特定意义的单词,用来表示一种数据型或表示程序的结构等。这些单词不能用作常量名或者变量名。
Java语言中规定的关键字有:
abstract、boolean、break、byte、case、catch、char、class、
continue、default、do、double、else、extends、false、final、
finally、float、for、if、implements、import、instanceof、int、
interface、long、native、new、null、package、private、protected、
public、return、short、static、super、switch、synchronized、
this、throw、throws、 transient、true、try、void、volatile、while。
2.1.5 转义符
| 转义符 | 含义 |
| — | — |
| \b | 退格 |
| \f | 走纸换页 |
| \n | 换行 |
| \r | 回车 |
| \t | 横向跳格 |
| ’ | 单引号 |
| " | 双引号 |
| \ | 反斜杠 |
2.2基本数据类型应用示例
2.2.1 整数类型与浮点类型
1.整数类型
import javax.swing.*;
class Example_1
{
public static void mian(String args[])
{
int x,y,sum; //声明三个整型变量
x = 3;
y = 5;
sum = x + y;
JOptionPane.showMessageDialog(null,
“x = 3;”+“\n y = 5;”+"\n x + y = "+sum);
System.exit(0);
}
}
运行结果:
2.浮点类型
浮点数也称为实数,当计算的表达水有精度要求时就要使用浮点类型。
/* 计算圆的面积 */
import javax.swing.*;
class Example2_2
{
public static void main(String args[])
{
double pi,r,s;
r = 10.8; //圆的半径
pi = 3.1416;
s = pi * r * r; //计算面积
JOptionPane.showMessageDialog(null,“圆的面积为:” + s);
System.exit(0);
}
}
运行结果:
2.2.2 字符型
1.字符型变量
在Java中,存储字符的数据类型是 char。一个字符在内存中占2个字节。
给字符型变量赋值时,字符需要用单引号括起来,例如:
char ch = ‘a’
/* char 变量的用法 */
import javax.swing.*;
class example_1
{
public static void main(String args[])
{
char ch1,ch2,ch3;
ch1 = 88; // code for X
ch2 = ‘Y’; ch3 = ‘汉’;
JOptionPane.showMessageDialog(null,"ch1、ch2 、ch3: " + ch1 + “、” + ch2 + “、” +ch3);
System.exit(0);
}
}
运行结果:
Java的char与C或C++中的char不同。在C或C++中char只有8位,而在Java中char有16位,因为它使用的是Uniocode定义的国际化的字符集。
2.字符串
用双引号括起来的多个字符常量称为字符串。
'abc’是不合法的,而""是合法的,表示空字符。
2.2.3 布尔型
Java表示逻辑值的基本类型称为布尔型。它只有true和false两个值。
/* 布尔类型的用法 */
class Example2_4 {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
运行结果:
2.2.4 数据类型转换
1.自动类型转换(级别低的变量值赋给级别高的变量)
数据类型转换需要符合的两个条件:
a.转换前的数据类型与转换后的数据类型兼容;
b.转换后的数据类型比转换前的数据类型表示的范围大。
基本数据类型精度从低到高依次为:
byte --> short --> int --> long --> float --> double
2.强制类型转换
强制类型转换是指当把级别高的变量赋给级别低的变量时,必须使用类型的强制转换运算。转换的格式为:
(类型名)要转换的值或者变量;
例:
int a;
double b = 3.14;
a = (int)b;
//结果a = 3,b = 3.14(强质类型转换会降低数据精度)
2.3 表达式和运算符
2.3.1 表达式与运算符分类
1.表达式
表达式是由运算符、操作数和方法调用按照语言的语法构造而成的符号序列。
2.运算符以及分类
#mermaid-svg-8niCmAwWRYg44DYk .label{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);fill:#333;color:#333}#mermaid-svg-8niCmAwWRYg44DYk .label text{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .node rect,#mermaid-svg-8niCmAwWRYg44DYk .node circle,#mermaid-svg-8niCmAwWRYg44DYk .node ellipse,#mermaid-svg-8niCmAwWRYg44DYk .node polygon,#mermaid-svg-8niCmAwWRYg44DYk .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-8niCmAwWRYg44DYk .node .label{text-align:center;fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .node.clickable{cursor:pointer}#mermaid-svg-8niCmAwWRYg44DYk .arrowheadPath{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-8niCmAwWRYg44DYk .flowchart-link{stroke:#333;fill:none}#mermaid-svg-8niCmAwWRYg44DYk .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-8niCmAwWRYg44DYk .edgeLabel rect{opacity:0.9}#mermaid-svg-8niCmAwWRYg44DYk .edgeLabel span{color:#333}#mermaid-svg-8niCmAwWRYg44DYk .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-8niCmAwWRYg44DYk .cluster text{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-8niCmAwWRYg44DYk .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-8niCmAwWRYg44DYk text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-8niCmAwWRYg44DYk .actor-line{stroke:grey}#mermaid-svg-8niCmAwWRYg44DYk .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-8niCmAwWRYg44DYk .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-8niCmAwWRYg44DYk #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-8niCmAwWRYg44DYk .sequenceNumber{fill:#fff}#mermaid-svg-8niCmAwWRYg44DYk #sequencenumber{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk #crosshead path{fill:#333;stroke:#333}#mermaid-svg-8niCmAwWRYg44DYk .messageText{fill:#333;stroke:#333}#mermaid-svg-8niCmAwWRYg44DYk .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-8niCmAwWRYg44DYk .labelText,#mermaid-svg-8niCmAwWRYg44DYk .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-8niCmAwWRYg44DYk .loopText,#mermaid-svg-8niCmAwWRYg44DYk .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-8niCmAwWRYg44DYk .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-8niCmAwWRYg44DYk .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-8niCmAwWRYg44DYk .noteText,#mermaid-svg-8niCmAwWRYg44DYk .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-8niCmAwWRYg44DYk .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-8niCmAwWRYg44DYk .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-8niCmAwWRYg44DYk .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-8niCmAwWRYg44DYk .mermaid-main-font{font-family:“trebuchet ms”, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .section{stroke:none;opacity:0.2}#mermaid-svg-8niCmAwWRYg44DYk .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-8niCmAwWRYg44DYk .section2{fill:#fff400}#mermaid-svg-8niCmAwWRYg44DYk .section1,#mermaid-svg-8niCmAwWRYg44DYk .section3{fill:#fff;opacity:0.2}#mermaid-svg-8niCmAwWRYg44DYk .sectionTitle0{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .sectionTitle1{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .sectionTitle2{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .sectionTitle3{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-8niCmAwWRYg44DYk .grid .tick text{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .grid path{stroke-width:0}#mermaid-svg-8niCmAwWRYg44DYk .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-8niCmAwWRYg44DYk .task{stroke-width:2}#mermaid-svg-8niCmAwWRYg44DYk .taskText{text-anchor:middle;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .taskText:not([font-size]){font-size:11px}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-8niCmAwWRYg44DYk .task.clickable{cursor:pointer}#mermaid-svg-8niCmAwWRYg44DYk .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8niCmAwWRYg44DYk .taskText0,#mermaid-svg-8niCmAwWRYg44DYk .taskText1,#mermaid-svg-8niCmAwWRYg44DYk .taskText2,#mermaid-svg-8niCmAwWRYg44DYk .taskText3{fill:#fff}#mermaid-svg-8niCmAwWRYg44DYk .task0,#mermaid-svg-8niCmAwWRYg44DYk .task1,#mermaid-svg-8niCmAwWRYg44DYk .task2,#mermaid-svg-8niCmAwWRYg44DYk .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutside0,#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutside2{fill:#000}#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutside1,#mermaid-svg-8niCmAwWRYg44DYk .taskTextOutside3{fill:#000}#mermaid-svg-8niCmAwWRYg44DYk .active0,#mermaid-svg-8niCmAwWRYg44DYk .active1,#mermaid-svg-8niCmAwWRYg44DYk .active2,#mermaid-svg-8niCmAwWRYg44DYk .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-8niCmAwWRYg44DYk .activeText0,#mermaid-svg-8niCmAwWRYg44DYk .activeText1,#mermaid-svg-8niCmAwWRYg44DYk .activeText2,#mermaid-svg-8niCmAwWRYg44DYk .activeText3{fill:#000 !important}#mermaid-svg-8niCmAwWRYg44DYk .done0,#mermaid-svg-8niCmAwWRYg44DYk .done1,#mermaid-svg-8niCmAwWRYg44DYk .done2,#mermaid-svg-8niCmAwWRYg44DYk .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-8niCmAwWRYg44DYk .doneText0,#mermaid-svg-8niCmAwWRYg44DYk .doneText1,#mermaid-svg-8niCmAwWRYg44DYk .doneText2,#mermaid-svg-8niCmAwWRYg44DYk .doneText3{fill:#000 !important}#mermaid-svg-8niCmAwWRYg44DYk .crit0,#mermaid-svg-8niCmAwWRYg44DYk .crit1,#mermaid-svg-8niCmAwWRYg44DYk .crit2,#mermaid-svg-8niCmAwWRYg44DYk .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-8niCmAwWRYg44DYk .activeCrit0,#mermaid-svg-8niCmAwWRYg44DYk .activeCrit1,#mermaid-svg-8niCmAwWRYg44DYk .activeCrit2,#mermaid-svg-8niCmAwWRYg44DYk .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-8niCmAwWRYg44DYk .doneCrit0,#mermaid-svg-8niCmAwWRYg44DYk .doneCrit1,#mermaid-svg-8niCmAwWRYg44DYk .doneCrit2,#mermaid-svg-8niCmAwWRYg44DYk .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-8niCmAwWRYg44DYk .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-8niCmAwWRYg44DYk .milestoneText{font-style:italic}#mermaid-svg-8niCmAwWRYg44DYk .doneCritText0,#mermaid-svg-8niCmAwWRYg44DYk .doneCritText1,#mermaid-svg-8niCmAwWRYg44DYk .doneCritText2,#mermaid-svg-8niCmAwWRYg44DYk .doneCritText3{fill:#000 !important}#mermaid-svg-8niCmAwWRYg44DYk .activeCritText0,#mermaid-svg-8niCmAwWRYg44DYk .activeCritText1,#mermaid-svg-8niCmAwWRYg44DYk .activeCritText2,#mermaid-svg-8niCmAwWRYg44DYk .activeCritText3{fill:#000 !important}#mermaid-svg-8niCmAwWRYg44DYk .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk g.classGroup text{fill:#9370db;stroke:none;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:10px}#mermaid-svg-8niCmAwWRYg44DYk g.classGroup text .title{font-weight:bolder}#mermaid-svg-8niCmAwWRYg44DYk g.clickable{cursor:pointer}#mermaid-svg-8niCmAwWRYg44DYk g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-8niCmAwWRYg44DYk g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-8niCmAwWRYg44DYk .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-8niCmAwWRYg44DYk .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-8niCmAwWRYg44DYk .dashed-line{stroke-dasharray:3}#mermaid-svg-8niCmAwWRYg44DYk #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk .commit-id,#mermaid-svg-8niCmAwWRYg44DYk .commit-msg,#mermaid-svg-8niCmAwWRYg44DYk .branch-label{fill:lightgrey;color:lightgrey;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .slice{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-8niCmAwWRYg44DYk g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-8niCmAwWRYg44DYk g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-8niCmAwWRYg44DYk g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-8niCmAwWRYg44DYk .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-8niCmAwWRYg44DYk .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-8niCmAwWRYg44DYk .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-8niCmAwWRYg44DYk .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-8niCmAwWRYg44DYk .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-8niCmAwWRYg44DYk .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-8niCmAwWRYg44DYk .edgeLabel text{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8niCmAwWRYg44DYk .node circle.state-start{fill:black;stroke:black}#mermaid-svg-8niCmAwWRYg44DYk .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-8niCmAwWRYg44DYk #statediagram-barbEnd{fill:#9370db}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-state .divider{stroke:#9370db}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-8niCmAwWRYg44DYk .note-edge{stroke-dasharray:5}#mermaid-svg-8niCmAwWRYg44DYk .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{–mermaid-font-family: ‘“trebuchet ms”, verdana, arial’;–mermaid-font-family: “Comic Sans MS”, “Comic Sans”, cursive}#mermaid-svg-8niCmAwWRYg44DYk .error-icon{fill:#522}#mermaid-svg-8niCmAwWRYg44DYk .error-text{fill:#522;stroke:#522}#mermaid-svg-8niCmAwWRYg44DYk .edge-thickness-normal{stroke-width:2px}#mermaid-svg-8niCmAwWRYg44DYk .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-8niCmAwWRYg44DYk .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-8niCmAwWRYg44DYk .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-8niCmAwWRYg44DYk .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-8niCmAwWRYg44DYk .marker{fill:#333}#mermaid-svg-8niCmAwWRYg44DYk .marker.cross{stroke:#333} :root { --mermaid-font-family: “trebuchet ms”, verdana, arial;} #mermaid-svg-8niCmAwWRYg44DYk { color: rgba(0, 0, 0, 0.75); font: ; }
运算符
算数运算符
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!
![img](https://i-blog.csdnimg.cn/blog_migrate/edd01a5cbe61150b887abac75fec08c1.jpeg)
总结
上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。
很多人担心学了容易忘,这里教你一个方法,那就是重复学习。
打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。
从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。
人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。
于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频**
如果你觉得这些内容对你有帮助,可以扫码领取!
![img](https://i-blog.csdnimg.cn/blog_migrate/edd01a5cbe61150b887abac75fec08c1.jpeg)
总结
上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。
很多人担心学了容易忘,这里教你一个方法,那就是重复学习。
打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。
从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。
[外链图片转存中…(img-TpNpMquE-1711387468307)]
人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。
需要更多Java资料的小伙伴可以帮忙点赞+关注,点击传送门,即可免费领取!