HelloWorld代码解读
/*
* 第一行: 声明类所在的包
* package: java的关键字,用来定义包
* com.baidu.hello: 包名 全小写 + 公司域名倒着写
* ; java语句的结束符
*/
package com.baidu.hello;
//单行注释
/*多行注释*/
/*
* 第二行: 定义一个类
* public: java的关键字,公共的公开的,修饰类是公共的
* class: java的关键字,用来定义一个类
* HelloWorld: 类名 (驼峰命名法)
*/
public class HelloWorld {
/*
* 定义main方法/主方法/主函数
* public: java关键字 公开的,修饰方法是公共的
* static: java关键字,修饰方法是静态的
* void: java关键字 空/无,修饰方法是没有返回值的
* main: 方法名 固定的名字
* (): 方法的标志
* String[] args 两部分由空格隔开 统称是方法的参数
*
*/
public static void main(String[] args) {
/*
* 向控制台输出数据
* System: 发出系统指令
* out: 执行输出动作
* println: 输出后,换行
* (): 方法的标志
* "hello world": 方法的参数
* ; java语句结束符
*/
System.out.println("Hello World");
}
}
标识符
标识符为类名、方法、字段、变量、包名等起名字。遵循以下规则:
1、由字母、数字、下划线、$ 组成
2.、不能以数字开头
3、严格区分大小写
4、标识符能反映其作用
关键字
| 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|
注: 关键字不能备用于标识符。另 const、goto 为保留字,也不要使用
注释
// 单行注释
/*
- 多行注释
/
/* 文本注释 */
变量与常量
变量 由 变量类型、变量名、变量值描述。
int age = 20; // 声明int 类型变量并赋值
age = 23; //对变量age 重新赋值
String name; // 声明String类型变量
注:变量名必须是一个有效的标识符,不可以使用java关键字。
变量使用规则:
1、JAVA中的变量需先声明后使用
2、变量使用时,可以声明的同时赋值,也可以先声明后赋值
常量 是程序运行过程中不能被改变的量 。常量必须在声明的同时初始化。(关于常量的定义在后续章节介绍)
数据类型
JAVA 的 数据类型 包括 基本类型 和 引用类型。引用类型后续章节进行介绍。
基本类型(八种)
每个基本数据类型都有对应的包装类型,其包装类型提供了很多方法。
package com.baidu.basic;
public class Test1_Scope {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte bmax = Byte.MAX_VALUE;
byte bmin = Byte.MIN_VALUE;
System.out.println("byte max:"+ bmax+" min:"+bmin);
short smax = Short.MAX_VALUE;
short smin = Short.MIN_VALUE;
System.out.println("short max:"+ smax+" min:"+smin);
int imax = Integer.MAX_VALUE;
int imin = Integer.MIN_VALUE;
System.out.println("int max:"+ imax+" min:"+imin);
long lmax = Long.MAX_VALUE;
long lmin = Long.MIN_VALUE;
System.out.println("long max:"+ lmax+" min:"+lmin);
float fmax = Float.MAX_VALUE;
float fmin = Float.MIN_VALUE;
System.out.println("float max:"+ fmax+" min:"+fmin);
double dmax = Double.MAX_VALUE;
double dmin = Double.MIN_VALUE;
System.out.println("double max:"+ dmax+" min:"+dmin);
char cmax = Character.MAX_VALUE;
char cmin = Character.MIN_VALUE;
System.out.println("char max:"+ cmax+" min:"+cmin);
char c = 'a';
char d = 97;
System.out.println("c="+c+",d="+d);
}
}
字符串连接
package com.baidu.basic;
public class Test2_Info {
public static void main(String[] args) {
String name = "重庆森林呼呼呼";
int age = 26;
String type = "java";
System.out.println("大家好,我叫"+name+",今年"+age+"岁,正在学习"+type);
}
}
计算圆的面积和周长(接收键盘输入的值作为半径r)
package com.baidu.basic;
// import 导入Scanner所需的包
import java.util.Scanner;
public class Test3_Area {
public static void main(String[] args) {
// double r = 5.5;
// 动态接受键盘输入的值
double r = new Scanner(System.in).nextDouble();
double result = 3.14 * r * r;
System.out.println("result =" + result);
double result2 = 2 * 3.14 * r;
System.out.println("result2 =" + result2);
}
}
交换两个数值
package com.baidu.basic;
import java.util.Scanner;
public class Test4_Exchange {
public static void main(String[] args) {
int a = new Scanner(System.in).nextInt();
int b = new Scanner(System.in).nextInt();
System.out.println("a ="+ a + ",b = "+ b);
// 交换两个值
int c = a;
a = b;
b = c;
System.out.println("a ="+ a + ",b = "+ b);
}
}
基本数据类型字面值
1、整数字面值默认是 int 类型
2、byte、short、char三种比 int 小的整数可以用范围内的值直接赋值
3、浮点数的字面值默认是 double 类型
4、字面值后缀 L、F、D
package com.baidu.basic;
public class Test5 {
public static void main(String[] args) {
// int a = 99999999999; // 错误,右边是 int 类型数据,但超出数据范围
// float b = 9.9; // 错误,浮点数默认是double类型
byte c = 127; // 正确
// byte d = 128; // 错误,右侧数据超出byte数据范围
long L = 99999999999999999L; // long 类型需加L/l 后缀
float b2 = 9.9f; // float 类型需加 F/f后缀
double e = 3d; // 加上d后缀可将int类型转为double类型
System.out.println(e);
}
}
基本类型的类型转换
数据类型由小转到大(隐式类型转换)
由大到小(强制转换)
package com.baidu.basic;
public class Test6 {
public static void main(String[] args) {
// 1、从小转到大 隐式转换
// int -> double
int a = 10;
double b = a;
// 2、大转小 强制转换
// long -> byte
long m = 5L;
byte n = (byte)m;
System.out.println(n);
}
}