【Java 基础篇】【第二课】基本数组类型

就像第一章所说一样,这次学习为了快,因此说明性的文字就不想写太多了,直接帖代码吧,代码当中尽量加一些注释:

  1 package a.b;
  2 
  3 public class test 
  4 {
  5 
  6     static void BasicVariables()
  7     {
  8         //一、变量的类型的学习
  9         System.out.println("一、变量的类型的学习 ");
 10         
 11         // byte 表数范围:-128~127, 存储大小占1byte  
 12         byte a; 
 13         a = 12;
 14         System.out.println("byte num Is " + a);
 15                 
 16         // int 占4字节
 17         int b;
 18         b = 66633;
 19         System.out.println("int num Is " + b);
 20             
 21         // short 占2字节
 22         short c;
 23         c = 1234;
 24         System.out.println("short num Is " + c);
 25         
 26         // long 占2字节
 27         long d;
 28         d = 655366;
 29         System.out.println("long num Is " + d);
 30         
 31         float e; 
 32         e = (float)12.6;
 33         System.out.println("fload num Is " + e);
 34                 
 35         // int 占4字节
 36         double f;
 37         f = 33.4;
 38         System.out.println("double num Is " +f);
 39             
 40         // short 占2字节
 41         char g;
 42         g = 'a';
 43         System.out.println("char num Is " + g);
 44         
 45         // long 占2字节
 46         boolean h;
 47         h = true;
 48         System.out.println("boolean num Is " + h);
 49     }
 50     
 51     static void AboutArrays()
 52     {
 53         // 二、 数组的学习
 54         System.out.println("二、 数组的学习 ");
 55         
 56         
 57         // 基本类型数组赋值、输出
 58         int []a ;
 59         a = new int [5];
 60         
 61         a[0] = a[1] = a[2] = a[3] = a[4] = 9;
 62         
 63         for (int i = 0; i < 5; i++) 
 64         {
 65             System.out.println(a[i]);
 66         }
 67         
 68         // 基本类型数组赋值、输出
 69         int []b = new int [5];
 70         
 71         for (int i = 0; i < 5; i++) 
 72         {
 73             b[i] = a[i] + i +1;
 74             System.out.println(b[i]);
 75         }
 76         
 77         // 基本类型数组初始化时候赋值
 78         int []c = new int [] {3,4,5,6,7};
 79                 
 80         for (int i = 0; i < 5; i++) 
 81         {
 82             System.out.println(c[i]);
 83         }
 84         
 85         // 字符数组
 86         String []d = new String [] {"you","are","my","small","apple"};
 87                         
 88         for (int i = 0; i < 5; i++) 
 89         {
 90             System.out.println(d[i]);
 91         }
 92     }
 93     
 94     public static void main(String[] args)
 95     {
 96         //一、基本变量
 97         BasicVariables();
 98         
 99         //二、数组
100         AboutArrays();
101     }
102     
103     
104     
105 }

 附带一下输出结果把:

一、变量的类型的学习 
byte num Is 12
int num Is 66633
short num Is 1234
long num Is 655366
fload num Is 12.6
double num Is 33.4
char num Is a
boolean num Is true
二、 数组的学习 
9
9
9
9
9
10
11
12
13
14
3
4
5
6
7
you
are
my
small
apple

 

 

这里需要说一个问题,就是刚开始 我自己写的两个函数都不是静态的,编译的时候就发生了报错

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
 Cannot make a static reference to the non-static method BasicVariables() from the type test

后来在网上查了之后:解释为在静态方法中,不能直接访问非静态成员(包括方法和变量)。因为,非静态的变量是依赖于对象存在的,对象必须实例化之后,它的变量才会在内存中存在。

所以解决方案就是以下两种:

 

// 方法一:在静态函数main中,实例化对象之后进行调用非静态函数BasicVariables
public class test 
{
    void BasicVariables()
    {
        //一、变量的类型的学习
        System.out.println("一、变量的类型的学习 ");
    }    
    
    public static void main(String[] args)
    {
        test t = new test();
        t.BasicVariables();
    }    
}


//方法二:直接定义静态函数BasicVariables,在静态函数main中直接调用
public class test 
{
    static void BasicVariables()
    {
        //一、变量的类型的学习
        System.out.println("一、变量的类型的学习 ");
    }    
    
    public static void main(String[] args)
    {
        BasicVariables();
    }    
}

 

 

学习参考的地方:

http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html

vamei的真心写的不错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值