Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Data Structures in Java (c) IDMS/SQL News

Similar presentations

Presentation on theme: "Basic Data Structures in Java (c) IDMS/SQL News"— Presentation transcript:

1 Basic Data Structures in Java (c) IDMS/SQL News

2 Java - Chapter 1 (c) 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)

3 Java - Chapter 1 (c) 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

4 Java - Chapter 1 (c) 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; } }

5 Java - Chapter 1 (c) 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 of type test test1 = new test(); int xyz1; xyz1 = test1.sampleMethod(30);//we get 40 This is the essence of OO programming.

6 Java - Chapter 1 (c) 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

7 Java - Chapter 1 (c) 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!

8 Java - Chapter 1 (c) 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

9 Java - Chapter 1 (c) 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

10 Java - Chapter 1 (c) 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

11 Java - Chapter 1 (c) 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.

12 Java - Chapter 1 (c) 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.

13 Java - Chapter 1 (c) 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

14 Java - Chapter 1 (c) 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!

15 Java - Chapter 1 (c) 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

16 Java - Chapter 1 (c) 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;

17 Java - Chapter 1 (c) 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...

18 Java - Chapter 1 (c) 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]

19 Java - Chapter 1 (c) 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.

20 Java - Chapter 1 (c) 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

21 Java - Chapter 1 (c) 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?

22 Java - Chapter 1 (c) 22 Javadoc – part of JSDK Input is Java source Output – html files with help information C:\tvg\JCourse1>Javadoc Loading source file 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...

23 Java - Chapter 1 (c) 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...

Download ppt "Basic Data Structures in Java (c) IDMS/SQL News"

Similar presentations

Ads by Google