Introduction to Programming in Java AP Computer Science

  • Slides: 26
Download presentation
Introduction to Programming in Java AP Computer Science A Dr. Persin www. Lnk 2

Introduction to Programming in Java AP Computer Science A Dr. Persin www. Lnk 2 Lrn. com 1

Contents • • • History First Java Application Data types Variables Strings Assignments Math,

Contents • • • History First Java Application Data types Variables Strings Assignments Math, Boolean expressions Relational operations If statements System. exit www. Lnk 2 Lrn. com 2

Very Brief History • Started in 1991 by SUN Microsystems • Targeted at consumer

Very Brief History • Started in 1991 by SUN Microsystems • Targeted at consumer electronics. Wanted reliable programming language. • Integrated into browsers • Evolved into write once run anywhere, integrates into Netscape • General purpose libraries released www. Lnk 2 Lrn. com 3

Basic Definitions • Java is an object oriented language. – – – – Object

Basic Definitions • Java is an object oriented language. – – – – Object Method Class Applications Applets Native classes Threads Exceptions www. Lnk 2 Lrn. com 4

First Application /** *Hello World, first application, only output. */ public class hello{ public

First Application /** *Hello World, first application, only output. */ public class hello{ public static void main (String [] args) { System. out. println(“Hello World”); } //end main }//end class www. Lnk 2 Lrn. com 5

Notice: • Java is CASE SENSITIVE!! • Whitespace is ignored by compiler • Whitespace

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 www. Lnk 2 Lrn. com 6

Variables • Variables: – Name – Type – Value • Naming: – – –

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 www. Lnk 2 Lrn. com 7

Primitive data types Byte Short Int Long Float Double Boolean Char 8 16 32

Primitive data types Byte Short Int Long Float Double Boolean Char 8 16 32 64 1 16 www. Lnk 2 Lrn. com -27 -215 -231 27 -1 215 -1 231 -1 0 1 8

Assignment • = • Example: int n; n = 10; or int n =

Assignment • = • Example: int n; n = 10; or int n = 10; //same www. Lnk 2 Lrn. com 9

Strings • Not a primitive class, its actually something called a wrapper class •

Strings • Not a primitive class, its actually something called a wrapper class • To find a built in class’s method use API documentation. • String is a group of char’s • A character has single quotes – char c = ‘h’; • A String has double quotes – String s = “Hello World”; • Method length – int n = s. length; www. Lnk 2 Lrn. com 10

Using Strings public class hello{ public static void main (String [] args) { String

Using Strings public class hello{ public static void main (String [] args) { String s = “Hello World”; System. out. println(s); //output simple string } //end main }//end class hello www. Lnk 2 Lrn. com 11

Math • Unary int x = -9; • Regular math (+, -, *, /)

Math • Unary int x = -9; • Regular math (+, -, *, /) int y = 3+x; • % modulo operator www. Lnk 2 Lrn. com 12

Incrementing • Increment and Decrement • i++ equivalent to i = i + 1;

Incrementing • Increment and Decrement • i++ equivalent to i = i + 1; • Can also do ++i, which uses i before incrementing it. • Decrementing: i--; www. Lnk 2 Lrn. com 13

Casting int n = 40; Wrong : byte b = n; why? ? Right:

Casting int n = 40; Wrong : byte b = n; why? ? Right: byte b = (byte) n; Type casting converts to target type www. Lnk 2 Lrn. com 14

Casting II • Type char is stored as a number. The ASCII value of

Casting II • Type char is stored as a number. The ASCII value of the character. • A declaration of : – char c = ‘B’; stores the value 66 in location c can use its value by casting to int how? ? www. Lnk 2 Lrn. com 15

Assignment • • • += -= *= /= %= www. Lnk 2 Lrn. com

Assignment • • • += -= *= /= %= www. Lnk 2 Lrn. com 16

Boolean Expressions • boolean b b will be either true (1) or false (0)

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)); www. Lnk 2 Lrn. com 17

Relational Operators • • • == != > < >= <= equality inequality greater

Relational Operators • • • == != > < >= <= equality inequality greater than less than greater than or equal to less than or equal to www. Lnk 2 Lrn. com 18

The if - branching statement • if ( x < y) { x =

The if - branching statement • if ( x < y) { x = y; } • if ( x < y ) { x = y; } else { x = 88; } www. Lnk 2 Lrn. com 19

If/Else • if (logic condition) { something } else { something else } www.

If/Else • if (logic condition) { something } else { something else } www. Lnk 2 Lrn. com 20

Nested IF if ( x < 0 ) { System. out. println( “ x

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 www. Lnk 2 Lrn. com 21

Switch/Case • Switch(variable){ case(1): something; break; case(23): something; break; default: something; } www. Lnk

Switch/Case • Switch(variable){ case(1): something; break; case(23): something; break; default: something; } www. Lnk 2 Lrn. com 22

Exceptions • Java exception object. • java. io. Exception most general one. Some exception

Exceptions • Java exception object. • java. io. Exception most general one. Some exception like in Throwable class define methods to get the message. www. Lnk 2 Lrn. com 23

try…. catch blocks. • Try { ……. } catch ( IOException v) { ……….

try…. catch blocks. • Try { ……. } catch ( IOException v) { ………. } www. Lnk 2 Lrn. com 24

System. out. println • println is a method in the Printstream class. • Defined:

System. out. println • println is a method in the Printstream class. • Defined: – public void println(String x) can be any type of string or combination string using addition to join parts. Example: println(“hello “ + “world “ + x); www. Lnk 2 Lrn. com 25

System. exit() • One method in java. lang. System • Defined: public static void

System. exit() • One method in java. lang. System • Defined: public static void exit ( int status) • Terminates currently running Java VM • Status is status code, non zero will usually mean something abnormal. • Used at end to indicate success, or in middle to signal problems. • For more practice go to http: //www. javabat. com www. Lnk 2 Lrn. com 26