Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Java – Part I

Similar presentations


Presentation on theme: "An Introduction to Java – Part I"— Presentation transcript:

1 An Introduction to Java – Part I
Zachary Schoenstadt

2 Useful Links ava/index.html cs/api/

3 Basic Structure public class ClassName {
public static void main(String[] args) //program statements } //user defined methods Classes and methods have to be defined public private or protected. Static methods do not operate from within instanced objects. There can be only one public class in a single source file. The array args in the main method store command line arguments

4 Source Code, Compilation and Execution
Source code uses extension .java The Source code needs to be the same name as the public class Compiled into bytecode with extension .class Executed with command “java classname” which launches into an interpreter

5 Primitive Data Types Integers: int, short, long, byte
int foo = 1; Floating-Point Types: float, double float foo2 = 1.0f; The Character Type: char char foo3 = ‘a’; The Boolean Type: boolean (values: true, false) Boolean foo4 = true;

6 String Standard class (no need to import)
Strings are enclosed in double quotes String words = “Hello World!”; Concatenate with: + int compareTo(String other); returns 1,0 or -1 Boolean equals(Object other); int length() int compareTo(String other) returns a negative value if the implicit argument comes before the explicit argument (in alphabetical ordering), a positive value if the explicit argument comes before the implicit argument, and 0 if the strings are equal boolean equals(Object other) returns true if the implicit argument equals the explicit argument int length() returns the length of the string

7 Variables Variables in Java need to be declared.
You need to initialize variables before you use them. All variables/objects are passed by passed by reference, by default int n=5; System.out.println(n); String s=”Hello”; String t=”world”; System.out.println(s+” “+t);

8 If/else Statement Same as C++ If(…) //single statement if(…) { … }
else if else

9 Loops Again following C++ pattern while (…) { //stuff } do{ }while(…);
for(initialization; condition; update) {

10 Arrays Standard Class Declared like so:
type [] varname = new type[n]; Ex: int[] nubers = new int[10]; Ex: char[] letters = {‘a’ , ‘b’ , ‘c’ }; arrayName.length to findlength of the array static void sort(type[] a); static int binarySearch(type [] a, type v) ; static boolean equals(type [] a, Object other) static void sort(type [] a) Argument a is an array of a type listed in Data Types. sorts an array using a QuickSort algorithm static int binarySearch(type [] a, type v) Argument a is a sorted array of a type listed in Data Types, argument v has the same type as the elements of a. applies the BinarySearch algorithm to search for v static boolean equals(type [] a, Object other) Argument a is an array of a type listed in Data Types, 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

11 Last bits of Info Static functions are called through the class rather than instanciated objects Array.sort( ... ); // requires import java.util.Arrays Print to console using: System.out.print(...); // System.out.printl(…); //automaticly causes newline Use “import” to add packages akin to “#include” in c++ import java.util.Random; import java.util.*; Java has a Math.random and a Random in util with slightly different functionality


Download ppt "An Introduction to Java – Part I"

Similar presentations


Ads by Google