zoukankan      html  css  js  c++  java
  • java学习笔记(1)

    1.初体验

    编写HelloWorld程序

    public class HelloWorld{
        public static void main(String[] args) {
            System.out.println("Hello World");
        }
    
    }
    

    2.变量类型

    public class VariableDemo {
        public static void main(String[] args) {
            // 定义byte类型,一个字节,范围-128到127
            byte b = -128;
            System.out.println(b);
            // 定义short类型
            short s = 100;
            System.out.println(s);
            // 定义int
            int i = 1000;
            System.out.println(i); 
            // 定义float
            float c = 3.14f;
            System.out.println(c);
            // 定义double
            double d = 3.14;
            System.out.println(d);
            // 定义boolean
            boolean x = true;
            System.out.println(x);
        }
    }
    

    3.算数运算符

    package daigua.zhuwa;
    
    public class OperatorDemo {
        public static void main(String[] args) {
            int a = 3;
            int b = 4;
            System.out.println(a + b);
            System.out.println(a - b);
            System.out.println(a * b);
            System.out.println(a / b);
            
            // 有浮点数参与才会显示小数
            float c = 3.0f;
            System.out.println(c / b);
            
            int d = 10;
            int e = 3;
            System.out.println(d % e); // 取余
            
            // 字符参与加法运算
            char f = 'A';
            System.out.println(f);
            System.out.println(a + f);
            // 字符串跟其他数据做加法 是做字符串的拼接
            System.out.println("hello" + a);
            System.out.println(a + b + "hello");
            
            // ++ -- 自增自减,单独使用时,放在前面或者后面效果一样
            int g = 10;
            System.out.println("g:" + g);
            g++;
            System.out.println("g:" + g);
            ++g;
            System.out.println("g:" + g);
            
    //      int h = a;
    //      int h = a++;
            int h = ++ a;
            System.out.println("a:" + a);
            System.out.println("h:" + h);
    
        }
    }
    

    4.赋值运算符

    package daigua.zhuwa;
    /*
     * 赋值运算符:
     * 基本: = 
     * 扩展: += -= *= /=
     * */
    
    public class OperatorDemo2 {
        public static void main(String[] args) {
            int a = 10;
            System.out.println("a:" + a);
            
            // +=
            a += 20;
            System.out.println("a:" + a);
            
            // 注意,扩展的赋值运算符隐含了强制类型转换
            short s = 1;
            s += 1;
            System.out.println("s:" + s);
        }
    }
    

    5.关系运算符

    package daigua.zhuwa;
    /*
     * 关系运算符
     *      == != > >= < <=
     * */
    public class OperatorDemo3 {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
            int c = 10;
            System.out.println(a == c);
            System.out.println(a == b);
            System.out.println(a < b);
            System.out.println(a < c);
            System.out.println(a <= c);
            System.out.println(a != b);
        }
    }
    

    6.逻辑运算符

    package daigua.zhuwa;
    /*
     * 逻辑运算符
     * & | ^ !
     * && ||
     * */
    public class OperatorDemo4 {
        public static void main(String[] args) {
            int a = 3;
            int b = 4;
            int c = 5;
            
            // & 就是and,同时满足
            System.out.println((a>b) & (a>c));
            System.out.println((a>b) & (a<c));
            System.out.println((a<b) & (a<c));
            System.out.println((a<b) & (a>c));
            
            // | 就是or,一个满足
            System.out.println((a>b) | (a>c));
            System.out.println((a>b) | (a<c));
            System.out.println((a<b) | (a<c));
            System.out.println((a<b) | (a>c));
            
            // ^ 就是异或,不同为真
            System.out.println((a>b) ^ (a>c));
            System.out.println((a>b) ^ (a<c));
            System.out.println((a<b) ^ (a<c));
            System.out.println((a<b) ^ (a>c));
            
            // ! 非
            System.out.println(!(a>b));
            System.out.println(!(a<b));
            
            // && 跟&结果一样,区别在于有短路效应
            System.out.println((a>b) && (a>c));
            System.out.println((a>b) && (a<c));
            System.out.println((a<b) && (a<c));
            System.out.println((a<b) && (a>c));
            
            // || 跟|结果一样,区别在于有短路效应
            System.out.println((a>b) || (a>c));
            System.out.println((a>b) || (a<c));
            System.out.println((a<b) || (a<c));
            System.out.println((a<b) || (a>c));
    
        }
    }
    

    7.三元运算符

    package daigua.zhuwa;
    /*
     * 三元运算符
     * 关系表达式?表达式1:表达式2
     * */
    
    public class OperatorDemo5 {
        public static void main(String[] args) {
            int a = 1;
            int b = 2;
            int c = (a > b)? a:b;
            System.out.println(c);
        }
    }
    

    8.键盘输入

    package daigua.zhuwa;
    import java.util.Scanner;
    /*
     * 实现键盘录入,使用Scanner
     * */
    public class MyScanner {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            
            System.out.print("请输入:");
            int i = sc.nextInt();
            
            System.out.println("结果:"+ i);
        }
    }
    
  • 相关阅读:
    机器学习100天——实现简单线性回归(第二天)
    一起学Hive——详解四种导入数据的方式
    一起学Hive——创建内部表、外部表、分区表和分桶表及导入数据
    Apache Pulsar——企业级消息订阅系统介绍
    一起学Hadoop——实现两张表之间的连接操作
    一起学Hadoop——文件的上传、分发与打包
    机器学习100天——数据预处理(第一天)
    java调用python程序以及向python程序传递参数
    页面中查询模块的设计与实现思路
    以前的博客内容迁至CSDN,博客名不变,以后博客将在两个平台同步更新
  • 原文地址:https://www.cnblogs.com/daigua/p/java-xue-xi-bi-ji-1.html
Copyright © 2011-2022 走看看