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

 

转载于:https://www.cnblogs.com/jahnson/p/8570747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值