脚本宝典收集整理的这篇文章主要介绍了java学习笔记,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
八大基本数据类型
public class Demo02 {
public static void main(String[] args){
//整数
int = 10; //最常用,占4个字节,取值范围:-2147483648 ~ 2147483647
byte = 20; //占1个字节,取值范围:-128 ~ 127
short = 30; //占2个字节,取值范围:-32768 ~ 32767
long = 30L; //占8个字节,取值范围:-9223372036854775808 ~ 9223372036854775807
//小数:浮点数
float num5 = 50.1F;//float类型要在数字后面加个F,占4个字节
double num6 = 3.141592653589793238462643; //占8个字节
//字符
char name = '国'; //占2个字节
//字符串,String不是关键字,类
//String name = "秦疆";
//布尔值:是非
boolean flag = true; //占1位
//boolean flag = false;
}
}
位(bIT):是计算机内部数据存储的最小单位,11001100 是一个八位二进制数。
字节(byte):是计算机中数据处理的基本单位,习惯上用大写 B 来表示
字符:是指计算机中使用的字母、数字、字和符号
1bit表示1位
1byte表示一个字节 1B = 8b。
1024B = 1KB
1024KB = 1M
1024KM = 1G
import java.util.Calendar;
public class Demo03 {
public static void main(String[] args) {
// 整数拓展: 进制 二进制 八进制:0 十进制 十六进制:0x
int i = 10;
int i2 = 010; // 八进制0
int i3 = 0X10; // 十六进制0x
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("=================================================");
//====================================================
// 浮点数拓展 银行业务怎么表示 钱
//=======================================================
//float
//double
float f = 0.1f; // 0.1
double d = 1.0/10; // 0.1
System.out.println(f==d); //false
float d1 = 1464513154316156161f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
//=======================================
// 字符拓展
//=======================================
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); // 强制转换
System.out.println(c2);
System.out.println((int)c2); // 强制转换
// 所有的字符本质还是数字
// 编码 Unicode 表:(97 = a 65 = A) 2字节 0 ~ 65536 2^6 = 65536
// U0000 UFFFF
char c3 = 'u0061';
System.out.println(c3); // a
// 转义字符
// t 制表符
// n 换行
System.out.println("HellotWorld");
System.out.println("===============================");
String sa = new String( original:"Hello World");
String sb = new String( original:"Hello Wrold");
System.out.println(sa == sb);
String sc = "Hello World";
String sd = "Hello World";
System.out.println(sc == sd);
//对象 从内存分析
//布尔值扩展
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
//Less is More! 代码要精简易读
}
}
低 ——————————————————> 高
byte,short,char --> int --> long --> float --> double
public class Demo03 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i; // 内存溢出
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
System.out.println(i);
System.out.println(b);
/*
* 注意点:
* 1、不能对布尔值进行转换
* 2、不能把对象类型转换为不相干的类型
* 3、在把容量转换到低容量的时候,强制
* 4、转换的时候可能存在内存溢出,或者精度问题!
* */
System.out.println("============================");
System.out.println((int)23.7);
System.out.println((int)-45.89F);
System.out.println("==============================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char) d);
// 操作比较大的数的时候,注意溢出问题
// JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years; // -1474836480 , 计算的时候溢出了
long total2 = money*years; //默认是int,转换之前已经存在问题?
long total3 = money*years; //先把一个数转换为long
System.out.println(total3);
}
}
type VARName [=value] [{,varName[=value]}];
//数据类型 变量名 = 值;可以使用逗号隔开来声明多个同类型变量。
public class Demo07 {
public static void main(String[] args) {
//int a,b,c
int a=1;
int b=2;
int c=3;
string name = "qinjiang";
char x = 'x';
double pi = 3.14;
}
}
public class Variable{
static int allClicks = 0; // 类变量
String str = "hello world"; // 实例变量
public void method (){
int i = 0; // 局部变量
}
}
public class Demo08 {
// 类变量 static
static double salary = 2500;
// 属性:变量
// 实例变量:从属于对象:如果不自行初始化,这个类型的默认值 0 0.0
// 布尔值:默认是false
// 除了基本类型,其余的默认值都是null;
String name;
int age;
// main方法
public static void main(String[] args) {
// 局部变量
int i = 10;
System.out.println(i);
// 变量类型 变量名字 = new Demo08();
Demo08 demo08 = new Demo08();
System.out.println(demo08.age);
System.out.println(demo08.name);
//类变量 static
System.out.println(salary);
}
// 其他方法
public void add(){
}
}
final 常量名 = 值;
final double PI=3.14;
常量名一般使用大写字符。
public class Demo08 {
// 修饰符,不存在先后顺序
static final doubel PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
Java语言支持如下运算:运算优先级
算术运算符:+, -, *, /, %, ++,--
赋值运算符 =
关系运算符:>, <, >=, <=, ==, !=instanceof
条件运算符 ? :
扩展赋值运算符: +=, -=, *=, /=
public class Demo08{
public static void main(String[] args){
// 二元运算符
// Ctrl + D :复制当前行到下一行
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
public class Demo08{
public static void main(String[] args){
long a = 123124124124L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d); // Long
System.out.println(b+c+d); // int
System.out.println(c+d); // int
}
}
public class Demo08{
public static void main(String[] args){
// 关系运算符返回的结果:正确,错误 布尔值
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a);
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
public class Demo08{
public static void main(String[] args){
//++ -- 自增,自减 -元运算符
int a = 3;
int b = a++; //执行完 b = a 后在a自增
int c = ++b; //b自增后再执行 c = b
System.out.println(a);
System.out.println(b);
System.out.println(c);
// 自减同理
// 幂运算 2^3
double pow = Math.pow(3,2);
System.out.println(pow);
}
}
public class Demo08{
public static void main(String[] args){
// 与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b: "+(b&&a)); // 逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b: "+(b||a)); // 逻辑或运算:两个变量有一个为真,则结果为true
System.out.println("!(a && b): "+!(b&&a)); // 如果是真,则变为假,如果是假则变为真
// 短路运算
int c = 5;
boolean d = (c<4)&&(c++<4); // 短路测试,是否执行了c++,执行了为6,未执行则为5
System.out.println(d);
System.out.println(c);
}
}
public class Demo08{
public static void main(String[] args){
/*
位运算
A = 0011 1100
B = 0000 1101
--------------------------------
A&B = 0000 1100 # 对应位两个都是1才是1,否则为0
A|B = 0011 1101 # 有一个为1则为1,否则为0
A^B = 0011 0001 # 两个相同则为0,否则为1
~B = 1111 0010 # 取反
2*8 = 16 2*2*2*2
效率极高 !!!
<< *2 左移
>> /2 右移
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
……
*/
System.out.println(2<<3);
}
}
public class Demo08{
public static void main(String[] args){
int a = 10;
int b = 20;
a+=b; // a = a+b
a-=b; // a = a-b
System.out.println(a);
// 字符串连接 +
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
public class Demo08{
public static void main(String[] args){
// 三元运算符: x ? y : z
// 果如x==true,则结果为y,否则结果为z
int score = 50;
String type = score < 60 ? "不及格":"及格"; //必须掌握
System.out.println(type);
}
}
为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。
相当于文件目录,用于存放文件。
包语句的语法格式为:
package pkg1[.pkg2[.pkg3……]];
一般利用公司域名倒置作为包名:如 公司域名为:www.baidu.COM-->包名为:com.baidu.www
为了能够使用某一个包的成员,我们需要再 Java 程序中明确导入该包,使用 "import" 语句可以完成此功能
import package1[.package2……].(classname|*); // *表示所有文件
学习 “阿里巴巴开发手册”
javadoc 命令是用来生成自己API文档的
参数信息
@author 作者名
@version 版本号
@since 指明需要最早使用的jdk版本
@param 参数名
@return 返回值
@throws 异常抛出情况
#生成JavaDoc
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
/**
* @author kuangshen
* @version 1.0
* @since 1.8
*/
public class Doc {
/**
* author kuangshen
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
百度 “idea生成doc文档”
百度 “jdk帮助文档”
2022年4月12日 P33
以上是脚本宝典为你收集整理的java学习笔记全部内容,希望文章能够帮你解决java学习笔记所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。