120 likes | 277 Views
Introduction to the Java Language . Data Types Operators Control Flow Statements Arrays Functions -- Static Methods View Classes as modules. Exampe1: Hello.java. class Hello { public static void main(String[] args){ System.out.println("Hello World!"); } }. Data Types .
E N D
Introduction to the Java Language • Data Types • Operators • Control Flow Statements • Arrays • Functions -- Static Methods • View Classes as modules Lec2_Java
Exampe1: Hello.java class Hello { public static void main(String[] args){ System.out.println("Hello World!"); } } Lec2_Java
Data Types • byte,short,int,long • float,double • char • boolean • reference • array • object Lec2_Java
Operators • Arithmetic operators +, -, *, /, %, ++, -- • Relational operators >, >=, <, <=, ==, != • Conditional operators &&, ||, ! • Bitwise operators >>, <<, >>>, &, |, ^, ~ Lec2_Java
Control Flow Statements • decision making • if-else, switch-case • loop • for, while, do-while • exception • try-catch-finally, throw • miscellaneous • break, continue, label: , return Lec2_Java
Arrays • Dynamic • Array initialization • a.length Type[] a = new Type[n] a[0] a[1] a[n-1] Type[] a; ... a = new Type[expn] a = new Type[]{e1,e2,...,en}; A = {e1,e2,…,en} Lec2_Java
Example 2: Max.java class Max { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; int max; max = a[0]; for (int i=1; i<a.length; i++){ if (a[i]>max) max = a[i]; } System.out.println(" max="+max); } } Lec2_Java
Example 3: PrintArray class PrintArray { public static void main(String[] args){ int[][] a = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}; for (int i=0; i<a.length; i++){ for (int j=0; j< a[0].length; j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } } Lec2_Java
Functions -- Static Methods class Max { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; System.out.println(" max="+max(a)); } static int max(int[] a){ int max = a[0]; for (int i=1; i<a.length; i++){ if (a[i]>max) max = a[i]; } return max; } } Lec2_Java
Functions -- Static Methods class PrintArray { public static void main(String[] args){ int[][] a = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}; printArray(a); } static void printArray(int[][] a){ for (int i=0; i<a.length; i++){ for (int j=0; j< a[0].length; j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } } Lec2_Java
View Classes as Modules class Sort { public static void selectionSort(int a[]){ int tmp; for (int i=0; i<a.length; i++) for (int j=i+1; j<a.length; j++) if (a[i]>a[j]){ tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } Lec2_Java
View Classes as Modules public class TestSort { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; Sort.selectionSort(a); for (int i=0; i<a.length; i++){ System.out.print(a[i]+" "); } } } Lec2_Java