Basic Data Structures in Java (c) IDMS/SQL News .
-
Upload
alexander-beach -
Category
Documents
-
view
236 -
download
3
Transcript of Basic Data Structures in Java (c) IDMS/SQL News .
Basic Data Structures in Java
(c) IDMS/SQL News http://www.geocities.com/idmssql
Java - Chapter 1 (c)http://www.geocities.com/idmssql
2
Language Features
Object OrientedClass – the basic entity of OOPObject – instance of a classMethods – process code within class Interpreted Lang – Bytecode Builtin support for threading (concurrent
execution different pieces of code)
Java - Chapter 1 (c)http://www.geocities.com/idmssql
3
public class HelloWorld {public static void main(String arguments[]) { System.out.println(“Hello World"); }}
HelloWorld Again
Note:1. Any main ‘program’ must have a main method and it must be declared static. No instance is required. JVM can directly run this.
2. If a class has no main method, then the only way to use it is to call from another class. In practice, many classes are defined like this... See next page
Java - Chapter 1 (c)http://www.geocities.com/idmssql
4
/** * This is the template for a class */public class test{
// instance variables - // instance variables - private int x;private int x;/** Constructor for objects of class test *//** Constructor for objects of class test */public test()public test(){// initialise instance variables{// initialise instance variables
x = 10;x = 10; }}/** An example of a method – *//** An example of a method – */public int sampleMethod(int y)public int sampleMethod(int y){// put your code here{// put your code herereturn x + y;return x + y;}}
}
Java - Chapter 1 (c)http://www.geocities.com/idmssql
5
Instances
We have a class test and a method sampleMethod
How do we use them from another program?
test test1; // declare a new ‘variable’test test1; // declare a new ‘variable’
test1 of type testtest1 of type test
test1 = new test();test1 = new test();
int xyz1;int xyz1;
xyz1 = test1.sampleMethod(30);//we get 40xyz1 = test1.sampleMethod(30);//we get 40
This is the essence of OO programming.
Java - Chapter 1 (c)http://www.geocities.com/idmssql
6
OO features / conventions
Class name starts with Upper caseVariables start with lowercase, each word
with uppercaseMethod – lowercaseMethods must return something else
declared as void
Java - Chapter 1 (c)http://www.geocities.com/idmssql
7
Capitalization
Java is picky about capitalization;
System.out.println() vs. system.out.println()
are not the same
HelloWorld and helloworld are not the same!
Java - Chapter 1 (c)http://www.geocities.com/idmssql
8
Datatypes – 8 of them
boolean: true or false. 1 bit size char: Unicode character. 16 bits. byte: very small integer number; -128-127, 8 bits short: a smallish integer number, -32,768 to 32,767 int: normal integer, 32 bits, -2,147,483,648 to 2,147,483,647 long: a really big 64 bit integer, -9223372036854775808 to
+ 9223372036854775807 float: 32 bit floating point number, -3.40292347E+38 to +
3.40292347E+38 double: 64 bit long floating point number, good for -E+308 to +E+308 and about twice the number
of digits of precision as a float
Java - Chapter 1 (c)http://www.geocities.com/idmssql
9
Variable names must start with a letter or an underscore character, “_”
Cannot start with a number.
Declaring Variables
Variables are declared in the C style; the type first, followed by the variables being declared
int inputCount;int currentCount23, finalValue;
InterCap Style
Java - Chapter 1 (c)http://www.geocities.com/idmssql
10
3 types of variable declarations
instance, class and local variables
class Variables1 {double salary = 2534.50 ;// instance variablestatic int counter1; // class variable
public static void main (String args[]){ int temp1 = 0; // local variable defined inside a method System.out.println ("counter1=" + counter1 + " temp1=" + temp1 ); } // end of method main} // end of class Variables1
Java - Chapter 1 (c)http://www.geocities.com/idmssql
11
Initialize
Double initialSpeed =10.0, finalSpeed=60.0, currentSpeed = 0.0; char endChar = ‘a’;
Note: Instance and static(=class) variables will be initialized to default values. Local variable will NOT be initialized automatically.
You can initialize variables in a declaration.
Java - Chapter 1 (c)http://www.geocities.com/idmssql
12
Scope of a Variable
IBM doc says “variable scope can be confusing in Java”
An instance method has access to all (instance, class and local variables)
A class method (= defined static) has access to only class variables and local variables
Local variables are limited to within a method. Outside the method they do not exist.
Java - Chapter 1 (c)http://www.geocities.com/idmssql
13
Assignments
Similar to any other language… double currentSpeed = 0.0; int clock = 0; currentSpeed = currentSpeed + 1.0; clock++; // known as postincrement // clock = clock + 1; // same as above A number like 14.65 is treated as double! float price1; price1 = 14.65; Gives compile error price1 = 14.65f; // ok now
Java - Chapter 1 (c)http://www.geocities.com/idmssql
14
if .. elseif (booleanTest) { // code for Value is true. } else { // code for Value is false.}
eg:int weight1 = 100, weight2=200; if ( weight1 == weight2) {System.out.print(“weight1 equals weight2 ");} else {System.out.print(“weight1 not equals weight2 ");} Note the difference between the equality operator (==) and
the assignment operator (=)
Note!
Java - Chapter 1 (c)http://www.geocities.com/idmssql
15
StringNot a basic data type!Used as if it is !
String in Java is implemented as a class Note: not ‘string’, capital ‘S’ is requiredString str1 = new String(“string value”);String str2 = “ string value”;Char char1 = ‘A’; //String and char are not the same!
More on String later
Java - Chapter 1 (c)http://www.geocities.com/idmssql
16
Final Variables (=constants)
A final variable is one that cannot be changed. Any attempt to change it, will result in a compile-time error. This is done with the keyword ‘final’ in the declaration.
Typically they are in CAPITAL letters
double final PI = 3.14159265359 ;int final SPEED_LIMIT = 70;
Java - Chapter 1 (c)http://www.geocities.com/idmssql
17
Other Syntax
whiledo ... whilefor ... loopMost are simple and straightforward
Switch – will be discussed later
All others see syntax page at the end ...
Java - Chapter 1 (c)http://www.geocities.com/idmssql
18
Arrays
datatype arrayName [] = new datatype[25];
or datatype [] arrayName = new datatype[25];
eg:
String ErrorCode [] = new String[50];
The first/last elements are ErrorCode[0] ... ErrorCode[49]
Java - Chapter 1 (c)http://www.geocities.com/idmssql
19
Initializing Arrays
Method 1String names[];Names = new String[3];Names[0] = “Ada”;Names[1] = “Byron”;Names[2] = “Napolean”;Method 2String names[] = {“Ada”; “Byron”; “Napolean”};Here definition and initialize are done at once.
Java - Chapter 1 (c)http://www.geocities.com/idmssql
20
Hello World Again
In the HelloWorld
public static void main (String args[]) { ...}
The main method takes an array as input arguments. If I say
Java HelloWorld John David
These names are passed to args[0], args[1]...
Here follows the modified program
Java - Chapter 1 (c)http://www.geocities.com/idmssql
21
Example
class Hello2 { public static void main (String args[]) { int i; if (args.length == 0) System.out.println("Hello Nobody "); else System.out.println("Arguments length= " + args.length); for (i=0; i < args.length; i = i+1) { System.out.println("Hello " + args[i]); } }} Try Java Hello2 * What do you get?
Java - Chapter 1 (c)http://www.geocities.com/idmssql
22
Javadoc – part of JSDKInput is Java sourceOutput – html files with help informationC:\tvg\JCourse1>Javadoc TestGreeting.javaLoading source file TestGreeting.java...Constructing Javadoc information...Standard Doclet version 1.4.2_03Generating constant-values.html...Building tree for all the packages and classes...Building index for all the packages and classes...Generating overview-tree.html...Generating index-all.html... Generating deprecated-list.html...Building index for all classes...Generating allclasses-frame.html...Generating allclasses-noframe.html...Generating index.html...Generating packages.html...Generating TestGreeting.html...Generating package-list...Generating help-doc.html...
Java - Chapter 1 (c)http://www.geocities.com/idmssql
23
Java Buzzwords Java2 Platforms - Standard Edition J2SE - Enterprise Edition – J2EE - Micro Edition – J2ME (PDA)Every Platform has Run time – JRE (can be shipped with application) Dev Kit – JDK (includes JRE)Enterprise Information System Servlet, JSP, J2EE Client, Applet...Application Server JDBC, Beans, JavaBeans, EJB ...