zoukankan      html  css  js  c++  java
  • Java基础语法

    内存区分配:

    1. code segment:存放代码

    2. data segment: 静态变量

    3. stack: 局部变量

    4. heap: new出来的东西

    8种基本数据类型:

    布尔型: boolean

    字符型: char:1 字节

    整数型:

    byte:1字节   -127-128

    short:2字节  -2 15次方--2 15次方-1

    int:4字节  

    long:8字节

    浮点类型:float (4字节,  10的38次方), double(8字节, 10的300次方)

    引用数据类型: 类,接口, 数组

    double放到float变量里不行, long 放到int里不会出错

    数据类型转换:

    1. boolean类型不能转换成其他类型

    2. 整形int, 字符型char, 浮点型float数据可以相互转换

    3. 容量小的类型自动转换为容量大的类型:容量指的是代表的数字的多少, 大小排序为: 

        byte,short,char-->int-->long-->float-->double(byte,short,char运算时自动转换成int,int+long=long...)

        byte, short, char不能互相转换,他们三者在计算时首先自动转换为int类型

    4. 容量大的转换为容量小的数据类型要加上强制转换符, 但会造成精度降低或者溢出.

    5. 多种类型的数据混合计算时, 系统自动将所有的数据转换成容量最大的那种数据类型, 然后进行计算.

    6. 实数常量默认为double, 整形常量默认为int

    7. double变量不能强制转换为float, 因为浮点型存的方式不一样

    8. float类型要标明f

    byte=1 ;  //可以, 只要别超过127
    byte b3=b1+b2; //不可以, 要强制转换(byte),改成下面
    byte b3=(byte)b1+b2; //如果超过了127, 会自动砍掉前面的字节
    float f1=1.23f; //必须加f
    float f2=3.2f;
    float f3=f1+f2; //不可以, 要强制转换, 因为会变成double
    

      

    public class TestConvert {
        public static void main(String arg[]) {
            int i1 = 123; 
            int i2 = 456;
            double d1 = (i1+i2)*1.2;//系统将转换为double型运算
            float f1 = (float)((i1+i2)*1.2);//需要加强制转换符
            byte b1 = 67; 
            byte b2 = 89;
            byte b3 = (byte)(b1+b2);//系统将转换为int型运算,需
                                    //要强制转换符
            System.out.println(b3);
            double d2 = 1e200;
            float f2 = (float)d2;//会产生溢出
            System.out.println(f2);
    
            float f3 = 1.23f;//必须加f
            long l1 = 123;
            long l2 = 30000000000L;//必须加l
            float f = l1+l2+f3;//系统将转换为float型计算
            long l = (long)f;//强制转换会舍去小数部分(不是四舍五入)
    
        }
    }
    
    public class TestConvert2 {
    	public static void main(String[] args) {
       
        int i=1,j=12;
        float f1=(float)0.1;  //0.1f
        float f2=123;
        long l1 = 12345678,l2=8888888888L;
        double d1 = 2e20,d2=124;
        byte b1 = 1,b2 = 2,b3 = 127;
        j = j+10;
        i = i/10;
        i = (int)(i*0.1);
        char c1='a',c2=125;
        byte b = (byte)(b1-b2);
        char c = (char)(c1+c2-1);
        float f3 = f1+f2;
        float f4 = (float)(f1+f2*0.1);
        double d = d1*i+j;
        float f = (float)(d1*5+d2);
        }
    }
    

      

      

    char类型相加自动转换int,需要强制转换回来:

    public class Test {
    	public static void main(String[] args){
    		char c1='c';
    		char c2=1;
    		char c=(char)(c1+c2);
    		System.out.println("c="+c);
    		//System.out.println("c2="+c2);
    	}	
    }
    

    阶乘相加的程序:

    public class Test{
    	public static void main(String[] args){ 
    		long result = 0;
      		long f = 1;
      		for(int i=1;i<=10;i++){
      			f=f*i;
      			result=result+f;
      		}
      		System.out.println(result);
    	}
    }
    

      

    1+3+5+...+99:

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

      

    输出0~9:

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

     while break, continue:

    public class Test{
    	public static void main(String args[]){
    		int stop=4;
    		for(int i=1;i<10;i++){
    			if(i==stop) break;
    			System.out.println(i);
    		}	
    	}
    }
    

    1-100被3整除的前5位:

    public class Test{
    	public static void main(String args[]){
    		int num=0;
    		for(int i=1;i<=100;i++){
    			if(i%3==0){
    				System.out.println(i);
    				num++;
    			}
    			if(num==5) break;
    		}
    	}
    }
    

      

    101-200之间的质数:只能被自身和1整除的数:

    public class Test{
    	public static void main(String args[]){
    		for(int i=101;i<200;i+=2){
    			boolean f=true;
    			for(int j=2;j<i;j++){
    				if(i%j==0){
    					f=false;
    					break;
    				}
    			}
    			if(!f) continue;
    			System.out.println(i);
    		}
    		
    	}
    }
    

    switch语句: case可以合并, 小心case穿透, 要使用break, switch里面的变成只能检测int型, 所以可以使用可以转换为int的 char,byte,short类型

    public class Test{
    	public static void main(String args[]){
    	int i=8;
    		switch(i){
    			case(2):
    			case(3):
    			case(5):
    			case(8):
    				System.out.println("AAAA");
    				break;
    			case(9):
    				System.out.println("bbbb");
    				break;
    			default:
    				System.out.println("bbbb");			
    		}		
    	}
    }
    

      

     

     

  • 相关阅读:
    redhat linux 5.6 下安装oracle 11g 时netca报错不能配置监听解决方法
    数据库迁移windows>linux ORACLE 10G
    RedHat Linux 5.3 下安装ORACLE 软件之后手动安装数据库脚本记录
    X Server/Client
    Oracle实例解析:编码与字符集(转)
    RedHat Linux 5.3 下安装ORACLE DATABASE 10G
    Toad 10.5 连接RedHat Linux 5.3 下Oracle 10g
    RedHat5.6 安装mysql
    oracle 数据库开发原则(转自求道的路上)
    第一篇使用windows live writer发布日志
  • 原文地址:https://www.cnblogs.com/wujixing/p/5211057.html
Copyright © 2011-2022 走看看