java中操作符有哪些

发布时间:2021-08-26 11:12:11 作者:小新
来源:亿速云 阅读:141

这篇文章将为大家详细讲解有关java中操作符有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

0x001 算数运算符

int num1 = 1, num2 = 2;
    System.out.println(num1 + num2); // 3
    System.out.println(num1 - num2); // -1
    System.out.println(num1 / num2); // 0 
    System.out.println(num1 * num2); // 2
    System.out.println(num1 % num2); //1

0x002 自增自减

System.out.println(num1++); // 1
    System.out.println(num1); // 2
    System.out.println(num1--); // 2
    System.out.println(num1); // 1
    System.out.println(++num1); // 2
    System.out.println(--num1); // 1

0x003 关系操作符

System.out.println((num1 < num2)); // true
    System.out.println((num1 > num2)); // false
    System.out.println(num1 == num2); // false
    System.out.println(num1 != num2); // true

0x004 逻辑操作符

boolean boolean1 = false; 
    boolean boolean2 = true; 
    System.out.println(boolean1 && boolean2); // false
    System.out.println(boolean1 || boolean2); // true
    System.out.println(!boolean1); // true

0x005 直接操作符

int i1 = 0x2f;
    int i2 = 0x2F;
    int i3 = 0177;
    char c1 = 0xffff;
    byte b1 = 0x7f;
    short s1 = 0x7f;
    long l1 = 100L;
    long l2 = 100l;
    float f1 = 1;
    float f2 = 1f;
    float f3 = 1F;
    double d1 = 1d;
    double d2 = 1D;
    System.out.println(i1); // 47
    System.out.println(i2); //47
    System.out.println(i3); // 127
    System.out.println(c1); // 
    System.out.println(b1); // 127
    System.out.println(s1); // 127
    System.out.println(l1); // 100
    System.out.println(l2); // 100
    System.out.println(f1); // 1.0
    System.out.println(f2); // 1.0
    System.out.println(f3); // 1.0
    System.out.println(d1); // 1.0
    System.out.println(d2); // 1.0

0x006 三元运算符

int a = 0;
    boolean isSuccess = false;
    a = isSuccess ? 1 : 2;
    System.out.println(a);// 2

0x007 字符串+、+=

String str = "";
    str = str + "1";
    str += "2";
    System.out.println(str);

0x008 类型转化

int i = 100;
    long long1 = (long) i;
    System.out.println(long1);// 100
    long1 = i;
    System.out.println(long1);// 100
    long long2 = (long) 200;
    System.out.println(long2);// 200
    i = (int) long1;
    System.out.println(i); // 200
    
    float float1=0.1f; 
    float float2=0.9f;
    System.out.println((int) float1);// 转化int会被舍去
    System.out.println((int) float2);// 转化int会被舍去

关于“java中操作符有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. js中操作符有哪些类型
  2. JS中new操作符有什么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:python语言元素知识点有哪些

下一篇:C语言链表的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》