Java语法基础(续)
网页整顿改不了3.18之前的,续上
类型转换
public class Demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;
System.out.print(i);
System.out.print(b);
//高到低需要强制转换
//低到高自动转换
//byte,short,char→int→long→float→double 小数优先级一定大于整数
int m = 127;
double n = m;
System.out.print(m);
System.out.print(n);
/*
1.不能对布尔值进行转换
2.不能把对象类型转换为无关类型
3.转换时存在内存溢出或精确度差别
*/
System.out.print("|");
System.out.print((int )23.7);
System.out.print((int)-45.89f);
System.out.print("|");
char c = 'a';
int d = c+1;
System.out.print(d);
System.out.print((char)d);
}
}
public class Demo05 {
public static void main(String[] args) {
//操纵比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years;//-1474836480 计算时溢出
long total2 = money*years;//-1474836480 先计算再转换,但计算完得出的结果已经为int类型了
long total3 = money*(long)years;//20000000000 计算前已经有一个为long类型
System.out.println(total);
System.out.println(total2);
System.out.println(total3);
//尽量用L不用l 看着像1
}
}
变量与常量
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class Demo07 {
//类型 方法/属性 名字 赋值
String name;//三
int age;//二
static double salary = 10000;//二
/*一.局部变量
1.从属于方法
2.必须初始化(赋值)
*/
public static void main(String[] args) {
int i = 1;
System.out.println(i);
/*
二.实例变量
1.从属于对象
2.可位于方法外
3.不能直接调用
4.不自行初始化输出会变成该类型的默认值(数字0/0.0/ 布尔值false 基本类型外给null)
*/
Demo07 demo07 = new Demo07();
System.out.println(demo07.age);
System.out.println(demo07.name);
/*
三.类变量
1.static
2.从属于类
*/
System.out.println(salary);
}
}
public class Demo08 {
//定义常量(final)随时调用
static final double PI = 3.14;//final static double PI = 3.14
public static void main(String[] args) {
System.out.println(PI);
}
}
变量命名规范
- 类成员变量,局部变量,方法名。首字母小写驼峰原则 firstName
- 常量。全大写 MAX_NUM
- 类名。首字母大写驼峰原则 Demo08
- 不用拼音
运算符
-
算术运算符:+,-,*,/,%,++,--
-
赋值运算符:=(a=10的意思是把10赋值给a)
-
关系运算符:>,<,>=,<=,==.!=
-
逻辑运算符:&&,||,!(与或非)
-
位运算符:&,|,^,~,>>,<<,>>>
-
条件运算符:?
-
扩展赋值运算符:+=,-=,/=,
package operator;
import java.sql.SQLOutput;
public class Demo01 {
public static void main(String[] args) {
//二元运算符号
int a = 10;
int b = 20;
int c = 30;
int d = 40;//Ctrl+D
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);//注意作用范围
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 1231432425234L;
int b = 142;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//有long输出为long,有double输出为double
System.out.println(b+c+d);
System.out.println(c+d);
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);//java里等于的符号为==
System.out.println(a!=b);//输出为布尔值
System.out.println(b%a);//模运算,取余数
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 自减 医院运算符
int a = 1;
int b = a++;
int c = ++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
//a++ ++a都隐藏了一句 a = a+1
//但是a++是先赋值再自增 ++a反之
//输出a=3的原因是进行了两次隐藏 a=a+1
//输出b=1的原因是b=a比a=a+1先执行
//输出c=3的原因是a=a+1在c=a之前执行
//数学工具 math
double pow = Math.pow(2, 3);
System.out.println(pow);//次方
}
}
package operator;
//逻辑运算符
//与或非 &&两个都对 ||两个有一个对 !()对改成错,错改成对
public class Demo07 {
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println(a&&b);
System.out.println(a||b);
System.out.println(!(a&&b));
//短路运算
int c = 5;
boolean d = (c++ < 5) && (c < 5) ;
System.out.println(d);
System.out.println(c);
int e = 1;
e++;
System.out.println(e);
}
}
package operator;
public class Demo08 {
public static void main(String[] args) {
/*
A = 1100 1100
B = 1101 1011
-----------------
A&B 1100 1000
A|B 1101 1111
A^B 0001 0111
~B 0010 0100
*/
int a = 2;
int b = a<<3;
System.out.println(b);
/*
0000 0010 2
0000 0100 4
0000 1000 8
0001 0000 16
<< 表示*2 | >> 表示/2
*/
//位运算 用于计算非常有效率
}
}
package operator;
//字符串
public class Demo09 {
public static void main(String[] args) {
int a = 20;
int b = 30;
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");//两种不同!!!
}
}
package operator;
public class Demo10 {
public static void main(String[] args) {
//x ? y :z
//如果x==true 则结果为y 否则为z
int score = 80;
String type = score <60 ? "不及格":"及格";
System.out.println(type);
//该代码为if的简写,更加精简
}
}
关于优先级
一般无论优先等级,先运算的都加上括号,便于区分。
包机制
- 包
具体意思是相当于java里新建文件夹,包名字一般为网站域名倒置,如百度为com.baidu.www;
- 导入包
若想要引用其他包内的代码,输入import 包名(大量引用符号*)
javadoc
- javadoc即是三种注释里的文档注释
- 官方javadoc---jdk帮助文档
/*
参数信息
@author 作者名
@version 版本号
@since 指明需要最早使用的jdk版本
@param 参数名
@return 返回值情况
@throws 异常抛出情况
(变成里为/**+回车)
*/
package com.Hellscythe.Base;
/**
* @author Hellscythe
* @version 1.0
* @since 1.8
*/
public class Doc1 {
String name;
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
- 导航栏前加上cmd (空格)进入命令行
- javadoc命令生成文档
- -encoding UTF-8 -charset UTF-8 参数 让中文正常显示
- doc1.java 目标文件名
关于使用IDEA创建javadoc
- tools---Generate Javadoc…---第一行选取目标文件位置 第二行选取输出位置 第四行附加参数(-encoding UTF-8 -charset UTF-8)

浙公网安备 33010602011771号