Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.

Similar presentations


Presentation on theme: "1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running."— Presentation transcript:

1 1 Programming Java Java Basics

2 2 Java Program Java Application Program Application Program written in general programming language Applet Program running in Web Browser Environment Can be viewed by appletviewer or Web browser with JVM

3 3 Classes and Objects Object Memory Space to Define State and Operation Instance of Class Class Template of Creating Object

4 4 The Java Class Libraries java.applet : Applet related java.awt : Abstract Window Toolkit java.awt.event : Event process from awt component java.awt.image : Image processing java.beans : JavaBeans Component java.io : File or Network I/O Support java.lang : Java Language Support java.net : Network related functions java.util : Utility function

5 5 Variables and Assignments Variables Types char 16bits Unicode character data boolean Boolean Variable byte 8 bits signed integer short 16 bits signed integer int 32 bits signed integer long 64 bits signed integer float 32 bits signed floating point number double 64 bits signed floating point number

6 6  Primitive data types Byte8-2 7 2 7-1 Short16-2 15 2 15 -1 Int32-2 31 2 31 -1 Long64 Float32 Double64 Boolean101 Char16

7 7  Variables Variables: Name Type Value Naming: May contain numbers,underscore,dollar sign, or letters Can not start with number Can be any length Reserved keywords Case sensitive

8 8 Variables and Assignments int num = 100; long m = 21234234L double type :.5 0.8 9e-2 -9.3e-5 float type : 1.5e-3f; Variable Declaration type varName; Value assignments varName = value; float x, y, x;

9 9 Type Conversions in Expression char ch; int i; float f; double outcome; ch = ‘0’; i = 10; f = 10.2f; outcome = ch * i / f; int float double

10 10 Type Conversions in Assignment Widening Conversion byte b = 127; int i; i = b; Narrowing Conversion byte b; int i = 258; b = (byte) i; Wrong Conversion byte b; int i = 127; b = i; Right Conversion (But data may be lost) byte b; int i = 127; b = (byte) i; Type Casting

11 11  Assignment = Example: int n; n = 10; or int n = 10; //same

12 12  Assignment += -= *= /= %=

13 13 Arithmetic Operators Operators + - * / % += -= *= /= %= ++ -- 5 / 2  2 Why isn’t it 2.5 ? 5 % 2  1 4 / 2  2 4 % 2  0 count * num + 88 / val – 19 % count j += 6;  j = j + 6;

14 14  Boolean Expressions boolean b b will be either true (1) or false (0) Logical operations: !(not), && (and) || (or) boolean a,b; a = true; b = false; System.out.println ( “ a && b is “ + (a && b));

15 15  Relational Operators ==equality !=inequality >greater than <less than >=greater than or equal to <=less than or equal to

16 16  Incrementing Increment and Decrement i++ equivalent to i = i + 1; Can also do ++i, which uses i before incrementing it. Decrementing: i--;

17 17  The if - branching statement if ( x < y) { x = y; } if ( x < y ) { x = y; } else { x = 88; }

18 18  If/Else if (logic condition) { something } else if (logic condition) { something } else { something else }

19 19  Nested IF if ( x < 0 ) { System.out.println( “ x is negative “ ); } else { if ( x > 0 ) { System.out.println ( “ x is positive ” ); } //end if x > 0 else { System.out.println ( “ x is zero “ ); } } //end else x >=0

20 20

21 21  Switch/Case Switch(variable){ case(1): something; break; case(23): something; break; default:something; }

22 22

23 23 Comments Single Line Comment int i = 10 ; // i is counter Multiple Line Comment /* Some comments */ Documentation Comment /** Documentation Comment */ Using “javadoc” Tool, make document

24 24 Arrays ( One Dimensional) type VarName[] Definition of One Dimensional Array int ia[]; varName = new type[size] Assign Range to One Dimensional Array ia = new int[10]; type varName[] = new type[size] Declaration of One Dimensional Array with Range int ia[] = new int[10];

25 25 Arrays ( One Dimensional) varName.length Number of elements in Array Type varName[] = {e0, …, en}; Initialization of One Dimensional Array int j[] = {0, 1, 2, 3, 4, 5}; int k[]; K = j; Example 1.15

26 26 Arrays ( Multi-Dimensional) type VarName[][]; Definition of Two Dimensional Array float fa[][]; varName = new type[size1][size2]; Assign Range to Two Dimensional Array fa = new float[2][3]; type varName[][] = new type[size1][size2]; Declaration of Two Dimensional Array with Range float fa[] = new float[2][3];

27 27 Arrays ( Two Dimensional) varName.length Number of elements in Multi-Dimensional Array Type varName[][] = {{e00, …, e0n}, {e10, …,e1y}, {e20, …, e2z}}; Initialization of Multi-Dimensional Array Example 1.16 varName[index].length Number of elements in Each elements of Multi-Dimensional Array Number of Raw

28 28 Java Keywords 50 Java Keywords abstractdoubleintsuper booleanelseinterfaceswitch breakextendslong synchronized bytefinalnativethis casefinallynewthrow catchfloatpackagethrows charforprivatetransient* classgoto*protectedtry const*ifpublicvoid continueimplementsreturnvolatile defaultimportshortwhile doinstanceofstatic strictfp assert (New in 1.5) enum (New in 1.5) The “assert” is recognized as keyword in JDK1.4 compiler, but we could not use it. It can be used it from 1.5.

29 29  First Application /** *Hello World, first application, only output. */ import java.io.*; public class hello{ public static void main (String [] args) { System.out.println( “ Hello World\n ” ); } //end main }//end class

30 30  How to get it running Text in hello.java file Why? To compile: javac hello.java To run: java hello

31 31  Notice: Java is CASE SENSITIVE!! Whitespace is ignored by compiler Whitespace makes things easier to read … hence it affects your grade File name has to be the same as class name in file. Need to import necessary class definitions


Download ppt "1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running."

Similar presentations


Ads by Google