zoukankan      html  css  js  c++  java
  • 动手动脑

    一、枚举类型的基本用法

    示例:

     1 public class EnumTest {
     2     public static void main(String[] args) {
     3         Size s=Size.SMALL;
     4         Size t=Size.LARGE;
     5         //s和t引用同一个对象?
     6         System.out.println(s==t);  //
     7         //是原始数据类型吗?
     8         System.out.println(s.getClass().isPrimitive());
     9         //从字符串中转换
    10         Size u=Size.valueOf("SMALL");
    11         System.out.println(s==u);  //true
    12         //列出它的所有值
    13         for(Size value:Size.values()){
    14             System.out.println(value);
    15         }
    16     }
    17 }
    18  enum Size{SMALL,MEDIUM,LARGE};

    结论:

    1.枚举类型是引用类型,不属于原始数据类型,它的每个具体值都引用一个特定的对象。相同的值则引用同一个对象

    2.可以使用“==”和equals()方法直接比对枚举变量的值


    二、反码、补码和原码

    1.原码:

    最简单的机器数表示法,用两个字节(byte,1字节=8位(bite)),16位表示的二进制整数。

    正整数的原码,最高位是0,负整数的原码最高位是1(计算机中最高位0表示正,1表示负),除最高位外其余各位表示这个整数的绝对值即可;

    例如:1010:最高位是“1”,表示这是负数,其他三位为“010”。sum=0*22+1*21+0*20=2,所以该二进制表示的数字是 -2  

    2.反码:

    正整数的反码=原码
    负整数的最高位不变还是1,其它位取反(0变1,1变0)就得到负整数的反码

    例如:

    5的原码与反码相同,表示为0101

    -5的原码是1101,符号位不变,其他位(101)按位取反得(010),所以-5的反码是1010

    3.补码:

    补码是cpu内部用来计算数据的:
    正整数的补码=原码=反码
    负整数的补码=反码+1

    例如:

    -3的原码:1011  反码:1100  补码:1101

    Java中是采用补码来表示的


    三、两数相加

     1 //An addition program 
     2 
     3 import javax.swing.JOptionPane;  // import class JOptionPane
     4 
     5 public class Adition {
     6 public static void main( String args[] )
     7 {
     8    String firstNumber,   // first string entered by user
     9           secondNumber;  // second string entered by user
    10    int number1,          // first number to add
    11        number2,          // second number to add
    12        sum;              // sum of number1 and number2
    13 
    14    // read in first number from user as a string
    15    firstNumber =
    16       JOptionPane.showInputDialog( "Enter first integer" );
    17 
    18    // read in second number from user as a string
    19    secondNumber =
    20       JOptionPane.showInputDialog( "Enter second integer" );
    21 
    22    // convert numbers from type String to type int
    23    number1 = Integer.parseInt( firstNumber ); 
    24    number2 = Integer.parseInt( secondNumber );
    25 
    26    // add the numbers
    27    sum = number1 + number2;
    28 
    29    // display the results
    30    JOptionPane.showMessageDialog(
    31       null, "The sum is " + sum, "Results",
    32       JOptionPane.PLAIN_MESSAGE );
    33 
    34    System.exit( 0 );   // terminate the program
    35 }
    36 }

     


     四、变量作用域

    1.每个变量都有一个“有效”的区域(称为“作用域”),出了这个区域,变量将不再有效。

    1 public class Test {
    2     private static int value=1;
    3     public static void main(String[] args) {
    4         int value=2;
    5         System.out.println(value);
    6     }
    7 }

    2. Java变量遵循“同名变量的屏蔽原则”,即在java中是不允许在同一个函数中声明同名变量的,但是可以在类中声明类的全局变量,然后在函数中声明同名的函数的局部变量。在该函数中,这个同名变量才管用,而且局部变量优先。


    五、Java中的类型转换

    • Int          32位       取值范围为 -2的31次方到2的31次方减1之间的任意整数(-2147483648~2147483647)

    • Short       16位       取值范围为 -32768~32767之间的任意整数

    • long        64位       取值范围为 -2的63次方到2的63次方减1之间的任意整数         (-9223372036854774808~9223372036854774807)

    • float        32位       取值范围为 3.402823e+38 ~ 1.401298e-45

    • double      64位       取值范围为 1.797693e+308~ 4.9000000e-324

    • char        8位       取值范围为  -128~127

    • byte        8位       取值范围为 -128~127之间的任意整数

    结论:Java会自动完成从取值范围相对较小的数据类型转换成取值范围相对较大的数据类型。


    五、double类型

    1 public class Test {
    2     public static void main(String[] args) {
    3         System.out.println("0.05+0.01="+(0.05+0.01));
    4         System.out.println("1.0-0.42="+(1.0-0.42));
    5         System.out.println("4.015*100="+(4.015*100));
    6         System.out.println("123.3/100="+(123.3/100));
    7     }
    8 }

     结论:

    • 使用double类型的数值进行计算,结果是不精确的

    • 浮点数是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示任意某个实数。具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学计数法。浮点计算是指浮点数参与的运算,这种运算通常伴随着因为无法精确表示而进行的近似或舍入。

     解决方法:使用BigDecimal类


    六、字符串

    1 public class Test {
    2 public static void main(String[] args) {
    3         int X=100;
    4     int Y=200;
    5     System.out.println("X+Y="+X+Y);
    6     System.out.println(X+Y+"=X+Y");
    7     }
    8 }

    结论:在System.out.println()中,如果string字符串后面是+和变量,会自动把变量转换成string类型,则加号起连接作用,然后把两个字符串连接成一个新的字符串输出;如果先有变量的加减运算再有字符串,那么会从左到右先计算变量的加减,然后再与后面的string结合成一个新的字符串

  • 相关阅读:
    【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
    【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
    【leetcode】236. Lowest Common Ancestor of a Binary Tree
    【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
    【leetcode】352. Data Stream as Disjoint Intervals
    【leetcode】897. Increasing Order Search Tree
    【leetcode】900. RLE Iterator
    BEC listen and translation exercise 26
    BEC listen and translation exercise 25
    BEC listen and translation exercise 24
  • 原文地址:https://www.cnblogs.com/znjy/p/13765566.html
Copyright © 2011-2022 走看看