Basic Java Issues

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators

1. Java basic data type and reference data type
Https://www.cnblogs.com/Latiny/p/8099581.html

Case:

  class Test {public static void main (string[] args) {# Reference data type = Basic data structure + method Test T = new Test (        );        # BASIC data structure Char C1 = ' a ';        BYTE B1 = 2;        Short S1 = 13;        int i1 = 12;        Long L1 = 123;        Double D1 = 1234;        System.out.println ("+c1+i1");    System.out.println (T.judgetype (C1));    }public String judgetype (Object temp) {if (temp instanceof Byte) {return ' is a Byte type ';    The else if (temp instanceof integer) {return ' is an integer type ';    } else if (temp instanceof short) {return ' is short type ';    The else if (temp instanceof string) {return ' is a String type ';    The else if (temp instanceof long) {return ' is a long type ';    } else if (temp instanceof float) {return ' is Float type ';    The else if (temp instanceof double) {return "is a double type";    The else if (temp instanceof Character) {return "is Character type";    } else {return "is a reference data type"; }}}

2. Arithmetic operators

#错误: 不兼容的类型: 从double转换到int可能会有损失int i1 = 0.1;# 当“=”两侧数据类型不一致时,可以使用自动类型转换或使用强制类型转换原则进行处理int i1 = (int)0.1;int i1 *= 0.1;# 结果为 0System.out.println(i1);  

3. Logical operators

 “&”和“&&”的区别: 单&时,左边无论真假,右边都进行运算; 双&时,如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算! 建议使用 &&“|”和“||”的区别同理,||表示:当左边为真,右边不参与运算!异或( ^ )与或( | )的不同之处是:当左右都为true时,结果为false。

Example:

# 结果异常: Exception in thread "main" java.lang.ArithmeticException: / by zeroif ( false & (1/0 > 1) ){    System.out.println("true");}else{    System.out.println("false");}# 结果为false# 双&时,如果左边为假,那么右边不参与运算!if ( false && (1/0 > 1) ){    System.out.println("true");}else{    System.out.println("false");}

4, bitwise operators

// 带符号右移: 最高位用符号位补//  -8System.out.println(-31>>2);// 无符号右移: 最高位用0补//  1073741816System.out.println(-31>>>2);# 位运算解决基本数据类型交换int m = 5;int n = 17;System.out.println("m:"+m+" n:"+n);m = m ^ n;n = m ^ n; // (5 ^ 17) ^ 17 = 5m = m ^ n; // (5 ^ 17) ^ 5 = 17System.out.println("m:"+m+" n:"+n);

5, ternary operator

 // 三元运算符与if-else的联系与区别 // 1、三元运算符可简化if-else语句 // 2、三元运算符要求必须返回一个结果 // 3、if后的代码块可有多个语句示例:    // 三个值寻找最大值    int m = -5;    int n = 17;    int k = 12;    System.out.println( (k > ((m > n)? m:n)) ? k : ((m > n)? m:n) );

Basic Java Issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.