第1课_Java基础知识【学习笔记】

摘要:韦东山android视频学习笔记

一、android系统的基本框架如图一所示,应用程序是用java编写的,底层驱动是用C代码写的,java与C怎么连接在一起主要是通过本地服务。android系统的核心主要在于framwork层.

图一

二、

2.1 第一个java程序:需要注意的是类的命名,首字母必须要是大写,而且文件的名字要跟类名保持一致。

1 public class Hello{
2     public static void main(String args[]){
3         System.out.println("Hello,World!");
4     }
5 }

编译运行命令如图二:

图二

2.2 循环打印的例子:

1 public class Hello{
2     public static void main(String args[]){
3         int i = 0;
4         for (i = 0;i < 3;i ++){
5             System.out.println("Hello World");
6         }
7     } 
8 }

编译运行命令如图三:

 

图三

2.3 java与C语言的数据类型对比如图四,java中无指针类型的数据,有引用类型的数据.java中字符串是一个string类。数据不丢失的前提下可以自动转换,

图四

 

图五

相关代码:

 1 public class Var{
 2     public static void main(String args[]){
 3         int a = 3;   //整数默认是int,
 4         float f1 = (float)3.14; //有小数时默认是double,因此这里需要转换不然数据精度会丢失 
 5         float f2 = 3.14f;
 6 
 7         int i = 4;
 8         short s = 4;    //数据不丢失的前提下可以自动转换
 9         short s2 = (short)40000;  //超过数据范围,需要强制转换
10 
11         //s = i;
12         s = (short)(s + 1);    //,因为对于byte,short的运算,为了保证精度,会自动转换为int类型,因此等号右边需要强制转换
13         s = (short)(s + s2);
14 
15         //int* p = malloc(10*sizeof(int)); 
16         int p[] = new int[10];
17         int p2[] = {1,2,4}; // static alloc
18 
19         // char str[100];
20         char str[] = new char[100];
21 
22         //char str2[] = "abc";
23         String str2 = "abc";
24 
25 
26         p = null;
27         p2 = null;
28         str = null;
29         str2 = null;
30         
31     }
32 }

2.4与C语言相比,Java的函数可以进行重载的操作,对函数的个数以及函数参数的类型也能够进行重载.相关代码如下

 1 public class Function{
 2     public static void main(String args[]){
 3         System.out.println(add(1,2));
 4         System.out.println(add(1,2,3));
 5         System.out.println(add(1.0f,2.0f));
 6     }
 7 
 8     public static int add (int x,int y){
 9         return x + y;
10     }
11 
12     public static int add (int x,int y,int z){
13         return x + y + z;
14     }
15 
16     public static float add (float x,float y){
17         return x + y;
18     }
19     
20 }

编译运行结果如下图:

图六

2.5函数传递参数,如果要修改传递的参数要使用指针,相关代码如下:

 1 public class Param {
 2     public static void main(String args[]) {
 3         int x = 1;
 4         fun (x);
 5 
 6         int p[] = new int[1];
 7         p[0] = 123;
 8 
 9         System.out.println("Before fun2: "+p[0]);
10         fun2(p);
11         System.out.println("After fun2: "+p[0]);
12         
13         System.out.println(x);
14     }
15 
16     public static void fun(int x) {
17         x = 100;
18     }        
19 
20     public static void fun2(int[] p) {
21         p[0] = 200;
22     }        
23 
24 }

编译运行执行结果:

图七

 相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask 

 

原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/10851916.html