1 / 28

Introduction to java

Introduction to java. 3101-003 Class 1 Fall 2003 Shlomo Hershkop. Welcome. History First Java Application Data types Variables Strings Assignments Math, Boolean expressions Relational operations If statements System.exit. Very Brief History. Started in 1991 by SUN Microsystems

Download Presentation

Introduction to 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. Introduction to java 3101-003 Class 1 Fall 2003 Shlomo Hershkop Shlomo Hershkop

  2. Welcome • History • First Java Application • Data types • Variables • Strings • Assignments • Math, Boolean expressions • Relational operations • If statements • System.exit Shlomo Hershkop

  3. 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 Shlomo Hershkop

  4. Course Information • Textbook: Java in a NutshellReading class 1chapters 1,2 Shlomo Hershkop

  5. Basic Definitions • Java is an object oriented language. • Object • Method • Class • Applications • Applets • Native classes • Threads • Exceptions Shlomo Hershkop

  6. 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 Shlomo Hershkop

  7. How to get it running • Text in hello.java file • Why? • To compile: • javac hello.java • To run: • java hello Shlomo Hershkop

  8. 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 Shlomo Hershkop

  9. 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 Shlomo Hershkop

  10. Primitive data types Shlomo Hershkop

  11. Assignment • = • Example: int n; n = 10; or int n = 10; //same Shlomo Hershkop

  12. 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; Shlomo Hershkop

  13. Using Strings public class hello{ public static void main (String [] args) { String s = “Hello World\n”; System.out.println(s); //output simple string } //end main }//end class hello Shlomo Hershkop

  14. Math • Unary int x = -9; • Regular math (+,-,*,/) int y = 3+x; • % modulo operator Shlomo Hershkop

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

  16. Casting int n = 40; Wrong : byte b = n; why?? Right: byte b = (byte) n; Type casting converts to target type Shlomo Hershkop

  17. 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 ccan use its value by casting to inthow?? Shlomo Hershkop

  18. Assignment • += • -= • *= • /= • %= Shlomo Hershkop

  19. Boolean Expressions • boolean bb 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)); Shlomo Hershkop

  20. Relational Operators • == equality • != inequality • > greater than • < less than • >= greater than or equal to • <= less than or equal to Shlomo Hershkop

  21. if ( x < y) { x = y; } if ( x < y ) { x = y;}else { x = 88;} The if - branching statement Shlomo Hershkop

  22. If/Else • if (logic condition) {something}else if (logic condition) { something}else {something else} Shlomo Hershkop

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

  24. Switch/Case • Switch(variable){ case(1): something; break; case(23): something; break; default: something; } Shlomo Hershkop

  25. Exceptions • Java exception object. • java.io.Exception most general one.Some exception like in Throwable class define methods to get the message. Shlomo Hershkop

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

  27. 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); Shlomo Hershkop

  28. 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. Shlomo Hershkop

More Related