java基础汇总防忘

预备知识

Java虚拟机—JVM(Java Virtual Machine)
是运行所有Java程序假象的计算机,是Java程序的运行环境。

跨平台: 任何软件都是运行在操作系统之上,Java编写的软件可以运行在任何操作系统上,这个特性成为Java语言的跨平台特性,该特性由JVM实现。
编写的Java程序运行在JVM上,JVM又运行在操作系统上。
Java虚拟机本身不具备跨平台性,每个操作系统都有不同版本的虚拟机,从而实现跨平台性。

JRE(Java Runtime Environment): 是Java程序的运行时环境,包括JVM和运行时所需要的核心类库。

JDK(Java Development Kit): 是Java程序开发工具包,包含JRE和开发人员使用的工具

Java总共有三个版本:
(1)JavaSE(Java Platform,Standard Edition):标准版
Java基础所学的如基础语法、网络协议、多线程…
SE用来写GUI、客户端(桌面、服务器、嵌入式环境和实时环境)
(2)JavaEE(Java Platform,Enterprise Edition):企业版
在SE的基础上加了servletContext、容器、request、session…
EE写Web端
(3)Java ME(Java Platform,Micro Edition):微型版
ME写移动端(移动设备和嵌入式设备(比如手机、PDA、电视机顶盒和打印机))

java运行步骤
javac.exe:编译器
java.exe: 解释器

.java文件->编译->.class文件,编译成.class字节码,.class需要jvm解释,然后解释执行。Java程序需要编译但是没有直接编译成机器语言,而是编译成字节码(.class)再用解释方式执行。java程序编译以后的class属于中间代码,并不是可执行程序exe,不是二进制文件,所以在执行的时候需要一个中介来解释中间代码,这既是java解释器,也就是所谓的java虚拟机(JVM)。

访问权限

访问权限由高到低为public>protected>友好>private
protected与友好的区别是protected在包外也可以访问到,而友好的在包外无法访问。

接口interface

接口用interface关键字声明。
接口体中包含常量的声明和抽象方法(只有这两种),访问权限一定是public,且常量是static。

interface I{
	public static final int var;
	public abstract void func();
}

如果一个非抽象类实现了某个接口,那么这个类必须重写这个接口中的所有方法。

String类

对象的字符序列不能被修改和删除。

public int length():获取字符串中含有的字符个数,拿到字符串当读
public String concat(String str):将当前字符串和参数拼接
public char charAt(int index):获取指定索引位置的单个字符
public int indexOf(String str):查找参数字符串当中首次出现的索引位置,没有返回-1

public String substring(int begin, int end):截取从begin到end-1的字符串

public char[] toCharArray():将当前字符串拆分成字符数组作为返回返回值
例:byte[] b = str.toCharArray();

public byte[] getBytes():获得当前字符串底层的字节数组

public String replace(CharSequence oldString, CharSequence newString):
将所有出现的老字符串替换成新的字符串,返回替换后的结果新字符串 

public String[] split(String regex):按照参数的规则,将字符串切分成若干部分

String str = String.valueOf(数值型变量) 数值转为string

StringBuffer类

StringBuffer类的对象实体的内存空间可以自动改变大小,便于存放一个可变的字符序列。

三个构造函数
StringBuffer()
StringBuffer(int size)
StringBuffer(String s)

.length()获取字符串长度
.capacity()获取实际容量

.append(String s)追加s
.charAt(int n)获取第n位字符
.setCharAt(int n, char ch)设置第n位字符

.insert(int index, String str)插入
.reverse()倒置
.delete(int startIndex, int endIndex)删除
.replace(int startIndex, int endIndex, Strign str)代替

Date类

Date nowTime = new Date();
System.out.println(nowTime);

Calendar类

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);

Math类

long abs(double a);
double max(double a, double b);
double min(double a, double b);
double random(double a, double b);
double pow(double a, double b);
double sqrt(double a);
double log(double a);
double sin(double a);
double asin(double a);
double ceil(double a);
double floor(double a);
long round(double a);

BigInteger类

BigInteger add(BigInteger val);
BigInteger subtract(BigInteger val);
BigInteger multiply(BigInteger val);
BigInteger divid(BigInteger val);
BigInteger remainder(BigInteger val);
int compareTo(BigInteger val);
BigInteger abs(BigInteger val);
BigInteger pow(BigInteger val);
String toString()
String toString(int p); 生成p进制

Random类

Random r = new Random();
r.nextInt();
r.nextDouble();

File类

文件属性

public static void main(String[] args) {
	File f = new File("F:\\", "a.txt");
	System.out.println(f.getName());
	System.out.println(f.canRead());
	System.out.println(f.canWrite());
	System.out.println(f.exists());
	System.out.println(f.length());
	System.out.println(f.getAbsolutePath());
	System.out.println(f.getParent());
	System.out.println(f.isFile());
	System.out.println(f.isDirectory());
	System.out.println(f.isHidden());
	System.out.println(f.lastModified());
}

目录

public static void main(String[] args) {
	File f = new File("F:\\folder");
	String[] res = f.list();	# String[] res = f.listFiles()
	for (String s:res) {
		System.out.println(s);
	}
}	

创建与删除

File f = new File("F:\\folder", "aa.txt");
f.createNewFile();

文件字节输入流

public static void main(String[] args) {
	File f = new File("F:\\folder\\aa.txt");
	try {
		FileInputStream in = new FileInputStream(f);
		System.out.println(in.read());
		byte[] b = new byte[5];
		int n = in.read(b,0,5);
		String str = new String(b, 0, n);
		System.out.println(str);
		in.close();
	}catch (IOException e){
		System.out.println(e);
	}
}

文件字节输出流

public static void main(String[] args) {
	File f = new File("F:\\folder\\aa.txt");
	try {
		FileOutputStream out = new FileOutputStream(f);
		byte[] a = "helloworld".getBytes();
		out.write(a);
	}catch (IOException e){
		System.out.println(e);
	}
}	

文件字符输入输出流

public static void main(String[] args) {
	File f1 = new File("F:\\folder\\a.txt");
	File f2 = new File("F:\\folder\\b.txt");
	try {
		FileReader in = new FileReader(f1);
		FileWriter out = new FileWriter(f2);
		char[] c = new char [20];
		int n = -1;
		while ((n=in.read(c)) != -1) {
			out.write(c, 0, n);
		}
		out.flush();
		in.close();
		out.close();
	}catch (IOException e){
		System.out.println(e);
	}
}		

多线程

通过Thread子类创建多线程

public class hello {
	public static void main(String[] args) {
		A a = new A();
		B b = new B();
		a.start();
		b.start();
	}	
}
class A extends Thread{
	public void run() {
		for (int i=1; i<=5; i++) {
			System.out.println("A"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
class B extends Thread{
	public void run() {
		for (int i=1; i<=5; i++) {
			System.out.println("B"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

通过Thread类创建多线程

public class hello {
	public static void main(String[] args) {
		A a = new A();
		B b = new B();
		Thread ta = new Thread(a);
		Thread tb = new Thread(b);
		ta.start();
		tb.start();
	}	
}
class A implements Runnable{
	public void run() {
		for (int i=1; i<=5; i++) {
			System.out.println("A"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
class B implements Runnable{
	public void run() {
		for (int i=1; i<=5; i++) {
			System.out.println("B"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

LinkedList

LinkedList<String> mylist = new LinkedList<String>();

public boolean add(E element)
public void add(int index, E element)
public void clear()
public E remove(int index)
public boolean remove(E element)
public E get(int index)
public int indexOf(E element)
public int lastIndexOf(E element)
public E set(int index, E element)
public int size()
public boolean contains(Object element)
public void addFirst(E element)
public addLast(E element)
public E getFirst()
public E getLast()
public E removeFirst()
public E removeLast()
package hello.pack;
import java.util.*;

public class hello {
	public static void main(String[] args) {
		LinkedList<Integer> l = new LinkedList<Integer>();
		for (int i=1; i<=10; i++) {
			l.add(i);
		}
		Iterator<Integer> iter = l.iterator();
		long starttime = System.currentTimeMillis();
		while (iter.hasNext()) {
			Integer t = iter.next();
			System.out.println(t);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		long endtime = System.currentTimeMillis();
		System.out.println("time cost: " + (endtime-starttime));
	}	
}

Collections类

public static sort(List<E> list)
public static binarySearch(List<T> list, T key, CompareTo<T> c)

Stack

public E push(E item)
public E pop();
public boolean empty();
public E peek();

Swing

转载https://blog.csdn.net/xietansheng/article/details/72814492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H4ppyD0g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值