《Java 基础一》

1 Java基本数据类型
2 public class Main {
3 public static void main(String[] args) {
4 /**
5 * 输出java基本数据类型的数值范围
6 */
       
     System.out.println("byte : " + Byte.MIN_VALUE + " to "
+ Byte.MAX_VALUE);//short的范围
8
7 System.out.println("short : " + Short.MIN_VALUE + " to "
8 + Short.MAX_VALUE);//short的范围
9 System.out.println("integer : " + Integer.MIN_VALUE + " to "
10 + Integer.MAX_VALUE);//integer的范围
11 System.out
12 .println("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
13 System.out.println("float : " + Float.MIN_VALUE + " to "
14 + Float.MAX_VALUE);//long的范围
15 System.out.println("double : " + Double.MAX_VALUE + " to "
16 + Double.MAX_VALUE);//double的范围
17 System.out
18 .println("boolean : " + Boolean.FALSE + " to " + Boolean.TRUE);//boolean的范围
19      System.exit(0);
20 }
1 Java基本变量的申明/定义
2 public class Main {
3 public static void main(String[] args) {
4 short shorta = 0;
5 Short shortb = new Short((short)0);//将0强行转换为short 0;
6 int inta = 0;
7 Integer intb = new Integer(0);
8 long longa = 0;
9 Long longb = new Long(0);
10 float floata = 0;
11 Float floatb = new Float(0);
12 double doublea = 0;
13 Double doubleb = new Double(0);
14 boolean booleana = true;
15 Boolean booleanb = new Boolean(true);
16 System.exit(0);
17 }
18
19 }
1 Java数组
2 //定义数组方式
3 int[] intArrayA = new int[10];
4 //定义并初始化数组方式
5 int[] intArrayB = new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
6 //输出完整数组方法”[10,9,8,7,6,5,4,3,2,1]”
7 System.out.println(Arrays.toString(arrayA));
8 for (int i = 0; i < intArrayA.length; i++)
9 {
10 intArrayA[i] = i;
11 }
12 //类似于foreach的方式输出数组
13 for (Integer i : intArrayA)
14 {
15 System.out.println(i);
16 }
1 Java泛型
2 //用泛型定义数据类型
3 List<Integer> intListA = new ArrayList<Integer>();
4 intListA.add(0);
5 System.out.println(intListA.get(0));
6 //类似foreach的方法输出数据
7 for (Integer i : intListA)
8 {
9 System.out.println(i);
10 }
11 //泛型数组
12 int[] intArrayB = new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
13 List<int[]> intListB = new ArrayList<int[]>();
14 //数组向泛型转换
15 intListB = Arrays.asList(intArrayB);
16 for (Integer i : intListB.get(0)){
17 System.out.println(i);
18 }
1 //Java IO
2 import java.io.File;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.util.Scanner;
6
7 public class Main {
8 public static void main(String[] args) throws IOException {
9      System.out.println("Pleas input two Integer: ");
10 Scanner keyboard = new Scanner(System.in);
11 int inta = keyboard.nextInt();
12 int intb = keyboard.nextInt();
13 System.out.println("sum : " + (inta + intb));
14 Scanner dataInput = null;
15 try {
16 dataInput = new Scanner(new File("F:\\data.txt"));
17 } catch (FileNotFoundException e) {
18 e.printStackTrace();
19 System.out.println(e.getMessage());
20 }
21 String str;
22 while (dataInput.hasNext()) {
23 str = dataInput.nextLine();
24 System.out.println(str);
25 }
26 System.exit(0);
27 }

posted on 2011-02-26 16:10  谁家的猫啊  阅读(266)  评论(0编辑  收藏  举报

导航