1 / 23

Basic Data Structures in Java

Basic Data Structures in Java. (c) IDMS/SQL News http://www.geocities.com/idmssql. Language Features. Object Oriented Class – the basic entity of OOP Object – instance of a class Methods – process code within class Interpreted Lang – Bytecode

Download Presentation

Basic Data Structures in Java

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. Basic Data Structures in Java (c) IDMS/SQL News http://www.geocities.com/idmssql

  2. Language Features • Object Oriented • Class – the basic entity of OOP • Object – instance of a class • Methods – 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. HelloWorld Again public class HelloWorld { public static void main(String arguments[]) { System.out.println(“Hello World"); } } 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 - private int x; /** Constructor for objects of class test */ public test() {// initialise instance variables x = 10; } /** An example of a method – */ public int sampleMethod(int y) {// put your code here 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’ test1 of type test test1 = new test(); int xyz1; xyz1 = 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 case • Variables start with lowercase, each word with uppercase • Method – lowercase • Methods 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. Declaring Variables • Variables are declared in the C style; the type first, followed by the variables being declared • Variable names must start with a letter or an underscore character, “_” • Cannot start with a number. InterCap Style int inputCount; int currentCount23, finalValue; 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 variable static 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 You can initialize variables in a declaration. 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. 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 .. else if (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. String Not a basic data type! Used as if it is ! String in Java is implemented as a class Note: not ‘string’, capital ‘S’ is required String 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 • while • do ... while • for ... loop • Most 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 1 String names[]; Names = new String[3]; Names[0] = “Ada”; Names[1] = “Byron”; Names[2] = “Napolean”; Method 2 String 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 JSDK Input is Java source Output – html files with help information C:\tvg\JCourse1>Javadoc TestGreeting.java Loading source file TestGreeting.java... Constructing Javadoc information... Standard Doclet version 1.4.2_03 Generating 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 ... Java - Chapter 1 (c)http://www.geocities.com/idmssql

More Related