java要点

Java要点

Chapter 1 概述

  1. 整数相除会返回除法的整数部分,9/5=1,想要得到正确的结果,应该9.0/5=1.8
  2. 利用eclipse开发java
    • file->new->java project
    • .java和.class文件在同一个目录下,方便访问
    • file->new->class 创建java程序

Chapter 2 基本程序设计

  1. 在源代码中,字符串常量不能跨行。可以将该字符串分成几个单独的子串,再用连接符+将他们合并

    System.out.println("Introduction to Java Programming," +
         "by Y. Daniel Liang");
    System.out.print("Welcome Jenny!"); //输出不换行
    System.out.println("Welcome Jenny!");//输出并换行
    
  2. 从控制台读取输入

    import java.util.Scanner; //Scanner is in the java.util package
    import java.util.* //导入java.util中所有的类
    Scanner input = new Scanner(System.in);//Create a Scanner object
    //创建一个Scanner对象就好了
    double radius = input.nextDouble(); 
    float area = input.nextFloat();
    

  3. 命名常量

    final datatype CONSTANTNAME = value;
    final double PI = 3.14159;
    
  4. 命名习惯

    • 小写字母命名变量和方法,若包含多个单词,利用驼峰命名法 例如:numberOfStudents,radius
    • 类名每个单词首字母大写 例如:System,ComputeArea
    • 常量所有字母大写 例如:PI
  5. 数值类型

  6. 幂运算:a的b次方

    int a = Math.pow(a,b);
    
  7. 数值型字面值:字面值两个数字间可以使用下划线,提高可读性 例如:232_45_4519

    • 表示long型的数值,在数据后加L或l 例如:2147483648L
    • 表示二进制0B 表示八进制0 表示十六进制 0x 例如:0B1111 07777 0xFFFF
    • 表示浮点型小数,数据后加D或d L或l 例如:1.0F
    • 科学计数法 表示某个小数乘10的x次方 例如:1.234E2 1.234E+2 1.234E-2
  8. 数值类型转换

    • 扩展类型 widening a type : 将范围较小的类型转换为范围较大的

    • 缩小类型 narrowing a type : 将范围较大的类型转换为范围较小的 必须使用类型转换,否则编译报错

      double d = 4.5 //显式转换
      int i = (int)d; //i becomes 4,but d is still 4.5
      
      int a  = 4;
      double b = a; //隐式转换
      
      
  9. 注意

     (int)(monthlyPayment * 100) / 100.0); //保留小数点后两位
    
     /*
      *可以利用System.currentTimeMillis()来从GMT1970年1月1日00:00:00
      *开始到现在时刻的毫秒数,这一时间也称为UNIX epoch 
      */
      
    // Obtain the total milliseconds since midnight, Jan 1, 1970
    long totalMilliseconds = System.currentTimeMillis();
    
    // Obtain the total seconds since midnight, Jan 1, 1970
    long totalSeconds = totalMilliseconds / 1000;
    

Chapter 3 选择

  1. else 总是与离它最近的if匹配,注意代码的可读性

  2. 判断两个浮点数相等

    	final double EPSILON = 1E-14; //double类型判断
    	final float EPISLON2 = 1E-7;  //float类型判断  
    	double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1;
    	double y = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1;
    	if(Math.abs(x - 0.5) < EPSILON )
    	    System.out.println(x " is approximately 0.5");
    	if(Math.abs(y - 0.5) < EPSILON2 )
    	    System.out.println(y " is approximately 0.5");
    
  3. 产生随机数

    int number1 = (int)(System.currentTimeMillis() % 10;
    //取当前时间的最后一位数字
    int number2 = (int)(System.currentTimeMillis() /10 % 10
    //取当前时间的倒数第二位数字
    
    int number =int)(Math.random() * 10//返回一个0到9之间的数字
    //Math.random可以获得一个0.0到1.0之间的随机double值,不包括1.0
    (int)(Math.random()*n) + m 
    //返回一个[m,m+n)的整数
    
  4. 落空行为 fall-through behavior

    switch(day){
    case 1: 
    case 2: //一旦匹配上其中一个case,就从匹配的case处开始执行
    case 3: //直到遇到break语句或达到switch语句的结尾
    case 4: //为了提高可读性,省略break的情况请添加注释
    case 5: System.out.println("Weekday");break;
    case 0:
    case 6: System.out.println("Weekend");break;
    }
    
  5. 运算符的优先级

  1. 判断闰年:某年可以被4整除而不能被100整除,或者可以被400整除

    // Check if the year is a leap year 
    boolean isLeapYear = 
      (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    

    判断5的倍数,或偶数

    if (number % 5 == 0)
      System.out.println("HiFive");
    
    if (number % 2 == 0)
      System.out.println("HiEven");
    
  2. 取出某整数的每个位

    int a = 12345; //d5d4d3d2d1
    int d1 = a % 10;
    int d2 = a / 10 % 10;
    int d3 = a / 100 % 10;
    int d4 = a / 1000 % 10;
    int d5 = a / 10000 % 10;
    

Chapter 4 数学函数、字符和字符串

  1. Math类:位于java.lang包中,java.lang包中的所有类是隐式导入的

    Math.pow(a,b)
    Math.random()
    
    //常量
    Math.PI
    Math.E //  自然对数的底 2.718...
    
    //三角函数方法
    Math.sin(radians) //以弧度为单位的三角正弦函数值
    Math.cos(radians)
    Math.tan(radians)
    Math.toRadians(degree)
    Math.toDegrees(radians)
    Math.asin(a) //返回以弧度为单位的反三角正弦函数值
    Math.acos(a)
    Math.atan(a)
    
    //指数函数方法
    Math.exp(x) //返回e的x次方
    Math.log(x) //返回x的自然对数 lnx
    Math.log10(x) //返回以10为底x的对数
    Math.pow(a,b) //返回a的b次方
    Math.sqrt(x) //对于x >= 0的数字,返回x的平方根
    
    //取整方法
    ceil(x) //x上取整为它最接近的整数,double返回
    floor(x)//下取整 double
    rint(x)//取整为最接近的 如果距离相等返回偶数的整数 double
    round(x)//如果是单精度 返回(int)Math.floor(x + 0.5)
             //如果是双精度 返回(long)Math.floor(x + 0.5)
      
        
    min(a,b) //返回最小值 int long float double
    max(a,b) //返回最大值
    abs(x) //返回绝对值
    
    (int)(Math.random()*b) + a //返回一个[a,a+b)的整数
    
  2. 字符数据类型和操作

    • 字符串字面值在双引号中,字符字面值在单引号中
    "A"是一个字符串
    'A'是一个字符
    
    • java支持unicode码,大多数计算机采用ascii码
发布了7 篇原创文章 · 获赞 0 · 访问量 390

猜你喜欢

转载自blog.csdn.net/weixin_44145782/article/details/102886666