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一遍,对于像我这种初学者很有好处。

  • 相关阅读:
    19_多态及引用类型的转化
    18_接口以及基本实现
    17_super关键字 超,基,父
    Static 关键字
    17_抽象类
    17_继承
    数 函数类 Math类
    ArrayList类 Arrays类 注释
    我的第一篇博客
    hdu 3478 Catch--二分图判断
  • 原文地址:https://www.cnblogs.com/jahnson/p/8570747.html
Copyright © 2011-2022 走看看