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
相关推荐
浮游本尊3 分钟前
Java学习第21天 - 微服务架构设计
java
渣哥6 分钟前
Java CyclicBarrier 详解:原理、使用方式与应用场景
java
杨杨杨大侠13 分钟前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent
SimonKing14 分钟前
接口调用总失败?试试Spring官方重试框架Spring-Retry
java·后端·程序员
咖啡Beans15 分钟前
SpringCloud网关Gateway功能实现
java·spring cloud
杨杨杨大侠16 分钟前
Atlas Mapper 案例 01:初级开发者 - 电商订单系统开发
java·开源·github
华仔啊17 分钟前
Java 8都出了这么多年,Optional还是没人用?到底卡在哪了?
java
用户0920 分钟前
Gradle Cache Entries 深度探索
android·java·kotlin
循环不息优化不止40 分钟前
安卓 View 绘制机制深度解析
android
叽哥41 分钟前
Kotlin学习第 9 课:Kotlin 实战应用:从案例到项目
android·java·kotlin