脚本宝典收集整理的这篇文章主要介绍了Java SE,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
著作权归Guide哥所有。 链接: https://javaguide.cn/java/basis/java基础知识总结/#什么是字节码-采用字节码的好处是什么
//long类型要在数字后加L
long number = 127L;
整数扩展
1、二进制:0B开头
2、八进制:0开头
3、十进制:阿拉伯数字
4、十六进制:0x开头
//float类型要在数字后面加F
float number = 50.1F;
浮点数扩展
1、float:有限、离散,有舍入误差,实际结果为大约或者接近但不等于
bollean isTrue = true;
1B(byte,字节):8bit(位)
1bit表示1位
1Byte表示一个字节1B=8b
1024B=1KB
1024KB=1M
1024M=1G
低------------------------------------>高
1、根据存储长度从低到高排序
2、long比float和double都长,但是排在其后,因为小数优于整数
byte,short,char->int->long->float->double
注意: 1、不能对boolean值进行转换 2、不能把对象类型转换为不相干的类型 3、把高等级转换为低等级时,进行强制转换 4、转换的时候可能存在内存溢出或者精度问题 5、操作比较大的数的时候,注意溢出问题 int money=10_0000_0000
int a = 10;
//++在后,先赋值再运算
int b = a++;
//++再前,先运算再赋值
int c = ++a;
A = 0011 1100
B = 0000 1101
//与运算符,A和B进行比较,如果对应位两个都为1结果为1,反之为0
A&B:0000 1100
//或运算符,A和B进行比较,如果对应位两个都为0结果为0,反之为1
A|B:0011 1101
//异或运算符,A和B进行比较,如果对应位相同为结果为0,反之为1
A^B:0011 0001
//取反运算符
~B:1111 0010
//左移、右移
0000 0000:0
0000 0001:1
0000 0010:2
0000 0011:3
0000 0100:4
0000 0101:5
0000 0110:6
0000 0111:7
0000 1000:8
<<:当前数值*2或者向前进一位
>>: 当前数值/2或者向后退一位
//生成doc语法,命令行模式
javadoc -encoding UTF-8 -charset UTF-8 Hello.java
//基本语法
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户是否输入字符串
if(scanner.hasNext()){
String userInput = scanner.next();
System.out.println("输入的内容:" + userInput);
}
scanner.close();
}
//死循环
for(;;){
}
int[] numbers = {1,2,3,4,5,6};
for(int x:numbers){
System.out.println(x);
}
int count = 0;
outer:for(int i=101;i<150;i++){
for(int j=2;j<i/2;j++){
if(i%j==0){
continue outer;
}
}
}
//声明
int[] numbers;
int numbers[];
//创建
int[] numbers = new int[10];
int[] numbers = new int[]{1,2,3,4,5};
//获取数组长度
int count = numbers.length;
int[] numbers = {1,2,3,4,5};
for(int i : numbers){
System.out.println(i);
}
//数组a可以看成一个两行五列的数组
int a[][] = new int[2][5];
//数组b可以看成一个两行三列的数组
int b[][] = {{1,2},{3,4},{5,6}};
/*
Ctrl+H 可以查看类的继承关系
Alt+Insert 快速创建构造、属性方法
Ctrl+Alt+点击鼠标 跳转至实现
*/
public class Person{
public void say(){
System.out.println("Hello World")
}
}
public class Chinese extends Person{
}
public static void main(String[] args){
new Chinese().save();
}
public class Person(){
protected String name="parent";
}
public class Chinese extends Person{
private String name="lvfeng";
public void test(string name){
//方法的参数
System.out.println(name)
//当前类的字段
System.out.println(this.name)
//父类的字段
System.out.println(super.name)
}
}
public class Person(){
public void say(){
System.out.println("Hello World")
}
}
public class Chinese extends Person{
@override
public void say(){
System.out.println("Hello World")
}
}
if(chinese instanceof person){
return "父子关系";
}
{
/*
匿名代码块:在静态代码块之后构造函数之前执行
*/
}
static {
/*
静态代码块:构造函数之前执行且只执行一次
*/
}
//静态导入包
import static java.lang.Math.random;
public static void main(String[] args){
System.out.println(random());
}
public abstract class Action{
public abstract void go();
}
public class Child extends Action{
@override
public void go(){
}
}
public abstract class Child2 extends Action{
public abstract void go();
public void run(){
System.out.println("Hello World");
}
}
public static void main(String[] agrs){
Action action = new Child();
action.go();
}
public interface IAction{
//静态常量:public static final
int AGE=99;
void go();
}
public interface IAction1{
void run();
}
public class Action implements IAction,IAction1 {
@override
public void go(){
System.out.println("Hello World");
}
@override
public void run(){
System.out.println("Hello World");
}
}
public void scal(){
int a = 10
int b = 0;
if(b==0){
throw new ArithmeticException();
}
}
public void scal() throws ArithmeticException{
int a = 10;
int b = 0;
int c = a / b;
}
public class MyException extends Exception{
private int _numbers;
public MyException(int numbers){
_numbers = numbers;
}
@override
public String toString(){
return "MyException{"+_numbers+"}";
}
}
public class Outer{
private String name;
private int age;
class Inner{
private String name;
private String address;
private String phone;
public void show(){
//打印内部类的name,如果内部类没有这个成员直接访问的外部类
System.out.println(name);
//当出现重名时,使用如下方式调用外部类的属性
System.out.println(Outer.this.name);
}
}
}
public static void main(String[] args){
Outer outer = new Outer();
Inner inner = outer.new Inner();
Inner inner = new Outer().new Inner();
inner.show();
}
public class Outer{
private String name;
private int age;
static class Inner{
private String address;
private String phone;
private static int count;
public void show(){
Outer outer = new Outer();
System.out.println(outer.name);
//访问静态内部类非静态成员
System.out.println(phone);
//访问静态内部类静态成员
System.out.println(Inner.count);
}
}
}
public static void main(String[] args){
Outer.Inner inner = new Outer.Inner();
inner.show();
}
public class Outer{
private String name;
private int age;
public void show(){
String address;
//定义局部内部类
class Inner{
private String phone;
private String email;
public void show2(){
//访问外部类属性
System.out.println(name);
//访问内部类属性
System.out.println(phone);
//访问外部方法的属性,jdk1.7要求变量必须是常量,jdk1.8自动添加final
System.out.println(address);
}
}
Inner inner = new Inner();
inner.show();
}
}
public static void main(String[] args){
new Outer().show();
}
public interface IOuter{
void service();
}
public static void main(String[] args){
//局部内部类
class Inner implements IOuter{
@override
public void service(){
System.out.println("Hello World");
}
}
IOuter outer = new Inner();
outer.service();
//匿名内部类
IOuter outer = new IOuter(){
@override
public void service(){
System.out.println("Hello World");
}
}
outer.service();
}
public class Student{
public String name;
public int age;
}
public static void main(String[] args){
Student stu1 = new Student();
Student stu2 = new Student();
//判断stu1和stu2是否同一类型
Class c1 = stu1.getClass();
Class c2 = stu2.getClass();
if(c1 == c2){
return true;
}else{
return false;
}
}
public class Student{
public String name;
public int age;
}
public static void main(String[] args){
Student stu1 = new Student();
Student stu2 = new Student();
int stu1 = stu1.hashCode();
int stu2 = stu2.hashCode();
if(stu1 == stu2){
return true;
}else{
return false;
}
}
public class Student{
private String name;
//重写Object的toString()
@override
public String toString(){
return "Hello World";
}
}
public static void main(String[] args){
//返回对象的全名称+@+hashCode(16进制)
String stu1 = stu1.toString();
String stu2 = stu2.toString();
}
public class Student{
private String name;
@override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
//判断对象的类型是否相同
<!-- if(this.getClass() == obj.getClass()){
return true;
} -->
if(obj instanceof Student){
Student stu = (Student)obj;
if(this.name == stu.name){
return true;
}
}
return false;
}
}
public static void main(String[] args){
Student stu1 = new Student();
Student stu2 = new Student();
System.out.println(stu1.equals(stu2));
}
public class Hello{
@override
protected void finalize() throws Throwable{
System.out.println("对象堆GC回收了");
}
}
public static void main(String[] args){
Hello hello = new Hello();
//垃圾回收,执行此语句没有执行对象的finalize
System.gc();
new Hello();
//垃圾回收,执行此语句执行了对象的finalize
System.gc();
}
/*
JDK1.5之前
*/
//装箱
int num = 18;
Integer i1 = new Integer(num);
Integer i2 = Integer.valueOf(num);
//拆箱
num = i1.intValue();
/*
JDK1.5之后,提供自动装箱和拆箱
*/
//装箱
Integer i1 = num;
//拆箱
num = i1;
Integer i1 = new Integer(100);
Integer i2 = new Integer(100);
//当前结果返回false,因为Integer是引用类型,i1和i2的内存引用不同
System.out.println(i1 == i2);
Integer i1 = 100; // 等同于 Integer i1 = Integer.valueOf(100);
Integer i2 = 100;
//当前结果返回true,因为上面的代码执行了自动装箱(valueOf)的操作,valueOf会从缓存中读取-127到128的数值,因为之前已经实例化,所以i1和i2的内存指向相同
System.out.println(i1 == i2);
Integer i1 = 200;
Integer i2 = 200;
//当前结果返回false,即使走的也是valueOf方法,但200已经超过了整数缓冲区,所以会执行new Integer()重新创建一个新的对象
System.out.println(i1 == i2);
/*
产生一个对象,字符串池中存储
创建流程:先在栈开辟空间,然后判断字符串池中是否有Hello,如果有返回内存地址,反之则创建后返回地址
*/
String str = "Hello";
/*
产生两个对象,堆、池各存储一个
创建流程:先在栈中开辟空间,然后判断字符串池中是否有Hello,如果有不进行任何操作,反之则开辟空间,最后在堆中开辟空间创建空对象,但是堆中的内存地址是字符串中的地址,然后将堆中的地址返回回去
*/
String str = new String("Hello");
StringBuffer sb = new StringBuffer();
sb.append("追加字符串");
System.out.println(sb.toString());
StringBuilder sb = new StringBuilder();
sb.append("追加字符串");
System.out.println(sb.toString());
//验证StringBuilder效率高于StringBuffer
long start = System.currentTimeMillis();
String string="";
for(int i=0;i<99999;i++){
string+=i;
}
long end = System.CurrentTimeMillis();
System.out.println("用时:"+(end-start));
long start = System.CurrentTimeMillis();
StringBuffer string = new StringBuffer();
for(int i=0;i<99999;i++){
string.append(i);
}
long end = System.CurrentTimeMillis();
System.out.println("用时:"+(end-start));
long start = System.CurrentTimeMillis();
StringBuilder string = new StringBuilder();
for(int i=0;i<99999;i++){
string.append(i);
}
long end = System.CurrentTimeMillis();
System.out.println("用时:"+(end-start));
//double和float在内存中存放的是一个近似值,所以计算出来的结果会有偏差
double d1 = 1.0;
double d2 = 0.9;
//d3并不等于0.1,而是0.999999998
double d3 = d1 - d2;
BigDecimal n1 = new BigDecimal("1.0");
BigDecimal n2 = new BigDecimal("0.9");
//执行减法
BigDecimal n3 = n1.suBTract(n2);
/*
加法:add
乘法:multiply
除法:devide
*/
//创建对象
Calendar calendar = Calendar.getInstance();
Collection collection = new ArrayList();
collection.add("Hello");
collection.remove("Hello");
collection.clear();
collection.size();
for(Object obj : collection){
System.out.println(obj);
}
//在迭代过程中不允许使用Collection移除方法
Iterator item = collection.iterator();
while(item.hasNext()){
item.remove();
System.out.println(item.next());
}
collection.contains("Hello");
List list = new ArrayList();
list.add("Hello");
ListIterator iterator = list.listIterator();
//从前往后
while(iterator.hasNext()){
int index = iterator.nextIndex();
Object item = iterator.next();
}
//从后往前
while(iterator.hasNext()){
int index = iterator.previousIndex();
Object item = iterator.previous();
}
ArrayList arrayList = new ArrayList();
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
ListIterator iterator = arrayList.listIterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
while(iterator.hasPrevious()){
System.out.println(iterator.previous());
}
以上是脚本宝典为你收集整理的Java SE全部内容,希望文章能够帮你解决Java SE所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。