TIPS
关于后缀
- long/int
1
long x=9999999999L;//超出2^31必须加L/l
- double/float关于转换
1
float x=10.0F;//不加F/f将丢失精度,默认double型
- 自动转换
数值型数据的转换:byte→short→int→long→float→double
字符型转换为整型:char→int
实例程序:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99/*
PrimitiveOverloading.java, 拓宽类型的方法过载
*/
public class PrimitiveOverloading {
// 布尔类型不能自动转换
static void prt(String s) {System.out.println(s);}
void f1(char x) {prt("f1(char)"); }
void f1(byte x) {prt("f1(byte)"); }
void f1(short x) {prt("f1(short)"); }
void f1(int x) {prt("f1(int)");}
void f1(long x) {prt("f1(long)"); }
void f1(float x) {prt("f1(float)"); }
void f1(double x) { prt("f1(double)"); }
void f2(byte x) { prt("f2(byte)"); }
void f2(short x) { prt("f2(short)"); }
void f2(int x) { prt("f2(int)"); }
void f2(long x) { prt("f2(long)"); }
void f2(float x) { prt("f2(float)"); }
void f2(double x) { prt("f2(double)"); }
void f3(short x) { prt("f3(short)"); }
void f3(int x) { prt("f3(int)"); }
void f3(long x) { prt("f3(long)"); }
void f3(float x) { prt("f3(float)"); }
void f3(double x) { prt("f3(double)"); }
void f4(int x) { prt("f4(int)"); }
void f4(long x) { prt("f4(long)"); }
void f4(float x) { prt("f4(float)"); }
void f4(double x) { prt("f4(double)"); }
void f5(long x) { prt("f5(long)"); }
void f5(float x) { prt("f5(float)"); }
void f5(double x) { prt("f5(double)"); }
void f6(float x) { prt("f6(float)"); }
void f6(double x) { prt("f6(double)"); }
void f7(double x) { prt("f7(double)"); }
void testConstVal() {
prt("Testing with 5");
f1(5); f2(5);f3(5);f4(5); //java中常数的默认类型是int型,调用void fx(int x) { prt("fx(int)"); }
f5(5);//5为int型,f5无int参数,向上转换为f5(long x) { prt("f5(long)"); }
f6(5); //5为int型,f6无int、long参数方法,向上转换为void f6(float x) { prt("f6(float)"); }
f7(5);//5为int型,f7无int、long、float参数方法,向上转换为void f7(double x) { prt("f7(double)"); }
}
void testChar() {
char x = 'x';
prt("char argument:");
f1(x); //自动匹配void f1(char x) {prt("f1(char)"); }
f2(x);f3(x);f4(x); //'x'为char类型,向上转换为void fx(int x) { prt("fx(int)"); }
f5(x);//'x'为char类型,f5无char、int型,向上转换为f5(long x) { prt("f5(long)"); }
f6(x);//'x'为char类型,f6无char、int、long型,向上转换为void f6(float x) { prt("f6(float)"); }
f7(x);//'x'为char类型,f7无char、int、long、float型,向上转换为void f7(double x) { prt("f7(double)"); }
}
void testByte() {
byte x = 0;
prt("byte argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testShort() {
short x = 0;
prt("short argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testInt() {
int x = 0;
prt("int argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testLong() {
long x = 0;
prt("long argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testFloat() {
float x = 0;
prt("float argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testDouble() {
double x = 0;
prt("double argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
public static void main(String[] args) {
PrimitiveOverloading p = new PrimitiveOverloading();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testFloat();
p.testDouble();
}
} - 变量声明作用域在一个块内(一对大括号内)
- 当两个类创建在同一个项目下,同一个包下则可以不使用import,就可以直接调用另一个类
例
就是打印字符串,只是使用类来做了数据储存,类似结构体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16class MyDate {
int year;
int month;
int day;
}
public class MyDateTest{
public static void main(String[] args){
MyDate date = new MyDate();
date.year = 2021;
date.month = 9;
date.day = 22;
System.out.println("The day is " + date.year
+ "-"+ date.month + "-"+ date.day);
}
}
下面看一个复杂一点的:再看一个带返回值的:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40package test;
class Location {
int a=-1, b=-1;
}
class Point {
int x, y;
Point() {
x = 20;
y = 30;
}
void getData(int xValue, int yValue) {
xValue = 100;
yValue = 200;
}
void getLocation(Location locRef) {
locRef.a = x;//就是20
locRef.b = y;
}
}
public class test0 {
public static void main(String[] args){
Point p = new Point();
System.out.println("Object p’s value:");
System.out.println("p.x = " + p.x +" p.y = " + p.y);
int xVal = -1, yVal = -1;
p.getData(xVal,yVal);//传参不改变原值
System.out.println("Pass by value:");
System.out.println("xVal = " + xVal +" yVal = " + yVal);
Location loc = new Location();
p.getLocation(loc);//引用可改变值
System.out.println("Pass by reference:");
System.out.println("xVal = " + loc.a + " yVal = " +loc.b);
}
}基本和C一样1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45import java.util.Scanner;
public class TestReturnValue{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int testScore = input.nextInt();
printGrade(testScore) ; //调用返回类型Void方法
char ret=getGrade(testScore); //调用返回类型非Void方法
System.out.println(ret);
}
public static void printGrade(double testscore){ //返回类型为void,可以有return,但是不能带表示式。
if (testscore<0 || testscore>100){
System.out.println("成绩输入错误");
return; //中断程序,该方法后面语句不会被执行,可以试一下,如果注释掉该行的执行情况
}
if (testscore>=90.0){
System.out.println('A');
}else if(testscore>=80.0){
System.out.println('B');
}else if(testscore>=70.0){
System.out.println('C');
}else if(testscore>=60.0){
System.out.println('D');
}else {
System.out.println('F');
}
}
public static char getGrade(double testscore){ //返回类型非void,则需要return+表示式。
if (testscore<0 || testscore>100){
return 'N';
}else if (testscore>=90.0){
return 'A';
}else if(testscore>=80.0){
return 'B';
}else if(testscore>=70.0){
return 'C';
}else if(testscore>=60.0){
return 'D';
}else {
return 'F';
}
}
}