zoukankan      html  css  js  c++  java
  • java的几个入门小程序

    1.先是最最经典的hello world!

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

    2.print与println的区别

    public class Hello {
        public static void main(String args[])
     {
            System.out.print("hello world!"); 
            System.out.println("hello world!");
            System.out.println("hello world!");
         }
    }
    hello world!hello world!
    hello world!

    显然println结束后有换行而print没有,所以用println比较好

    3.println的用法

    public class Hello {
        public static void main(String args[])
     {
            int x=2;
            System.out.println(x+"*"+x+"="+(x*x));
         }
    }
    2*2=4

    这里要输出的数字用“+”号连接

    4.常量的声明

    public class Hello {
            static final int YEAR=365;
            public static void main(String args[]) {
            System.out.println("两年等于"+2*YEAR+"天");
         }
    }
    两年等于730天
    public class Hello {
            public static void main(String args[]) {
            final int YEAR=365;
            System.out.println("两年等于"+2*YEAR+"天");
         }
    }
    两年等于730天

    常量赋值语法final+数据类型+变量名称=变量值,当常量用于一个类的成员变量时,必须给常量赋值。

    5.变量的声明

    public class Hello {
            public static void main(String args[]) {
            int num=3;
            char ch='a';
            System.out.println(num+"是整数");
            System.out.println(ch+"是字符");
         }
    }
    3是整数
    a是字符

       这些程序均是从java书中摘抄的,将书中的代码自己用编译器run一遍,对于像我这种初学者很有好处。

  • 相关阅读:
    Pigeon源码分析(一)-- 服务注册
    使用postman配置header的惨痛经历
    Redis字典知识点总结
    SpringBoot和Mybatis结合原理
    Mycat误区解释
    Mycat之常用分片规则
    Mycat之ER表划分
    模拟死锁
    生产者消费者阻塞队列
    堆排序TopK
  • 原文地址:https://www.cnblogs.com/jahnson/p/8570747.html
Copyright © 2011-2022 走看看