第一章 –– Java基础语法

2022-12-26,,

第一章 –– Java基础语法

<!--
.wiz-editor-body .wiz-code-container{position: relative; padding:8px 0; margin: 5px 25px 5px 5px;text-indent:0; text-align:left;}.CodeMirror {font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: black; font-size: 10.5pt; font-size: 0.875rem}.wiz-editor-body .wiz-code-container .CodeMirror div {margin-top: 0; margin-bottom: 0;}.CodeMirror-lines {padding: 4px 0;}.CodeMirror pre {padding: 0 4px;}.CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {background-color: white;}.CodeMirror-gutters {border-right: 1px solid #ddd; background-color: #f7f7f7; white-space: nowrap;}.CodeMirror-linenumbers {}.CodeMirror-linenumber {padding: 0 3px 0 5px; min-width: 20px; text-align: right; color: #999; white-space: nowrap;}.CodeMirror-guttermarker {color: black;}.CodeMirror-guttermarker-subtle {color: #999;}.CodeMirror-cursor {border-left: 1px solid black; border-right: none; width: 0;}.CodeMirror div.CodeMirror-secondarycursor {border-left: 1px solid silver;}.cm-fat-cursor .CodeMirror-cursor {width: auto; border: 0 !important; background: #7e7;}.cm-fat-cursor div.CodeMirror-cursors {z-index: 1;}.cm-animate-fat-cursor {width: auto; border: 0; -webkit-animation: blink 1.06s steps(1) infinite; -moz-animation: blink 1.06s steps(1) infinite; animation: blink 1.06s steps(1) infinite; background-color: #7e7;}@-moz-keyframes blink { 0% {} 50% { background-color: transparent; } 100% {}}@-webkit-keyframes blink { 0% {} 50% { background-color: transparent; } 100% {}}@keyframes blink { 0% {} 50% { background-color: transparent; } 100% {}}.CodeMirror-overwrite .CodeMirror-cursor {}.cm-tab { display: inline-block; text-decoration: inherit; }.CodeMirror-rulers {position: absolute; left: 0; right: 0; top: -50px; bottom: -20px; overflow: hidden;}.CodeMirror-ruler {border-left: 1px solid #ccc; top: 0; bottom: 0; position: absolute;}.cm-s-default .cm-header {color: blue;}.cm-s-default .cm-quote {color: #090;}.cm-negative {color: #d44;}.cm-positive {color: #292;}.cm-header, .cm-strong {font-weight: bold;}.cm-em {font-style: italic;}.cm-link {text-decoration: underline;}.cm-strikethrough {text-decoration: line-through;}.cm-s-default .cm-keyword {color: #708;}.cm-s-default .cm-atom {color: #219;}.cm-s-default .cm-number {color: #164;}.cm-s-default .cm-def {color: #00f;}.cm-s-default .cm-variable,.cm-s-default .cm-punctuation,.cm-s-default .cm-property,.cm-s-default .cm-operator {}.cm-s-default .cm-variable-2 {color: #05a;}.cm-s-default .cm-variable-3 {color: #085;}.cm-s-default .cm-comment {color: #a50;}.cm-s-default .cm-string {color: #a11;}.cm-s-default .cm-string-2 {color: #f50;}.cm-s-default .cm-meta {color: #555;}.cm-s-default .cm-qualifier {color: #555;}.cm-s-default .cm-builtin {color: #30a;}.cm-s-default .cm-bracket {color: #997;}.cm-s-default .cm-tag {color: #170;}.cm-s-default .cm-attribute {color: #00c;}.cm-s-default .cm-hr {color: #999;}.cm-s-default .cm-link {color: #00c;}.cm-s-default .cm-error {color: #f00;}.cm-invalidchar {color: #f00;}.CodeMirror-composing { border-bottom: 2px solid; }div.CodeMirror span.CodeMirror-matchingbracket {color: #0f0;}div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}.CodeMirror-matchingtag { background: rgba(255, 150, 0, .3); }.CodeMirror-activeline-background {background: #e8f2ff;}.CodeMirror {position: relative; background: #f5f5f5;}.CodeMirror-scroll {overflow: hidden !important; margin-bottom: 0; margin-right: -30px; padding: 16px 30px 16px 0; outline: none; position: relative;}.CodeMirror-sizer {position: relative; border-right: 30px solid transparent;}.CodeMirror-vscrollbar, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {position: absolute; z-index: 6; display: none;}.CodeMirror-vscrollbar {right: 0; top: 0; overflow-x: hidden; overflow-y: scroll;}.CodeMirror-hscrollbar {bottom: 0; left: 0 !important; overflow-y: hidden; overflow-x: scroll;}.CodeMirror-scrollbar-filler {right: 0; bottom: 0;}.CodeMirror-gutter-filler {left: 0; bottom: 0;}.CodeMirror-gutters {position: absolute; left: 0; top: -5px; min-height: 100%; z-index: 3;}.CodeMirror-gutter {white-space: normal; height: inherit; display: inline-block; vertical-align: top; margin-bottom: -30px;}.CodeMirror-gutter-wrapper {position: absolute; z-index: 4; background: none !important; border: none !important;}.CodeMirror-gutter-background {position: absolute; top: 0; bottom: 0; z-index: 4;}.CodeMirror-gutter-elt {position: absolute; cursor: default; z-index: 4; text-align: center;}.CodeMirror-gutter-wrapper ::selection { background-color: transparent }.CodeMirror-gutter-wrapper ::-moz-selection { background-color: transparent }.CodeMirror-lines {cursor: text; min-height: 1px;}.CodeMirror pre {-moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0; border-width: 0; background: transparent; font-family: inherit; font-size: inherit; margin: 0; white-space: pre; word-wrap: normal; line-height: inherit; color: inherit; z-index: 2; position: relative; overflow: visible; -webkit-tap-highlight-color: transparent; -webkit-font-variant-ligatures: contextual; font-variant-ligatures: contextual;}.CodeMirror-wrap pre {word-wrap: break-word; white-space: pre-wrap; word-break: normal;}.CodeMirror-linebackground {position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: 0;}.CodeMirror-linewidget {position: relative; z-index: 2; overflow: auto;}.CodeMirror-widget {}.CodeMirror-rtl pre { direction: rtl; }.CodeMirror-code {outline: none;}.CodeMirror-scroll,.CodeMirror-sizer,.CodeMirror-gutter,.CodeMirror-gutters,.CodeMirror-linenumber {-moz-box-sizing: content-box; box-sizing: content-box;}.CodeMirror-measure {position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden;}.CodeMirror-cursor {position: absolute; pointer-events: none;}.CodeMirror-measure pre { position: static; }div.CodeMirror-cursors {visibility: hidden; position: relative; z-index: 3;}div.CodeMirror-dragcursors {visibility: visible;}.CodeMirror-focused div.CodeMirror-cursors {visibility: visible;}.CodeMirror-selected { background: #d9d9d9; }.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }.CodeMirror-crosshair { cursor: crosshair; }.CodeMirror-line::selection, .CodeMirror-line >--> span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
<!--
.cm-s-tomorrow-night-eighties.CodeMirror { background: #000000; color: #CCCCCC; }.cm-s-tomorrow-night-eighties div.CodeMirror-selected { background: #2D2D2D; }.cm-s-tomorrow-night-eighties .CodeMirror-line::selection, .cm-s-tomorrow-night-eighties .CodeMirror-line > span::selection, .cm-s-tomorrow-night-eighties .CodeMirror-line > span > span::selection { background: rgba(45, 45, 45, 0.99); }.cm-s-tomorrow-night-eighties .CodeMirror-line::-moz-selection, .cm-s-tomorrow-night-eighties .CodeMirror-line > span::-moz-selection, .cm-s-tomorrow-night-eighties .CodeMirror-line > span > span::-moz-selection { background: rgba(45, 45, 45, 0.99); }.cm-s-tomorrow-night-eighties .CodeMirror-gutters { background: #000000; border-right: 0px; }.cm-s-tomorrow-night-eighties .CodeMirror-guttermarker { color: #f2777a; }.cm-s-tomorrow-night-eighties .CodeMirror-guttermarker-subtle { color: #777; }.cm-s-tomorrow-night-eighties .CodeMirror-linenumber { color: #515151; }.cm-s-tomorrow-night-eighties .CodeMirror-cursor { border-left: 1px solid #6A6A6A; }.cm-s-tomorrow-night-eighties span.cm-comment { color: #d27b53; }.cm-s-tomorrow-night-eighties span.cm-atom { color: #a16a94; }.cm-s-tomorrow-night-eighties span.cm-number { color: #a16a94; }.cm-s-tomorrow-night-eighties span.cm-property, .cm-s-tomorrow-night-eighties span.cm-attribute { color: #99cc99; }.cm-s-tomorrow-night-eighties span.cm-keyword { color: #f2777a; }.cm-s-tomorrow-night-eighties span.cm-string { color: #ffcc66; }.cm-s-tomorrow-night-eighties span.cm-variable { color: #99cc99; }.cm-s-tomorrow-night-eighties span.cm-variable-2 { color: #6699cc; }.cm-s-tomorrow-night-eighties span.cm-def { color: #f99157; }.cm-s-tomorrow-night-eighties span.cm-bracket { color: #CCCCCC; }.cm-s-tomorrow-night-eighties span.cm-tag { color: #f2777a; }.cm-s-tomorrow-night-eighties span.cm-link { color: #a16a94; }.cm-s-tomorrow-night-eighties span.cm-error { background: #f2777a; color: #6A6A6A; }.cm-s-tomorrow-night-eighties .CodeMirror-activeline-background { background: #343600; }.cm-s-tomorrow-night-eighties .CodeMirror-matchingbracket { text-decoration: underline; color: white !important; }
-->
<!--
html, body {font-size: 12pt;}body {font-family: Helvetica, "Hiragino Sans GB", "微软雅黑", "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif;line-height: 1.6;margin: 0 auto;padding: 20px 16px;padding: 1.25rem 1rem;}h1, h2, h3, h4, h5, h6 {margin:20px 0 10px;margin:1.25rem 0 0.625rem;padding: 0;font-weight: bold;}h1 {font-size:20pt;font-size:1.67rem;}h2 {font-size:18pt;font-size:1.5rem;}h3 {font-size:15pt;font-size:1.25rem;}h4 {font-size:14pt;font-size:1.17rem;}h5 {font-size:12pt;font-size:1rem;}h6 {font-size:12pt;font-size:1rem;color: #777777;margin: 1rem 0;}div, p, ul, ol, dl, li {margin:0;}blockquote, table, pre, code {margin:8px 0;}ul, ol {padding-left:32px;padding-left:2rem;}ol.wiz-list-level1 > li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-level3 > li {list-style-type:lower-roman;}blockquote {padding:0 12px;padding:0 0.75rem;}blockquote > :first-child {margin-top:0;}blockquote > :last-child {margin-bottom:0;}img {border:0;max-width:100%;height:auto !important;margin:2px 0;}table {border-collapse:collapse;border:1px solid #bbbbbb;}td, th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-all;box-sizing: border-box;}.wiz-hide {display:none !important;}
-->

一丶运算的前提是数据类型必须一致(掌握)

(1) 观点一:(掌握)

// A: byte short char-int-long-float-double 默认大小转化时向最大的数据类型靠近, (除了 下面的观点的数据类型外)

// B: byte short char 在运算时默认转化成int 类型进行运算

// A: byte short char-int-long-float-double

// B: byte short char 在运算时默认转化成int 类型进行运算

 

 
 

14

14

 
 
 

 
 

1

 @Test

2

 public void test1()

3

 {

4

  byte b = 4;

5

  int i = 3;

6


7

//  int sum = b + i;

8

//  byte sum = b + i;

9

  short s = 2;

10

  byte b1 = 1;

11

  // (1) 验证成功, byte short char 在运算时默认转化成int 类型进行运算

12

//  short sum = s + b1;

13

  System.out.println("sum = " + sum);

14

 }

 
 

注意: double和float精度不正确问题,二进制问题,使用大数据类型BigDecimal

(2) 面试题:(掌握)

/*

面试题:

byte b1=3,b2=4,b;

b=b1+b2;

b=3+4;

哪句是编译失败的呢?为什么呢?

b = b1 + b2;是有问题的。

因为变量相加,会首先看类型问题,最终把结果赋值的也会考虑类型问题。

常量相加,首先做加法,然后看结果是否在赋值的数据类型范围内,如果不是,才报错。

*/

 

 
 

11

11

 
 
 

 
 

1

class DataTypeDemo6 {

2

 public static void main(String[] args) {

3

  //定义了三个byte类型的变量,b1,b2,b3

4

  //b1的值是3,b2的值是4,b没有值

5

  byte b1 = 3,b2 = 4,b;

6


7

  //b = b1 + b2; //这个是类型提升,所有有问题

8


9

  b = 3 + 4; //常量,先把结果计算出来,然后看是否在byte的范围内,如果在就不报错。

10

 }

11

}

 
 

注意:(掌握)

常量之间的运算结果是在编译成字节码文件时便被计算完毕, 成为一个新的常量例如: int a = 3 + 7 编译后便成为 int a = 10, 即使是  int a = 0;  a = 3 + 7;, 在编译时也会变成 int a = 3 + 7; 这就是编译器的优化效果, 但是常量是存在数据类型, 从 c++ 方面便可简单的看出来  const int a = 10; 这个便是常量, 在java中也是存在常量 的  例如 : private final String str = "aaaa"还存在常量对象, private final String str1 = new String("aaa");还有 从 float 和 double 类型的常量可以看出来常量存在数据类型. 0.01f这些都是常量

(3)案例思考题:(掌握)

 

 

 
 

17

17

 
 
 

 
 

1

// 思考题5

2

 // 'a' = 97

3

 @Test

4

 public void test6()

5

 {

6

  System.out.println('a');

7

  // 这个将被虚拟机设置成 98

8

  System.out.println('a' + 1);

9

  // 这个在编译期间就被编译成 "helloa1"

10

  System.out.println("hello" + 'a' + 1);

11

  // "98hello"

12

  System.out.println('a' + 1 + "hello");

13

  // 5 + 5 = 55

14

  System.out.println("5 + 5 = " + 5 + 5);

15

  // 10= 5 + 5

16

  System.out.println(5 + 5 + "= 5 + 5");

17

 }

 
 

 

 

 
 

9

9

 
 
 

 
 

1

 // 思考题4

2

 // byte b = (byte)120; -127 ~ 126;

3

 @Test

4

 public void test5()

5

 {

6

  // byte b = -126;

7

  byte b = (byte)130;

8

  System.out.println(b);

9

 }

 
 

 

 
 

17

17

 
 
 

 
 

1

 // 思考题3:

2

 // byte b1 = 3, b2 = 4, b;

3

 // b = b1 + b2;

4

 // b = 3 + 4;

5

 @Test

6

 public void test4()

7

 {

8

   byte b1 = 3, b2 = 4, b;

9

   // b1 b2 在运行时, 类型提升了, 所以有问题

10

//  b = b1 + b2;

11

   // 常量, 先把结果计算出来, 然后看是否在 byte 的范围内,

12

   // 如果不在就报错

13

   b = 3 + 4;

14

   System.out.println(b);

15

   b = 10;

16

   System.out.println(b);

17

 }

 
 

 

 

 
 

13

13

 
 
 

 
 

1

 // 思考题2:

2

 // 下面这两种方法有没有区别??

3

 // float f = (float)12.345;

4

 // float f = 12.345f;

5


6

 // 一个是强制转化, 一个是原先就是 float 类型

7

 @Test

8

 public void test3()

9

 {

10

  // 但是我在反编译之后发现 这下面两句都一样的处理方式

11

   float f = (float)12.345;

12

   float f1 = 12.345f;

13

 }

 
 

 
 

10

10

 
 
 

 
 

1

 // 思考题1:

2

 // double d = 12.345;

3

 // float f = d; ???

4

 @Test

5

 public void test2()

6

 {

7

  double d = 12.345;

8

  float f = (float) d;

9

  System.out.println(f);

10

 }

 
 

 
 

16

16

 
 
 

 
 

1

 // A: byte short char-int-long-float-double

2

 // B: byte short char 在运算时默认转化成int 类型进行运算

3

 @Test

4

 public void test1()

5

 {

6

  byte b = 4;

7

  int i = 3;

8


9

  //    int sum = b + i;

10

  //    byte sum = b + i;

11

  short s = 2;

12

  byte b1 = 1;

13

  // (1) 验证成功, byte short char 在运算时默认转化成int 类型进行运算

14

//  short sum = s + b1;

15

//  System.out.println("sum = " + sum);

16

 }

 
 

(4) 交换两个变量的方式, 面试题(掌握)

 

 
 

32

32

 
 
 

 
 

1

// 思考题1

2

 // 交换两个变量值的话题, 解决方案, 平时使用和面试使用

3

 @Test

4

 public void test1()

5

 {

6

  int a = 10;

7

  int b = 20;

8


9

// 方式一: 使用第三发变量存储 工作中使用...

10


11

//  int tmp = a;

12

//  a = b;

13

//  b = tmp;

14

//  System.out.println("a = " + a + " b = " + b);

15


16

// 方式二: 面试专用, 使用 a ^ b ^ b ==> a 的方法

17

// 效率最高

18

//  a = a ^ b;

19

//  b = a ^ b;  // b = a == (a ^ b) ^ b;

20

//  a = a ^ b;  // a = b == (a ^ b) ^ a

21

//  System.out.println("a = " + a + " b = " + b);

22


23

// 方式三: 用变量相加减的方法

24

//  a = a + b;

25

//  b = a - b;

26

//  a = a - b;

27

//  System.out.println("a = " + a + " b = " + b);

28


29

  // 方式四: 一句话搞定

30

  b = (a + b) - (a = b);

31

  System.out.println("a = " + a + " b = " + b);

32

 }

 
 

(5) 运算符与和或(掌握)

 
 

28

28

 
 
 

 
 

1

// 逻辑 && || 和 位 的 & 和 | 区分

2

    // && 和 || 的短路效果

3

    // && 出现左边是 false 右边就不执行了

4

    // || 出现左边是 true 右边就不执行了

5

    @Test

6

    public void test2()

7

    {

8

        boolean a = false;

9

        boolean b = true;

10

        System.out.println("(1)");

11

        System.out.println("a = " + a);

12

        System.out.println("b = " + b);

13

        if (a != true || (b = false))

14

        {

15

            // 输出的值 是 true

16

            System.out.println("b = " + b);

17

        }

18


19

        a = false;

20

        b = true;

21

        System.out.println("(2)");

22

        System.out.println("a = " + a);

23

        System.out.println("b = " + b);

24

        if (b == false && (a = true))

25

        {

26

            System.out.println("a = " + a);

27

        }

28

    }

 
 

(6) 面试题

 

 
 

20

20

 
 
 

 
 

1

/*

2

     * 面试题1

3

     * (1)

4

     * short s = 1;

5

     * s = s + 1;

6

     * (2)

7

     *  short s = 1;

8

     * s += 1;

9

     * */

10

@Test

11

    public void test1()

12

    {

13

        short s = 1;

14

        // 还是同一个问题

15

        // 变量(short char byte )在运算的时候会被上升为 int

16

        // 类型, 这个时候计算完毕, 但是赋值失败, 类型不符合

17

        //      s = s + 1;

18

        s += 1;

19

        System.out.println(s);

20

    }

 
 

(7) 位移运算符(要会)

// << 左移: 左边最高位置丢弃, 右边补齐0

// >> 右移: 最高位是0 左边补齐 0; 最高位是1 左边补齐1

// >>> 无符号右移: 无论最高位是 0 还是 1, 左边补齐 0

 

 
 

44

44

 
 
 

 
 

1

@Test

2

    public void test1()

3

    {

4

        //<< 把<<左边的数据乘以2的移动次幂

5

        System.out.println(3 << 2);// 12

6

        

7

        //>> 把>>左边的数据除以2的移动次幂

8

        System.out.println(24 >> 2);// 6

9

        System.out.println(24 >>> 2);// 6

10

        

11

        System.out.println(-24 >> 2);// 6

12

        System.out.println(-24 >>> 2);// 乱码

13

    }

14

    /*

15

    计算出3的二进制:11

16

        00000000 00000000 00000000 00000011

17

    (00)000000 00000000 00000000 0000001100

18

        

19

    >>的移动:  

20

    计算出24的二进制:11000

21

        原码:10000000 00000000 00000000 00011000

22

        反码:11111111 11111111 11111111 11100111

23

        补码:11111111 11111111 11111111 11101000

24

        

25

        11111111 11111111 11111111 11101000

26

        1111111111 11111111 11111111 111010(00) 补码

27

        

28

        补码:1111111111 11111111 11111111 111010

29

        反码:1111111111 11111111 11111111 111001

30

        原码:1000000000 00000000 00000000 000110

31

        

32

        结果:-6

33

        

34

    >>>的移动:

35

        计算出24的二进制:11000

36

        原码:10000000 00000000 00000000 00011000

37

        反码:11111111 11111111 11111111 11100111

38

        补码:11111111 11111111 11111111 11101000

39

        

40

        11111111 11111111 11111111 11101000

41

        0011111111 11111111 11111111 111010(00)

42

        

43

        结果:

44

*/

 
 

(8) 面试题

/*

面试题:

请用最有效率的方式写出计算2乘以8的结果?

2 * 8

2 << 3

*/

(9) 三目运算符(要会)

--------------------------------------------------------------------------------

// 1. 获得两个数的最大值

// 2. 获得三个数的最大值

// 3. 比较两个整数是否相等

 
 

34

34

 
 
 

 
 

1

@Test

2

    public void test3()

3

    {

4

        // 3. 比较两个整数是否相等

5

        int m = 20;

6

        int n = 10;

7

        boolean flag = (m == n) ? true : false;

8

        System.out.println(flag);

9

    }

10

    @Test

11

    public void test2()

12

    {

13

        // 2. 获得三个数的最大值

14

        int x = 10;

15

        int y = 30;

16

        int z = 20;

17

        

18

        // (1)

19

        int tmp = (x > y) ? x : y;

20

        int max = (tmp > y) ? tmp : y;

21

        System.out.println("max = " + max);

22

        // (2) 一步到位

23

        int max1 = (x > y) ? (x > z ? x : z) : (y > z ? y : z);

24

        System.out.println("max1 = " + max1);

25

    }

26

    @Test

27

    public void test1()

28

    {

29

        // 1. 获得两个数的最大值

30

        int x = 10;

31

        int y = 20;

32

        int max = (x > y) ? x : y;

33

        System.out.println("max = " + max);

34

    }

 
 

 

--------------------------------------------------------------------------------

注意:

 

 
 

4

4

 
 
 

 
 

1

// 写一个程序需要考虑:

2

    // (1) 成功的数据

3

    // (2) 错误的错误

4

    // (3) 边界问题

 
 


二、switch的使用方法

1. switch(表达式)中表达式的返回值必须是下述几种类型之一:byte,short,char,int,枚举,String;

2. case子句中的值必须是常量,且所有case子句中的值应是不同的;

3. default子句是可任选的,当没有匹配的case时,执行default

4. break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾

5. 附加跳出多层循环的方法,  goto tab;

三、方法的重载(overload)

1、重载的概念

在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。

2、重载的特点

与返回值类型无关,只看参数列表,且参数列表必须不同。(参数个数或参数类型)。调用时,根据方法参数列表的不同来区别。------ 这个和函数签名有关, 函数签名只要函数名和函数参数, 并没有函数的返回值

3. 重载示例

//返回两个整数的和

 

 
 

1

1

 
 
 

 
 

1

 int add(int x,int y){return x+y;}

 
 

//返回三个整数的和

 

 
 

1

1

 
 
 

 
 

1

int add(int x,int y,int z){return x+y+z;}

 
 

//返回两个小数的和

 

 
 

1

1

 
 
 

 
 

1

 double add(double x,double y){return x+y;}

 
 

 

 
 

9

9

 
 
 

 
 

1

public class PrintStream{

2

public static void print(int i) {……}

3

public static void print(float f) {……}

4

private static void print(String s) {……}

5

public static void main(String[] args){

6

    print(3);

7

    print(1.2f);

8

    print(“hello!”);

9

}

 
 

4. 判断方式:

与void show(int a,char b,double c){}构成重载的有:

 

 
 

7

7

 
 
 

 
 

1

a)void show(int x,char y,double z){}   //no

2

b)int show(int a,double c,char b){}   //yes

3

c)  void show(int a,double c,char b){}  //yes

4

d)  boolean show(int c,char b){}  //yes

5

e)  void show(double c){}  //yes

6

f)  double show(int x,char y,double z){}  //no

7

g)  void shows(){double c}  //no

 
 

函数重载只看参数个数和类型,不看返回值。

例子:

int biggest(int a,int b,int c);

{

......

}

float biggest(float a,float b)

{

......

}

float biggest(float a,float b,float c)

{

......

}

以上三个函数都可以。

但不能出现只是返回值不同的重载。

int biggest(int a,int b);

{

......

}

float biggest(int a,int b)

{

......

}

四、体会可变个数的形参

//下面采用数组形参来定义方法

 

 
 

1

1

 
 
 

 
 

1

public static void test(int a ,String[] books);

 
 

//以可变个数形参来定义方法

 

 
 

1

1

 
 
 

 
 

1

public static void test(int a ,String…books);

 
 

说明:

1.可变参数:方法参数部分指定类型的参数个数是可变多个

2.声明方式:方法名(参数的类型名...参数名)

3.可变参数方法的使用与方法参数部分使用数组是一致的

4.方法的参数部分有可变形参,需要放在形参声明的最后

 
 

17

17

 
 
 

 
 

1

public void test(String[] msg){

2

    System.out.println(“含字符串数组参数的test方法 ");

3

}

4

public void test1(String book){

5

    System.out.println(“****与可变形参方法构成重载的test1方法****");

6

}

7

public void test1(String ... books){

8

    System.out.println("****形参长度可变的test1方法****");

9

}

10

public static void main(String[] args){

11

    TestOverload to = new TestOverload();

12

    //下面两次调用将执行第二个test方法

13

    to.test1();

14

    to.test1("aa" , "bb");

15

    //下面将执行第一个test方法

16

    to.test(new String[]{"aa"});

17

}

 
 

五、方法的参数传递

1. 方法,必须有其所在类或对象调用才有意义。若方法含有参数:

Ø形参:方法声明时的参数

Ø实参:方法调用时实际传给形参的参数值

2. Java的实参值如何传入方法呢?

Java里方法的参数传递方式只有一种:值传递。  即将实际参数值的副本(复制品)传入方法内,而参数本身不受影响

六、字段名和属性名的概念

 

Bean层的属性名和变量名
属性名便是在方法 set 和 get 之后的名字便是属性名
属性名不再是变量的名字

 
 

3

 
 
 

 
 

1

Bean层的属性名和变量名

2

    属性名便是在方法 set 和 get 之后的名字便是属性名

3

    属性名不再是变量的名字

 
 

 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 
 
 
 
来自为知笔记(Wiz)

第一章 –– Java基础语法的相关教程结束。

《第一章 –– Java基础语法.doc》

下载本文的Word格式文档,以方便收藏与打印。