Java基础知识

1.刚开始学java,先写两个简单的函数

    1.在myeclipse新建一个工程,书写第一个类、方法HelloWorld,在控制台打印“Hello World!”

 public class HelloWorld {
	public static void main(String[] args){
		System.out.println("Hello World");
	}
}
    2.for循环,输出0到100所有奇数,并打印
     
public class Test {

public static void main(String[] args){
		for(int i=0;i<100;i++)
		{
			if(i%2!=0){
				System.out.print(i+"  ");
			  }				
			
		}
	}
}
    输出字串用

System.out.println("Hello World");
    输出数值用

System.out.print(i+"  ");
另外,输出0到100也可以用while,do while

while:

//while 实现100以内奇数输出的方法
		/*while(i<100)
		{
			if(i%2!=0)
			{
			System.out.print(i+"  ");	
			}
			i++;
		}*/
do while:

/*	//do while 实现100以内奇数输出的方法
		do{
			if(i%2!=0)
			{
			System.out.print(i+"  ");	
			}
			i++;
		}while(i<100);*/

2.Java的八种基本的数据类型(byte、short、int、long、float、double、char、boolean)
byte:8位,一个字节

short:16位,两个字节

int:32位,四个字节

long:64位,个字节

float:32位,四个字节,单精度浮点型

double:64位,个字节,双精度浮点型

char:16位,两个字节

boolean:只有TRUE和FALSE两个取值

不同的数据类型之间可以相互转换,由高精度向第精度转换会损失精度:

//验证数据类型之间的转换
	public static void datetype(){
		//数据类型由小到大byte,short,char)--int--long--float—double
		byte a=127;
		int b=80;
		//a=b; 报错
		a=(byte) b;  //大转小要强制转换
		b=a;  //由小转大可以
	//char可以当作int类型	
		char c='a';
		int i=c;
		i=200;
		c=(char) i;
		System.out.print(i);
/*①所有的byte,short,char型的值将被提升为int型;

②如果有一个操作数是long型,计算结果是long型;

③如果有一个操作数是float型,计算结果是float型;

④如果有一个操作数是double型,计算结果是double型;
*/
		
	}

3.java中,八大基本类型外还有引用类型

    1.数组,数组可以先声明再初始化,也可以声明同时初始化:

        1.声明数组

               数组元素类型 数组名字[];

               数组元素类型[]数组名字;

          2.为数组分配空间,要指明数组的长度

    数组名字 = new 数组元素类型[数组元素的个数]

           

//一维
	int arr[] = new int[]{1,2,3,4,5,6,7,8};
	int arr2[] = {1,2,3,4,5,6,7,8};
	//二维
	int arr3[][] = new int[][]{{1,2},{3,4},{5,6,7,8}};
	int arr34[][] = {{1,2},{3,4},{5,6,7,8}};
循环为数组赋值在输出

int arry[] = new int[5];
	for (int i = 1; i < arry.length; i++){
		arry[i] = i;
	}
	for( int i : arry)
	{
		System.out.print(i+" ");
		
	}
	}
}

2.字符串,String类

        String message1 = "Hello";
String message2 = "World";

字符串中有很多的方法,在字符串后可以调用:

public class Str {
	public static void main(String[] args){
	String message1 = "Hello";
	String message2 = "World";
	String message3 = "Hello" + "World";
	String message4 = "Hello".concat("World");
	String add1 = message1 + message2;
	
	System.out.println(add1);
	System.out.println(message3);
	System.out.println(message4);
	System.out.println("1+2="+1+2);
	System.out.println("1+2="+(1+2));
	System.out.println(message4+"的长度"+message4.length());
	System.out.println("l首次出现的位置"+message4.indexOf('l'));
	System.out.println("message4第二个字符"+message4.charAt(2));
3.类

1.访问权限修饰符

             1.   外部级别,可以使用public或者不用修饰符

             2.    成员级别,可以使用 public,private,protected或者不用修饰符

    对于一个在类内部定义的成员,无论使用何种修饰符都是可以被这个类访问,在同一个包中,不管类是否存在继承关系,仅有private修饰的成员不能被其他的类使用。在不同的包中,如果两个类存在继承关系,则使用protect修饰的成员可见,在全局范围内,仅有public修饰的成员可见

 2.类的定义

public class Student {

}

public class Student {              //访问修饰为public
public static void main(String[] args){
		
	Box a = new Box(10,20,30);       
	
		System.out.printf("Sque"+a.Sque());   //调用Box类的方法
		//datetype();
	

}

public static class Box {       //内部类
	 int length;           //成员变量
	 int wed;
	 int hight;
	public Box(int length,int wed,int hight)        //成员方法
	{
		this.length = length;
		this.wed = wed;
		this.hight = hight;
	}
public int Sque()        //成员方法
{
	return hight*wed*hight;
	
}

	
}

}
4.接口

     接口的定义

     [] interface 接口名   [extends 父接口名列表]

     [public][static][final] 变量;

      [public] [abstract]  方法;

public interface ICircle {
	
	double PI = 3.14159;
	double getCirumference(double radius);
	double getArea(double radius);
	double getVolume(double radius);
}
    接口的实现

  [修饰符]  class<类名> [extends  分类名] [implements 接口列表] {

}

public class Circle implements ICircle {

	@Override
	public double getArea(double radius) {
		// TODO Auto-generated method stub
		return 2 * PI * radius;
	}

	@Override
	public double getCirumference(double radius) {
		// TODO Auto-generated method stub
		return PI * PI * radius;
	}

	@Override
	public double getVolume(double radius) {
		// TODO Auto-generated method stub
		return 4 * PI * PI * radius/3.0;
	}
	

}
4.java常见的运算符(算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符)

    1.算数运算符

     加+,减-,乘*,除/,求余数%,自增++,自减--

    2.赋值运算符

    = 赋值

      +=:a+=b等价于a=a+b

      -=:a-=b等价于a=a-b

      *=:a*=b等价于a=a*b

      /=:a/=b等价于a=a/b

     3.比较运算符

      > :大于

      <:小于

     >=:大于等于

     <=:小于等于

      ==:等于

      !=:不等于

     4.逻辑运算符

    或:||                   与:&&                    非:!      

     5.位运算符

       按位与:|              按位或: &          右移>>           左移<<           0填充的右移


5.类的继承,重写

   定义一个类  (Biology),和他的两个子类animals,plant

public class Biology {
	
	//成员变量
	private String isFly;
	// 构造函数
	public Biology() {
	   isFly = "  ";
	}
	//方法
	public String canFly() {
        return isFly;
}
}
     子类 animals:
public class animals extends Biology {   //继承Biology类
	
	public animals() {
		isFly = "can";
	}

	private String isFly;
	
	
	public void canFly() {   //重写isFly()方法
		
		System.out.println("can fly");
	}
	
	

}
  子类 plant:

public class plant extends Biology {   //继承Biology类
	
	public plant() {
		isFly = "can not";
	}

	private String canFly;
	
	
	public void canFly() {   //重写isFly()方法
		
		System.out.println("can not fly");
	}
	
	

}
   定义一个接口,并在类中实现这个接口:
       接口ICircle

public interface ICircle {
	
	double PI = 3.14159; 
	double getCirumference(double radius);    //周长
	double getArea(double radius);           //面积
	double getVolume(double radius);        //体积
}
       定义类Circle实现接口 ICircle

public class Circle implements ICircle {

	@Override
	public double getArea(double radius) {          //周长
		// TODO Auto-generated method stub
		return 2 * PI * radius;
	}

	@Override
	public double getCirumference(double radius) {       //面积
		// TODO Auto-generated method stub
		return PI * PI * radius;
	}

	@Override
	public double getVolume(double radius) {           //体积
		// TODO Auto-generated method stub
		return 4 * PI * PI * radius/3.0;
	}
	

}










































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值