Android java基础知识

一.Android java基础知识

第一个java程序Hello Worid:

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

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Hello.java
root@ubuntu:/home/topeet/guyilian# java Hello
Hello, world!

循环打印的例子:

public class Hello {
	public static void main(String args[]) {
		int i = 0;
		for (i = 0; i < 3; i++) {
			System.out.println("Hello, world!");
		}
	}
}

运行结果: 

root@ubuntu:/home/topeet/guyilian# javac Hello.java 
root@ubuntu:/home/topeet/guyilian# java Hello
Hello, world!
Hello, world!
Hello, world!

java与C语言的数据类型对比,java中无指针类型的数据:


public class Var {
	public static void main(String args[]) {
		int   a = 3;
		float f = (float)3.14;
		float f2 = 3.14f;

		int i = 4;
		short s = 4;
		short s2 = (short)40000;

		//s = i;
		s = (short)(s + 1);  
		s = (short)(s + s2);

		/* Java has no pointer */
		//int* p = malloc(10*sizeof(int));
		int p[] = new int[10];
		int p2[] = {1,2,4}; /* static alloc */
		
		//char str[100];
		char str[] = new char[100];

		//char str2[] = "abc";

		String str2 = "abc";

		p = null;
		p2 = null;
		str = null;
		str2 = null;
	}
}


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


public class Hello {
	public static void main(String args[]) {
		System.out.println(add(1,2));
		System.out.println(add(1,2, 3));
		System.out.println(add(1.0f, 2.0f));
	}

	public static int add (int x, int y) {
		return x + y;	
	}
	
	public static int add (int x, int y, int z) {
		return x + y + z;	
	}

	public static float add (float x, float y) {
		return x + y;	
	}

}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Hello.java 
root@ubuntu:/home/topeet/guyilian# java Hello
3
6
3.0

函数传递参数,如果要修改传递的参数要使用指针,但是在java里面用的是数组的地址。

public class Hello {
	public static void main(String args[]) {
		int x = 1;
		fun (x);

		int p[] = new int[1];
		p[0] = 123;

		System.out.println("Before fun2: "+p[0]);
		fun2(p);
		System.out.println("After fun2: "+p[0]);
		
		System.out.println(x);
	}

	public static void fun(int x) {
		x = 100;
	}		

	public static void fun2(int[] p) {
		p[0] = 200;
	}		

}

运行结果:

root@ubuntu:/home/topeet/guyilian# java Hello
Before fun2: 123
After fun2: 200
1

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式_笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值