1 / 12

Introduction to the Java Language

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 .

pebbles
Download Presentation

Introduction to the Java Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to the Java Language • Data Types • Operators • Control Flow Statements • Arrays • Functions -- Static Methods • View Classes as modules Lec2_Java

  2. Exampe1: Hello.java class Hello { public static void main(String[] args){ System.out.println("Hello World!"); } } Lec2_Java

  3. Data Types • byte,short,int,long • float,double • char • boolean • reference • array • object Lec2_Java

  4. Operators • Arithmetic operators +, -, *, /, %, ++, -- • Relational operators >, >=, <, <=, ==, != • Conditional operators &&, ||, ! • Bitwise operators >>, <<, >>>, &, |, ^, ~ Lec2_Java

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related