Introduction to java Part I By Shenglan Zhang
Program Structure public class ClassName { public static void main(String[] args) program statements } user defined methods public class ClassName { public ClassName(){ Initilization of instance fields } user defined methods
Example public class HelloWorld { public static void main(String[ ] args) System.out.println(“Hello, World”); }
Explanation Public classes are accessible from any class. Classes, which are not declared public are accessible only within their package Public methods may be called from any class. (Encapsulation) Static methods do not operate on objects. (Math.arctan(x)) The keyword void indicates that the method does not return a value. The array args of the main method stores command line arguments. (Take arguments when the program runs from command line, java blahblah 1 2)
Source Code, Compilation and Execution: The source code and the public class have to have the same name. There only can be one public class in a Java program. The source code must have .java A bytecode file is created during compilation and has the extension .class The Java interpreter executes when we type command java followed by the class name
Data Types Integers: int, short, long, byte Floating-Point Types: float, double The Character Type: char The Boolean Type: boolean (values: true, false)
Operators Arithmetic Operators: +, -, *, /, % Relational Operators: ==, !=, <, >, <=, >= Logical Operators: &&, ||, ! **Compares references, not values. Use “==“ if you want to know if two references are to the same object
Strings Strings are a standard class in Java. Constant strings are enclosed in double quotes. String Concatenation: + Example: String a = “Hello”; String b = “World”; int n = 5; System.out.println(a+b); Will print: HelloWorld System.out.println(a+n); Will print: Hello5
Variables Variables in Java need to be declared. You need to initialize variables before you use them. Examples: int n=5; (without int, compiler will complain) System.out.println(n);
If/else Statement While Loops For Loops ** Enhanced for loop : For (int e:Array){ System.out.println(e); }
Selected Member Functions: int compareTo(String other) - Negative if the implicit argument comes before the explicit argument (in alphabetical order) - Positive if the explicit argument comes before the implicit argument - 0 if the strings are equal
boolean equals(Object other) True if the implicit argument equals the explicit argument Example: System.out.println(a.equals(a)); Will print true. int length() Returns the length of the string
Arrays Declaration: int[ 5 ] arr; Initialization: Int[ ] arr = new int[ 5]; Int[ ] arr = {1 ,2, 3, 4, 5}; Arr.length returns the length of the array.
Selected Member Functions: static void sort(type [] a) Sorts an array using a QuickSort algorithm static int binarySearch(type [] a, type v) a must be a sorted array ( v and a has the same type. Ex: int ) static boolean equals(type [] a, Object other) argument other is an object. returns true if other is an array of the same type as a, if it has the same length, and if the corresponding elements match
Java Sort Method of Arrays Class public class SortJava { public static void main(String[] args) for (int i=0; i<numbers.length; i++) numbers[i]=(int)(Math.random()*n); Arrays.sort(numbers); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); }
Quick Sort Algorithm in Java The code is contained in the directory java/quick_sort on dunx1 Structure: public class QuickSortJava { public static void main(String[] args) … } public static void sort(int[] v, int left, int right) public static void swap(int[] v, int i, int j) public static int rand(int left, int right)