Java入门学习笔记

文 章 目 录

类型转换

int a=(int)'a';
char b=(char)97;
a.getClass() --查看类型

常量

static final float pi=3.14159f;
final int one=1;


逻辑运算


&

|

~
移位
<<
>>
无符号右移
>>>


三元运算符

int a=12>20?1:0;

# python
a = 1 if 12>20 else 0

复合语句

public static void min(String[] args){
	int a=12;
	for(int i=0;i<a:i++){
		System.out.println("hello world");
		{
			// 定义的变量只在花括号内有意义
			int b=13;
			int c=0;
			a--;
			{
				char a='a';
				String s="Hello world";
			}
		}
	}
	System.out.println("Over");
}

流程控制

判断

if(a>12){
	b=12;
}
else if(a<=12&&a>0){
	b=11;
}
else{
	b=0
}

for循环

for(int i=0;i<12;i++){
	a += i;
}
else{ //跟Python用法差不多
 	// 循环正常结束则执行 else
	System.out.println("Over");
}

for each

for(int a[]:arr2){
	for(int b:a){
		System.out.println(b);
	}
}

switch分支

int a=12;
switch (a){
	case 1:
		System.out.println("one");
		break;
	case 2:
		System.out.println("two");
		break;
	case 3:
		System.out.println("three");
		break;
	default:
		System.out.println("not 1 2 3");
}

while

int c = 0;
int a = 12;
while(a>0){
	int b=2;
	c += 2;
	c = c<<1;
	a--;
}

do while

int a = 4;
int c = 2;
do{
	c += 12;
	c = c>>>1;
	a--;
}while(a>0);

Break 循环体

标签0:循环体{
	标签1:循环体{
	...
	....
	break 标签0;
	...
	}
}
loop1: while(a<b){
		a++;
		loop2: for(int i=12;i>1;i--){
					c += i;
					if(c>12){
						break loop1;
					}
				}
	}

String

定义
String s = "hello wworld";
String s = new String("hello world");
char a[]={'h', 'e','l','l','o'}; String s = new String(a);

索引
str.charAt(int index);

定位
str.indexOf(substring);
str.lastindexOf(substring);

长度
str.length();

取子串
str.substring(begin[,end]);

去两头空格
str.trim()

替换
str.replace(old, new)

判断开头、结尾
str.endsWith(substring)
str.starsWith(substring)

判断相等
str1.equals(str2) 区分大小写
str2.equalsIgnoreCase(str2) 不区分

比较
str.compare(str2) 相等才返回0

小写
str.toLowerCase()

大写
str.toUpperCase()

切片
str.split(str1) str1可以为正则表达式

字符串格式化
str.format()


正则

可以用,跟爬虫学的差不离


数组

定义
int a[] = {1,2,3};
int a[] = new int[]{1,2,3};

int a[][];
a = new int[2][];
a[1] = new int[2];
a[0] = new int[3];

填充
fill(arr, data);
fill(arr, from, to, data);

复制
arr2 = Arrays.copyOf(arr, length);
arr2 = Arrays.copyOfRange(arr, from, to);

查询
Arrays.binarySearch(arr, from, to, data);

排序
Arrays.sort(arr)

import java.util.Arrays

arr1 = Arrays.sort(arr);

类和对象 权限修饰符 | 类的构造方法 | 静态变量、方法 | 类的主要方法 | 类的创建、使用 | 对象的比较

public class Book{
	private String name;
	
	public String getName(){
		int id=0;
		setName("Java");
		return id+this.name;
	}
	
	private void setName(String name){
		this.name = name;
	}

	public Book getBook(){
		return this;
	}
}

权限修饰符

访问位置 privateprotectedpublic
本类可见可见可见
同包其他类或子类不可见可见可见
其他包的类或子类不可见不可见可见

!类的权限设定会约束类成员的权限

package test
class AnyClass{
	public void doString(){
		//方法主体
		...
	}
}

上述等同于下面代码[由于类的修饰符为默认修饰符]

package test
class AnyClass{
	void doString(){
		//方法主体
		....
	}
}

this关键字用于代表本类对象的引用。


类的构造方法

- 用于创建对象
- 与类同名
public class TestOne{
	public TestOne(){	//无参构造方法
		this("通过 this 调用有参的构造方法");
		// 只可以在无参构造第一句用this 调用 有参构造
		System.out.println("调用了无参构造方法。");
	}
	public TestOne(String name){
		this.name = name;
		System.out.pringln("调用了有参构造方法");
	}

}

【示例】

public class hellojava {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		System.out.print("hello world!");
		System.out.print("hello java\n");
		NoName n = new NoName(119, "wula", 23);
		System.out.println("id: " + n.id);
		n.Getprofile();
	}
}

class NoName{
	protected int id=0;
	protected String name="";
	public int price=0;
	
	public NoName() {
		this.id = 22;
		this.name = "hello";
		this.price = 34;
	}
	
	public NoName(int id, String name,int price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
	
	public void Getprofile() {
		System.out.println(this.id+this.name+this.price);
	}
}

静态变量、常量、方法

  • 静态方法中不可以使用this关键字
  • 静态方法中不可以直接调用非静态方法
  • 不能将方法体内的局部变量声明为static
public class StaticTest{
	final static double PI = 3.14159;  //定义静态常量
	static int id;                     //定义静态变量
	public static void method1(){      //定义静态方法
		//None
	}
	public void method2(){             //定义非静态方法
		System.out.println(StaticTest.PI);
		System.out.println(StaticTest.id);
		StaticTest.method1();
	}
	public static StaticTest method3(){ //定义静态方法
		//....
		method2();            			//调用非静态方法 [x]
		return this; 					//使用this关键字 [x]
	}
}

静态数据与方法的作用通常是为了提供共享数据或方法

  • 可以用static定义一个静态区域(希望先执行类的初始化动作时)
  • 示例
    • 执行此段代码时,static块中的代码先执行,且只会执行一次
public class example{
	static{
		//do somethind
	}
}

类的主要方法

public class Test{
	public static void main(String[] args){
		for(int i=0;i<args.length();i++){
			System.out.println(args[i]);
		}
	}
}

对象的创建与使用

public class Test{
	int i = 12;
	public void call(){
		System.out.println("hello world");
		for(i=0;i<10<i++){
			System.out.print(i+"  ");
			if(i == 2){
				System.out.println("ln");
			}
		}
	}

	public Test(){
	}

	public static void main(String[] args){
		Test t1 = new Test();
		Test t2 = new Test();
		t2.i = 30;
		System.out.println("对象t1的变量i值" + t1.i);
		t1.call();
		System.out.println("对象t2变量i值:" + t2.i);
		t2.call();	
	}
}

对象的比较

  • ==比较两对象的引用地址是否相同
  • str1.equals(str2)比较两对象内容
String s1 = new String("abs");
String s2 = new String("abs");
String s3 = s1;
System.out.println("s2 == s3 ?  :" + s2 == s3);
System.out.println("s2.equals(s3) ?  :" + s2.equals(s3));

输出

>> s2 == s3 ?  :false
>> s2.equals(s3) ?  :true

对象的销毁

  • 对象超过其作用范围
{//超过此花括号...
	Test t = new Test();
}
  • 对象被赋值 null
{
	Test t = new Test();
	t = null;
}

垃圾回收

  Java提供了一个 finalize() 方法(Object类、声明为 protected)。
  用户可以在自己的类里定义这个方法,于是垃圾回收时会首先调用该方法,在下一次垃圾回收动作发生时,才能真正回收被对象占用的内存。

强制启动垃圾回收器

System.gc()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

薛定谔的壳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值