160 likes | 193 Views
An Introduction to Java – Part 1. Erin Hamalainen CS 265 Sec 001 October 20, 2010. Some Java Basics. Derives most of its syntax from C++ Built almost exclusively as an object-oriented programming language All code is written inside a class
E N D
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010
Some Java Basics • Derives most of its syntax from C++ • Built almost exclusively as an object-oriented programming language • All code is written inside a class • Everything is an object, with the exception of the intrinsic data types
Data Types (also called primitive types) • Byte • Min value: -128 • Max value: 127 • 8 bits (used for memory saving purposes) • Short • Min value: -32,768 • Max value: 32,767 • 16 bits • Int • Min value: -2,147,483,648 • Max value: 2,147,483,647 • 32 bits • For integral types, this data type is generally the default • Long • Min value: -9,223,372,036,854,775,808 • Max value: 9,223,372,036,854,775,807 • 64 bits • Use this only when you need a range of values wider than those provided by int
Data Types (also called primitive types) • Float • Use a float (instead of a double) if you need to save memory • Single precision, 32 bit • Double • For decimal values this is the default choice • Double precision, 64 bit • Boolean • Two possible values: true and false • Char • A single character
Arrays • A container object that holds a fixed number of values of a single type • Length is established when the array is created. After creation, length is fixed. • Declaration: int[] anArray; // declares an array of integers • The declaration does not actually create an array. It simply tells the compiler that this variable will hold an array of the specified type.
Arrays cont’d • You can also declare an array like this: int anArray[]; //this form is discouraged • However, convention discourages this form; the brackets identify the array type and should appear with the type designation. int[] anArray; //declares an array of integers anArray = new int[10]; //allocates memory for 10 integers • Alternatively you can use the shortcut syntax to create and initialize an array int[] anArray = {1,2,3,4,5,6,7,8,9,10}; • Here the length of the array is determined by the number of values between { and }.
Arrays cont’d • Elements of an array are accessed using subscripts (just like in C++ and Perl) int[] anArray; //declares an array of integers anArray = new int[10]; //allocates memory for 10 integers anArray[0] = 1; //initialize first element anArray[1] = 2; //Initialize second element System.out.println(“Element at index 0: “ + anArray[0]); System.out.println(“Element at index 1: “ + anArray[1]);
Selected Member Functions for Arrays Static void sort(type[] a) • Sorts an array using a QuickSort algorithm Static int binarySearch(type[] a, type v) • Argument a is a sorted array. This function searches for v among the elements of a and returns subscript if v is found. Static boolean equals(type[] a, Object other) • This function 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
Strings • Strings are a standard class in Java String str1; //declare a string variable str1 = “Erin”; //initialize string variable String str2 = “My name is” //declaration and initialization in one line • String concatenation: + String str3 = str2 + “ “ + str1 + “.”; //str3 holds the string “My name is Erin.”
Selected Member Functions for Strings int compareTo(String other) • Returns a negative value if the implicit argument comes before the explicit argument, a positive value if the explicit argument comes before the implicit argument, and 0 if the strings are equal int cmp = impStr.compareTo(expStr); boolean equals(Object other) • Returns true if the implicit argument equals the explicit argument. Returns false otherwise. boolean eq = impStr.equals(expStr); int length() • Returns the length of the string int length = impStr.length();
Operators • Arithmetic Operators +, -, *, /, % • Relational Operators ==, !=, <, >, <=, >= • Logical Operators &&, ||, !
Control Flow • If else statements • Same as in C++ • Curly braces are unnecessary if they only enclose one statement • While loops • Same as C++ • Curly braces are unnecessary if they only enclose one statement • For loops • Same as C++ for(int i=0; i<5; i++) System.out.println(numbers[i]);
Program Structure Public class className { public static void main(String[] args) { program statements } user-defined methods } • Public classes are accessible from any class • Classes that are not declared public are accessible only within their package • (A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. ) • Public methods may be called from any class • Static methods do not operate on objects • The keyword void indicates that the method does not return a value • The array args of the main method stores command line arguments
public class SortJava { public static void main(String[] args) { int n=5; int[] numbers = new int[n]; //The Math.random() method returns a random floating oint number in the range //[0,1). We multiply the random number by n and then we cast the type of the result //to integer. for(int i=0; i<numbers.length; i++) numbers[i]=(int)(Math.random()*n); System.out.println("Random numbers before sorting"); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); System.out.println(); System.out.println("Random numbers after sorting"); // We call sort method of Arrays class. // Array class is located in java.util.Arrays. Import //statement java.util.* allows us to access it. Arrays.sort(numbers); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); } }
Source Code, Compilation, Execution • Source code needs to have same as name of public class • There can only be one public class in a Java program • Source code needs to have extension .java • During compilation, the Java compiler produces a bytecode file • This file has the extension .class • The Java interpreter starts execution when we type command java followed by the class name