java学习笔记

java学习笔记

1.java是一门面向对象的语言

  • 就好像只有一个 main ,一个文件里面也只有一个类它的属性是 public

例如:

public class Test{
	public static void main (String args[]){
		System.out.println("Hello Word");
	}
}
/*这样就是一个简单的java程序了*/

2.编译和运行

  • 编译:javac -encoding utf-8 Test.java

  • 运行:java Test
    /这样就可以输出Hello Word了/

3.一个fileName.java里面包含一个主类(public)和多个类

//public 修饰的那个类就是主类

class T1{
	private String name;
	public T1(String name){
		this.name = name;
	}
	public void Print(){
		System.out.println(this.name);
	}
}

public class Test{
	public static void main (String args[]){
		T1 t1 = new T1("副类");
		t1.Print();
	}
}

/*
	上面创建了一个副类T1,不能用public修饰
	简单的实现了创建一个对象t1,并用对象t1,去调用类T1中的方法
	也可以把它分为两个主类在不同文件中调用
	-import java.io.* - 需要在文件中加载这个模块
*/

4.java中的几种基本的数据类型

  • 常见的几种数字数据类型:int / double / float / long / short /
  • 常见几种字符数据类型: char
  • 一些特殊的: String / boolean / byte

5.java的for循环结构

//循环打印1到10
public class Test{
	public static void main(String[] args){
		for(int x = 0;x <= 10;x = x + 1){
			System.out.println("输出" + x);
			System.out.println("\n");
		}
	}
}

6.java的while循环结构

//循环打印1到10
	public class Test{
		public static void main(String[] args){
			int x = 0;
			while(x <= 10){
				x++;
				System.out.println("输出" + x);
				System.out.println("\n");
			}
		}
	}
//do ... while 循环结构
public class Test{
	public static void main(String[] args){
		int x = 0;
		
		do{
			x++;
			System.out.println("输出" + x);
			System.out.println("\n");
		}
		while(x <= 10);//这个会打印到11,因为是先执行循环体里面的语句,然后在进行判断
	}
}

7.java中的条件语句if…else

//条件语句if
public class Test{
	public static void main(String[] args){
		int x = 0;
		
		if(x <= 10){
			System.out.println("x 小于或等于10");
		}else{
			System.out.println("x 大于10");
		}
	}
}

8.java中的数组

//数组
public class Test{
	public static void main(String[] args){
		//创建一个大小为10的数组
		int[] arr = new int[10]; - 写法一
		//int[] arr = {1,2,3,4,5,6,7,8,9,10}; //写法二
		
		for(int x = 0;x < 10;x++){
			arr[x] = x;
		}
		
		System.out.println("打印数组的第一个元素: arr[1] = " + arr[1]);
	}
}

9.java中的继承

class Father {
	void Money(){
		System.out.println("我是父亲,我有一百万");
	}
}

class Son extends Father {
	Son(){};
}

class Friend {
	Friend(){};
}

//继承
public class Test {
	public static void main(String[] args){
		Son son = new Son();
		son.Money();
		
		Friend friend = new Friend();
		//Friend.Money(); - 调用这个就会报错
		//因为Money是Father类中的方法,Friend和Father之间没有继承关系,不能调用
	}
}

10.java中的switch语句

//switch语句
public class Test{
	public static void main(String[] args){
		int x = 3;
		
		switch(x){
			case 0:
				System.out.println("输入的数为:1");
				break;
			case 1:
				System.out.println("输入的数为:2");
				break;
			case 3:
				System.out.println("输入的数为:3");
				break;
			default:
				System.out.println("输入的数大于三或小于零");
		}
	}
}

11.java中的异常捕获

try
{
   // 程序代码
}catch(ExceptionName e1) //ExceptionName 异常描述的名字,这个可以根据自己的情况定义
{
   //Catch 块
}
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值