java基础语法

Java基础语法

2.1 Java的注释
范例2-1:单行注释。
public class TestDemo {
public static void main(String[] args) {
// 此处为注释,编译代码时不编译
System.out.println(“Hello World .”);
}
}

范例2-2:多行注释。
public class TestDemo {
public static void main(String[] args) {
/*
* 此处为多行注释,编译代码时不编译
*
/
System.out.println(“Hello world.”);
}
}
范例2-3:使用文档注释。
/
*

  • 此处为文档注释

*/
public class TestDemo {
public static void main(String[] args) {
System.out.println(“Hello ck.”);
}
}
范例2-4:利用中文定义标识符。
public class 你好 { // 类名称
public static void main(String args[]) {
int 年龄 = 20 ; // 变量名称
System.out.println(年龄) ; // 输出内容
}
}

2.3 数据类型划分
范例2-5:定义int型变量。
public class TestDemo {
public static void main(String args[]) {
// 为变量设置内容使用如下格式:数据类型 变量名称 = 常量 ;
int num = 10 ; // 10是常量,常量的默认类型是int
int result = num * 2 ; // 利用num变量的内容乘以2,并且将其赋值给result
System.out.println(result) ; // 输出result变量
}
}
范例2-6:观察变量与常量的区别。
public class TestDemo {
public static void main(String args[]) {
// 所有的变量名称在同一块代码中只允许声明一次
int num = 10 ; // 10是常量,常量的默认类型是int
// 取出num变量的内容乘以2,并且将其设置给num变量
num = num * 2 ;
System.out.println(num) ;
}
}
范例2-7:如果超过了int的最大值或最小值的结果。
public class TestDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE ; // 取出最大值
int min = Integer.MIN_VALUE ; // 取出最小值
System.out.println(max) ; // 2147483647
System.out.println(min) ; // -2147483648
// int变量 ± int型常量 = int型数据
System.out.println(max + 1) ; // 最大值加1:-2147483648
System.out.println(min - 1) ; // 最小值减1:2147483647
System.out.println(min - 2) ; // 最小值减2:2147483646
}
}
范例2-8:扩大数据类型。
public class TestDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE; // 取出最大值
int min = Integer.MIN_VALUE; // 取出最小值
// int变量 ± long型常量 = long型数据
System.out.println(max + 1L); // 最大值加1:2147483648
System.out.println(min - (long) 1); // 最小值减1:-2147483649
// long变量 ± int型常量 = long型数据
System.out.println((long) min - 2); // 最小值减2:-2147483650
}
}
范例2-9:将范围大的数据类型变为范围小的数据类型。
public class TestDemo {
public static void main(String args[]) {
long num = 1000 ; // 1000常量是int型,使用long接受,发生了向大范围转型
int x = (int) num ; // 把long变为int
System.out.println(x) ;
}
}
范例2-10:观察发生溢出的转换问题。
public class TestDemo {
public static void main(String args[]) {
long num = 2147483650L ; // 该数据已经超过了int数据范围
int x = (int) num ; // 把long变为int
System.out.println(x) ;
}
}
范例2-11:观察byte转换。
public class TestDemo {
public static void main(String args[]) {
int num = 130 ; // 此范围超过了byte定义
byte x = (byte) num ; // 由int变为byte
System.out.println(x) ;
}
}
范例2-12:观察byte自动转型的操作。
public class TestDemo {
public static void main(String args[]) {
byte num = 100 ; // 100没有超过byte的保存范围
System.out.println(num) ; // 输出byte变量的内容
}
}
范例2-13:定义变量时不设置内容,使用变量前设置内容。
public class TestDemo {
public static void main(String args[]) {
int num; // 没有默认值
num = 0; // 在使用此变量之前设置内容
System.out.println(num);
}
}
范例2-14:定义小数。
public class TestDemo {
public static void main(String args[]) {
double num = 10.2 ; // 10.2是一个小数所以属于double型
// double型 * int型(转化为double,2.0) = double型
System.out.println(num * 2) ;
}
}
范例2-15:使用float型。
public class TestDemo {
public static void main(String args[]) {
float f1 = 10.2F ; // 小数都是double型,所以需要强制转换为float型
float f2 = (float)10.2 ; // 小数都是double型,所以需要强制转换为float型
System.out.println(f1 * f2) ; // float类型 * float类型 = float类型
}
}
范例2-16:关于除法的问题。
public class TestDemo {
public static void main(String args[]) {
int x = 9; // 声明整型变量
int y = 5; // 声明整型变量
System.out.println(x / y); // int型 ÷ int型 = int型
}
}
范例2-17:解决除法计算精度。
public class TestDemo {
public static void main(String args[]) {
int x = 9; // 声明整型变量
int y = 5; // 声明整型变量
System.out.println(x / (double) y); // 将其中一个int类型变量转换为double类型
}
}
范例2-18:定义字符。
public class TestDemo {
public static void main(String args[]) {
char c = ‘A’ ; // 字符
int num = c ; // 字符可以和int型互相转换(以编码的形式出现)
System.out.println© ;
System.out.println(num) ;
}
}
范例2-19:实现字母大小写转换
public class TestDemo {
public static void main(String args[]) {
char c = ‘A’; // 大写字母
int num = c; // 需要将字符变为int型才可以使用加法计算
num = num + 32; // 变为小写字母的编码
c = (char) num; // 将int变为char型
System.out.println©;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值